In this assignment, you will build a word counter. The programwill read and parse text from a .txt file, get the counts of wordsin the file, and then write the results to a new file.
There are four main files in the program: Main.java,MyFileReader.java, MyFileWriter.java, and WordCounter.java. TheMyFileReader class will read the provided “war_and_peace.txt” fileand return the cleaned up contents of the file as an ArrayList. TheWordCounter class will process the array list provided byMyFileReader and calculate the count of each word in the list oflines. It will keep track of the words and their counts in aHashMap. WordCounter will also provide a way (method) of getting alist of words that are repeated in the text a specific number oftimes, and the MyFileWriter class will write the list of words to anew file “output.txt”.
The Main class will drive the entire program, and the “main”method calls have been provided for you. Nothing needs to becompleted in the main method.
Main.java
import java.util.ArrayList;
import java.util.Map;
/**
* Main class to control the flow of the program.
*/
public class Main {
public static void main(String[] args) {
// Create new File Reader
MyFileReader fr = newMyFileReader("war_and_peace.txt");
// Create new File Writer
MyFileWriter fw = newMyFileWriter("output.txt");
// Get clean lines from the file
ArrayList<String> lines =fr.getCleanContent();
// Create new Word Counter with theclean lines
WordCounter wc = newWordCounter(lines);
// Get word count map
Map<String, Integer> counters= wc.getWordCounter();
// Get and print the counts of somewords
System.out.println(counters.get("Still"));
System.out.println(counters.get("still"));
System.out.println(counters.get("in"));
// Get the words repeated 5000 timesor more
ArrayList<String> words =wc.getWordsOccuringMoreThan(5000);
// Write the words to a file
fw.writeToFile(words);
}
}
1.MyFileReader.java
import java.util.ArrayList;
/**
* Class to read lines from a file.
*/
public class MyFileReader {
/**
* Name of file being read.
*/
private String filename;
/**
* Creates MyFileReader with given filenameto read.
* @param filename to read
*/
public MyFileReader(String filename) {
this.filename = filename;
}
/**
* Opens the file specified by filename andreads the text line by line.
* Cleans up each line by trimming whitespacefrom the beginning and end of each line.
* Adds each line to anArrayList<String> which is returned from the method.
* If a line is empty (does not contain anytext), it's skipped and is not added to theArrayList<String>.
*
* Example:
* - Calling getCleanContent() will open andread the file, clean up the text line by line,
* add each line containing text to anArrayList<String>, and return the ArrayList<String>from the method.
* If there is a line "nice to meetyou ", it will become "nice to meet you"
* If there is a line " hello world ", it will become "hello world"
* If there is a line "today is a niceday", it will remain "today is a nice day"
* If there is a line " ", it will beskipped and not added to the ArrayList<String>
*
* @return list of lines with no empty spacesat the beginning or end of each line
*/
public ArrayList<String> getCleanContent(){
ArrayList<String> lines = newArrayList<String>();
//TODO Implement method
return lines;
}
}
2. MyWriter.java
import java.util.ArrayList;
/**
* Class to write a list of words to a file.
*/
public class MyFileWriter {
/**
* Name of file being written to.
*/
private String filename;
/**
* Creates MyFileWriter with given filenameto write to.
* @param filename to write to
*/
public MyFileWriter(String filename) {
this.filename = filename;
}
/**
* Opens the file specified by filename andwrites each word in the given list of words to the file.
* Each word is written to a new line in thefile.
*
* Example(s):
* - If the given list of words contains:"a", "he", "in", "of", "to"
* CallingwriteToFile(ArrayList<String> words) will open the file, thenwrite each word in the
* given list of words to the file, in thefollowing format:
* a
* he
* in
* of
* to
*
* @param list of words to write to thefile
*/
public void writeToFile(ArrayList<String>words) {
//TODO Implement method
3. WorldCounter.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
/**
* Counts words in a given list of lines from a file.
*/
public class WordCounter {
/**
* List of lines of words to count.
*/
private ArrayList<String> lines;
/**
* Map storing the count of each word in thelist of lines.
* Each word will be a key, and theassociated counts of each word will be the values.
*/
private Map<String, Integer> wordCount;
/**
* Creates WordCounter based on the givenlist of lines.
* Starts the process of generating the countof each word in the list.
* @param lines of words to count
*/
public WordCounter(ArrayList<String> lines){
this.lines = lines;
this.wordCount = newHashMap<String, Integer>();
this.generateWordCounts();
}
/**
* Calculates the count of each word in thelist of lines.
* Traverses the list of lines, and keepstrack of the count of each word.
* Stores each word as a key and itsassociated count as a value in the HashMap<String, Integer>wordCount.
*
* Note, the words (keys) are case-sensitive.
* e.g. "Hello" is considered a differentword (key) than "hello".
* e.g. "UFO" is considered the same word as"UFO".
*
* Hint(s):
* - Traverse the list of lines and spliteach one into an array of words (strings). Use the words askeys and
* the associated counts as values in theHashMap<String, Integer> wordCount.
*
* Example(s):
* - If the list of lines contains:
* "war and the"
* "war the peace peace"
* "the war the"
*
* Calling generateWordCounts() wouldpopulate the HashMap<String, Integer> wordCount with:
* ("war", 3), ("and", 1), ("the", 4),("peace", 2)
*
* Example(s):
* - If the list of lines contains:
* "War and the"
* "war the Peace peace"
* "thE war The"
*
* Calling generateWordCounts() wouldpopulate the HashMap<String, Integer> wordCount with:
* ("War", 1), ("war", 2), ("and", 1),("the", 2), ("The", 1), ("thE", 1), ("Peace", 1), ("peace", 1)
*/
private void generateWordCounts() {
//TODO Implement method
}
/**
* Gets the HashMap<String, Integer>wordCount.
* @return wordCount
*/
public Map<String, Integer> getWordCounter(){
return this.wordCount;
}
/**
* Gets a list of words that appear aparticular number of times, indicated by the given threshold.
*
* Hint(s):
* - For each word (key) in the wordCountmap, check if the associated word count (value) is >=threshold.
*
* Example(s):
* - If the list of lines contains:
* "war and the"
* "war the peace peace"
* "the war the"
*
* Calling getWordsOccuringMoreThan(3) wouldreturn an ArrayList<String> with: "war", "the"
* Because "war" appears 3 times and "the"appears 4 times.
*
* Example(s):
* - If the list of lines contains:
* "War and the"
* "war the Peace peace"
* "thE war The"
*
* Calling getWordsOccuringMoreThan(2) wouldreturn an ArrayList<String> with: "war", "the"
* Because "war" appears 2 times and "the"appears 2 times.
*
* Example(s):
* - If the list of lines contains:
* "War and the"
* "war the Peace peace"
* "thE war The"
*
* Calling getWordsOccuringMoreThan(-1) wouldreturn an ArrayList<String> with:
* "War", "war", "and", "the", "The", "thE","Peace", "peace"
* Because all words appear more than -1times.
*
* @param threshold (minimum word count) forwords to include in the returned list,
* where each word has a word count >=threshold.
* @return list of words, where each has acount >= threshold
*/
public ArrayList<String>getWordsOccuringMoreThan(int threshold) {
ArrayList<String> result = newArrayList<String>();
//TODO Implement method
return result;
}
Other Information:
Text 1:
Lines Other Info"The Project Gutenberg EBook of War and Peace, by Leo Tolstoy" Author: Leo Tolstoy CHAPTER I "Well, Prince, so Genoa and Lucca are now just family estates ofthe""Buonapartes. But I warn you, if you don't tell me that this meanswar," if you still try to defend the infamies and horrors perpetrated bythat
Text 2:
In the first case it was necessary to renounce the consciousnessof an
unreal immobility in space and to recognize a motion we did notfeel;
in the present case it is similarly necessary to renounce afreedom
"that does not exist, and to recognize a dependence of which we arenot" conscious.
Most people start at our Web site which has the main PG searchfacility:
http://www.gutenberg.org
}
}
}
In this assignment, you will build a word counter. The program will read and parse text from a .txt file, get the counts
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am