The Artima Developer Community
Sponsored Link

Java Answers Forum
Sentence Counter Help,please :)

5 replies on 1 page. Most recent reply: Oct 20, 2002 12:52 PM by Natalie

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Sentence Counter Help,please :) Posted: Apr 21, 2002 4:26 AM
Reply to this message Reply
Advertisement
I am writing a program that counts the # of words,sentences,and alphabetic chars in a file imported by the user. I have everything working but found a problem with sentences, it works but it doesn't. I've never used BreakIterator before but it seemed like the best solution for this program.

Now my problem, it counts the number of sentences as long as there's more than one.

Ex: I am natalie. -- Will output 1 sentence found, and
I am natalie. I am natalie. -- Will output 2, etc. BUT
i -- Will output 1 sentence found. Anyone know why?

Okay here's the code if it helps:

if (selName == searchNames[0]) {
//read in file
File inFile = chooser.getSelectedFile();
FileReader reader = new FileReader(inFile);
BufferedReader in = new BufferedReader(reader);
String line; //line read in
int count = 0;
//while the file is not at its end
while ((line = in.readLine()) != null) {
// create a sentence break iterator, set to UK structure
BreakIterator bit = BreakIterator.getSentenceInstance(Locale.UK);
bit.setText(line);
// iterate across the string
int start = bit.first();
int end = bit.next();
while (end != BreakIterator.DONE) {
String sentence = line.substring(start, end);
count ++;
start = end;
end = bit.next();
}
}
JOptionPane.showMessageDialog(null,"There are: " +count+ " sentences.");
}//end choice [0]


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Sentence Counter Help,please :) Posted: Apr 21, 2002 7:22 PM
Reply to this message Reply
I think you need to read the whole file in to a string variable, then use the break iterator on the whole file string, counting each instance using the break iterator next() method until the break iterator ahs iterated to the the last occurrence.
Something like the following maybe? This is an untested snippet. I've go to go. Hope it helps.
Charles


public void countSentences(){
File inFile = chooser.getSelectedFile();
FileReader reader = new FileReader(inFile);
BufferedReader in = new BufferedReader(reader);
String line = ""; //line read in
String fileText = "";
//while the file is not at its end
while ((line = in.readLine()) != null) {
fileText = fileText + line;
}
// now the whole file is read in, so now count the sentences.
int count = 0;
// create a sentence break iterator, set to UK structure
BreakIterator bit = BreakIterator.getSentenceInstance(Locale.UK);
bit.setText(fileText);
// iterate across the string
int current = bit.first();
int last = bit.last();
while (current != last) {
count ++;
current = bit.next();
}
System.out.println("There are " + String.valueOf(count) + " sentences of the file " + inFile.getName());
}

Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Re: Sentence Counter Help,please :) Posted: Apr 22, 2002 3:19 AM
Reply to this message Reply
Thanks for your, help. :) I tried that out but it freezes the program. I'll have a play around with it and see if I can figure out what's wrong. Thanks very much though!

Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Re: Sentence Counter Help,please :) Posted: Apr 22, 2002 4:24 AM
Reply to this message Reply
If I use my old code and just change the fact that it is read into a string, and use that code, it still does the same thing as before. Plus, if I close off the while statement for the line != null, after reading it into fileText, it doesn't read the file correctly, if it contains something like:

I am Natalie.
I am Natalie.
I am Natalie.

It outputs that there is 1 sentence. If you have any other ideas, I'll all up for options.

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Sentence Counter Help,please :) Posted: Apr 22, 2002 8:10 AM
Reply to this message Reply

