Separate, count and sort the words in the example text file, electricity.txt (se
ID: 3862884 • Letter: S
Question
Separate, count and sort the words in the example text file, electricity.txt (see link at the class homepage). Sort in the following orders and your output should be nicely lined up in columns to the output file.
alphabetically (ignoring capitalization),
alphabetically with upper case words just in front of lower case words with the
same initial characters
by frequency, from high to low, (any order for equal frequency)
by frequency, with alphabetical order for words with the same frequency
I have added the link for the information located here - http://jjcweb.jjay.cuny.edu/jwkim/class/mat374-spring-17/electricity.txt
Explanation / Answer
Here is Java Program:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Map.Entry;
public class TestApp {
public Map<String, Integer> getWordCount(String fileName){
FileInputStream fis = null;
DataInputStream dis = null;
BufferedReader br = null;
Map<String, Integer> wordMap = new HashMap<String, Integer>();
try {
fis = new FileInputStream(fileName);
dis = new DataInputStream(fis);
br = new BufferedReader(new InputStreamReader(dis));
String line = null;
while((line = br.readLine()) != null){
StringTokenizer st = new StringTokenizer(line, " ");
while(st.hasMoreTokens()){
String tmp = st.nextToken().toLowerCase();
if(wordMap.containsKey(tmp)){
wordMap.put(tmp, wordMap.get(tmp)+1);
} else {
wordMap.put(tmp, 1);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try{if(br != null) br.close();}catch(Exception ex){}
}
return wordMap;
}
public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){
Set<Entry<String, Integer>> set = wordMap.entrySet();
List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);
Collections.sort( list, new Comparator<Map.Entry<String, Integer>>()
{
public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 )
{
return (o2.getValue()).compareTo( o1.getValue() );
}
} );
return list;
}
public static void main(String a[]) throws FileNotFoundException, IOException{
TestApp ta = new TestApp();
Map<String, Integer> wordMap = ta.getWordCount("C:/electricity.txt");
List<Entry<String, Integer>> list = ta.sortByValue(wordMap);
PrintWriter outputfile = new PrintWriter("c:/output3.txt");
System.out.println("WORD COUNT ");
outputfile.println("WORD COUNT ");
for(Map.Entry<String, Integer> entry:list){
System.out.println(entry.getKey()+" "+entry.getValue()+" ");
outputfile.println(entry.getKey()+" "+entry.getValue()+" ");
}
outputfile.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.