In: Computer Science
Java 20 most frequent words in a text file. Words are supposed to be stored in array that counts eah word.
Write a program that will read an article, parse each line into words, and keep track of how many times each word occurred. Run this program for each of the two articles and print out the 20 most frequently appearing words in each article.
You may think you need to use a StringTokenizer, but it turns
out that is old programming. The better thing to use is
string.split(regex)
Example of text to use:
If the subsidies vanish, low-income Americans who obtain insurance through Obamacare online marketplaces where insurers can sell policies would face higher insurance premiums and out-of-pocket medical costs. It would particularly hurt lower-middle-class families whose incomes are still too high to qualify for certain government assistance. About 10 million people are enrolled in Obamacare through its online marketplaces, and most receive subsidies. Trump’s action came just weeks before the period starting on Nov. 1 when individuals have to begin enrolling for 2018 insurance coverage through the law’s marketplaces.
package march11; import java.util.*; public class FrequentWord { static String artical1 = "If the subsidies vanish, low-income Americans who obtain insurance through Obamacare \n" + "online marketplaces where insurers can sell policies would face higher insurance premiums \n" + "and out-of-pocket medical costs. It would particularly hurt lower-middle-class families \n" + "whose incomes are still too high to qualify for certain government assistance."; static String artical2 = "About 10 million people are enrolled in Obamacare through its online marketplaces, and \n" + "most receive subsidies. Trump’s action came just weeks before the period starting on \n" + "Nov. 1 when individuals have to begin enrolling for 2018 insurance coverage through \n" + "the law’s marketplaces."; public static void main(String[] args) { HashMap<String, Integer> stringIntegerHashMap = new HashMap<>(); String[] artial1Array = artical1.split("\\s+"); String[] artial2Array = artical2.split("\\s+"); ////add array1 for (int i = 0; i < artial1Array.length; i++) { String word = artial1Array[i]; if (stringIntegerHashMap.containsKey(word)) { stringIntegerHashMap.put(word, stringIntegerHashMap.get(word) + 1); } else { stringIntegerHashMap.put(word, 1); } } ////add array2 for (int i = 0; i < artial2Array.length; i++) { String word = artial2Array[i]; if (stringIntegerHashMap.containsKey(word)) { stringIntegerHashMap.put(word, stringIntegerHashMap.get(word) + 1); } else { stringIntegerHashMap.put(word, 1); } } //////Print 20 stringIntegerHashMap= getSortedHashMapByValue(stringIntegerHashMap); int count = 0; for (String word : stringIntegerHashMap.keySet()) { System.out.println(word + " " + stringIntegerHashMap.get(word)); if (count == 20) { break; } count++; } } private static HashMap getSortedHashMapByValue(HashMap map) { List list = new LinkedList(map.entrySet()); Collections.sort(list, new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) ((Map.Entry) (o2)).getValue()) .compareTo(((Map.Entry) (o1)).getValue()); } }); HashMap sortedHashMap = new LinkedHashMap(); for (Iterator it = list.iterator(); it.hasNext();) { Map.Entry entry = (Map.Entry) it.next(); sortedHashMap.put(entry.getKey(), entry.getValue()); } return sortedHashMap; } }
////////output/////////