/* SentenceProcessor.java
* @author: Charles Bell
* @version: Apr 22, 2002
* Copyright ? 2002 QuantumHyperSpace.com
* <charbell@bellsouth.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. (http://www.fsf.org/copyleft/lgpl.html)
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You can request a copy of the GNU Library General Public
* License by writing to the Free Software Foundation at:
* Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.*;

/** SentenceProcessor is a a program that counts the # of words,sentences,
* and alphabetic chars in a file imported by the user.
*/
public class SentenceProcessor{

/** Creates a SentenceProcessor object and initiates sentence processing.
*/
public static void main(String[] args){

SentenceProcessor sentenceprocessor = new SentenceProcessor();
sentenceprocessor.process();
}

/** Iterates processing of files with the user until the user selects that they want to
* stop using a JOptionPane showConfirmDialog. Prompts the user for a file input to process.
* Reads the text from the selected file and then uses Break Iterators for sentence,
* word, and character processing to count the number of sentences, words, alphanumeric
* characters, and alphabet characters,
*/
public void process(){
boolean done = false;
while (!done){
try{
File file = getFile();
if (file != null) {
StringBuffer sb = new StringBuffer();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String lineRead = "";
while ((lineRead = br.readLine()) != null){
sb.append(lineRead + "\n");
}
String fileText = sb.toString();
String message = "There are " + String.valueOf(countSentences(fileText)) + " sentences in file " + file.getName() + "\n";
message = message + "There are " + String.valueOf(countWords(fileText) ) + " words in file " + file.getName() + "\n";
message = message + "There are " + String.valueOf(countCharacters(fileText) ) + " letter or digit Characters in file " + file.getName() + "\n";
message = message + "There are " + String.valueOf(countAlphabetCharacters(fileText) ) + " Alphabet Characters in file " + file.getName() + "\n";
JOptionPane.showMessageDialog(null,message);
}
}catch(FileNotFoundException fnfe){
System.err.println("FileNotFoundException: " + fnfe.getMessage());
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
done = (JOptionPane.showConfirmDialog(null,"Do you want to do another?") == JOptionPane.NO_OPTION);
}
System.exit(0);
}


/** Counts the number of sentences in the input string.
*/
public int countSentences(String s){
int count = 0;
BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.UK);
iterator.setText(s);
int current = iterator.current();
while (current < s.length()){
count++;
current = iterator.following(current);
}
return count;
}

/** Counts the number of words in the input string.
*/
public int countWords(String s){
int count = 0;
BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
iterator.setText(s);
int current = iterator.current();
while (current < s.length()){
if (Character.isLetter(s.charAt(current))) count++;
current = iterator.next();
}
return count;
}

/** Counts the number of Letters or Digits in the input string.
*/
public int countCharacters(String s){
int count = 0;
BreakIterator iterator = BreakIterator.getCharacterInstance(Locale.UK);
iterator.setText(s);
CharacterIterator charIteractor = iterator.getText();
int current = charIteractor.getIndex();
while (current < s.length()){
char c = charIteractor.current();
if (Character.isLetterOrDigit(c)) count++;
char nextChar = charIteractor.next();
current = charIteractor.getIndex();
}
return count;

}

/** Counts the number of Letters in the input string.
*/
public int countAlphabetCharacters(String s){
int count = 0;
BreakIterator iterator = BreakIterator.getCharacterInstance(Locale.UK);
iterator.setText(s);
CharacterIterator charIteractor = iterator.getText();
int current = charIteractor.getIndex();
while (current < s.length()){
char c = charIteractor.current();
if (Character.isLetter(c)) count++;
char nextChar = charIteractor.next();
current = charIteractor.getIndex();
}
return count;

}

/** Uses JFileChooser to prompt the user to select a file
*/
public File getFile(){
File file = null;
File currentDirectory = new File("."); // a single period denotes the current directory
JFileChooser chooser = new JFileChooser(currentDirectory);
int returnVal = chooser.showOpenDialog(null); //this is the parent JFrame
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
return file;
}

}

Natalie

Posts: 10
Nickname: natalie
Registered: Mar, 2002

Re: Sentence Counter Help,please :) Posted: Oct 20, 2002 12:52 PM
Reply to this message Reply
Thanks so much, I actually didn't get your message until later but while I was trying to figure out a way to do this I stumbled across the whole Sentence,Word Iterators and used them. My code wasn't as nice as yours but it worked. :) Thanks

Flat View: This topic has 5 replies on 1 page
Topic: Installing a eIRC chatroom Previous Topic   Next Topic Topic: calling servlet in iPlanet Web Server.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use