Charles Bell
Posts: 519
Nickname: charles
Registered: Feb, 2002
|
|
Re: Sentence Counter Help,please :)
|
Posted: Apr 22, 2002 8:10 AM
|
|
/* 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; }
}
|
|