Write a java program that reads a file (input.txt) and produces a histogram of t
ID: 3822582 • Letter: W
Question
Write a java program that reads a file (input.txt) and produces a histogram of the most frequently used words (all in lower case) found in the text. Ignore capitalization, whitespace, and punctuation. Sort the output in order of increasing usage of words. Words with same frequency can appear in any order.
You need to develop 3classes: 1-Class Node with two instance variables to record a word and the frequency of that. Class Node implements Comparable Node Node String word Int freq 2- Class Algorithm with two methods 2-1)Method sort: which gets a Vector of Nodes as its parameter and it returns the sorted 2-2) Method printVector: which gets a Vector of Nodes as its parameter and it prints the vector based on the above format (Sample Output) Algorithm static.... Sort( Static print Vector( 3- Class Test, Which reads the input file and creates a vector of Nodes (for the words in the file), and sorts and prints the result using methods in class Algorithm.Explanation / Answer
Node Class
/* This class implements Node as given in the description */
public class Node implements Comparable<Node> {
public String word;
public int freq;
/* default constructor */
public Node(){
this.word="";
this.freq=0;
}
/* constructor for node class */
public Node(String _word, int _freq ){
this.word=_word;
this.freq=_freq;
}
@Override
public int compareTo(Node arg0) {
/* *
* if this.freq > arg0.freq returns >0
* else if this.freq < arg0.freq returns <0
* else returns 0
*/
return Integer.compare(this.freq,arg0.freq );
}
}
Algorithm Class
import java.util.Collections;
import java.util.List;
/* This class provides the method to sort and to print Node */
public class Algorithm {
/* This method sorts nodes using frequencies */
public static List<Node> sort(List<Node> vector){
Collections.sort(vector); /* sort it using inbuilt java sorting */
return vector; /* return after sorted vector */
}
/* This method prints vector */
public static void printVector(List<Node> vector){
for (Node node:vector){
System.out.println(node.word+ " => "+node.freq);
}
}
}
Test Class
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/* This class reads from input file and tests sorting using algorithm class*/
public class Test {
private File file; /* file object pointing towards input file*/
public List<Node> vector;
/* constructor taking file name to initialize file object */
public Test(String fileName){
this.file=new File(fileName);
}
/* this method reads input from the file and stores word in a hashmap*/
private void read(){
HashMap<String,Integer> map=new HashMap<String,Integer>(); /* hashmap to store words and count*/
try{
FileReader input=new FileReader(this.file); /* create file reader object */
BufferedReader reader=new BufferedReader(input); /* create buffered reader object */
String line=reader.readLine();/* read first line from input */
while(line!=null){ /*keep reading while EOF*/
line=line.toLowerCase(); /*convert to lower case */
String words[]=line.split("[^a-z]+"); /*split on any character which is not letter*/
for(String word:words){
if(word.length()>0 && map.containsKey(word)){ /* update word frequency */
int oldFreq=map.get(word);
map.put(word,oldFreq+1);
}else if(word.length()>0){ /* not in map */
map.put(word, 1); /* put in map*/
}
}
line=reader.readLine(); /* read next line */
}
}catch(Exception e){
System.out.println("Read line exception");
}
this.vector=new ArrayList<Node>(); /* create vector of nodes */
for(String key :map.keySet()){
this.vector.add(new Node(key,map.get(key))); /* add node to vector*/
}
}
/* this method tests the whole system and prints output*/
public void test(){
this.read();
this.vector=Algorithm.sort(this.vector);
Algorithm.printVector(this.vector);
}
/*Main method*/
public static void main(String args[]){
Test test=new Test("input.txt");
test.test();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.