CIT 591 Assignment 6: Word Counter
Fall 2008, David Matuszek
ArrayList class).Read in a list of stop words, one per line. A stop word is an "unimportant" word, like "the" or "of," that doesn't help indicate what the text is all about.
Read in two files, one containing stop words, and one containing English text. Count how many times each word occurs in the second file. Provide methods to determine which are the most common words, and to determine how common particular words are.
Since we have not yet discussed how to read and write files, I am
providing the method
public static ArrayList<String> loadFile(String
message) throws IOException.
When called, loadFile() will ask the user for an input file
(using the message as the title of a dialog), and will return
an arrayList of Strings; each String in the arrayList will represent one
line of the file. Copy this method into your project.
Here are the requirements:
WordCounter.wordCounter.WordCounter.
WordCounter a no-argument constructor that
uses the provided loadFile method to get (1) the list of stop words
and (2) the text to count words in.public methods:
public String getWord(int i, boolean includeStopWords)ith most common word in the file,
where 1 indicates the most common, 2 indicates
the second most common, etc. All words are returned in lowercase,
with no spaces. If there is no ith word, the
method returns null.includeStopWords
is true and ignored if includeStopWords
is false.public int getWordCount()public int getRank(String word, boolean includeStopWords)1 represents
the most common word), or 0 if the word is not among those
that have been counted.includeStopWords
is true and ignored if includeStopWords
is false.it's).private unless
you have a good reason to do otherwise. @author tags.)You do not need a main method; all your testing can be done from JUnit.
// import java.io.*and java.util.ArrayList;
public static ArrayList<String> loadFile(String message) throws IOException {
BufferedReader reader = null;
ArrayList lines = null;
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle(message);
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
if (file != null) {
String fileName = file.getCanonicalPath();
reader = new BufferedReader(new FileReader(fileName));
lines = new ArrayList<String>();
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
}
}
return lines;
}
An ArrayList<String> is like an array, except that it doesn't have a fixed size;
it can be expanded as needed. Since you don't know how many words you are going to encounter, you
need an ArrayList instead of an array.
The angle brackets are an odd syntax that we haven't yet talked about. Just think of the whole thing,
ArrayList<String>, as meaning an ArrayList that contains Strings. Use it just like
an ordinary class name. So you can say things like
ArrayList<String> words = new ArrayList<String>();
and it will work fine.
You can create an ArrayList of any object type, for example, ArrayList<Integer>.
You can't create an array list of a primitive type,
for example, ArrayList<int>.
For planning what methods you will need, you can assume that you can do
anything with an ArrayList<String> that you can do with String[] array,
but you can also change its size by adding and removing things. You just
don't get to use the special [] syntax; you have to use Object syntax.
ArrayLists
are described in the Java API, in the java.util package.
Thursday, October 23, before midnight. Submit to Blackboard one copy of your project directory, properly zipped. Both your names should be in Javadoc comments in the classes. In addition, whichever of you turns the program in, should put your partner's name in the comments field in Blackboard.