Skip to content

Instantly share code, notes, and snippets.

@yupadhyay
Created September 26, 2018 04:00
Show Gist options
  • Save yupadhyay/dc00461d2ddc3265dd34747cbd865de6 to your computer and use it in GitHub Desktop.
Save yupadhyay/dc00461d2ddc3265dd34747cbd865de6 to your computer and use it in GitHub Desktop.
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
/**
* Count number of word in file in ascending order
* To run this program provide full path of file that has all words
* update AVOID_WORD variable for words you dont want to include
* @author yogeshupadhyay
*
*/
public class WordCount {
public static void main(String[] args) throws FileNotFoundException {
// open the file
Scanner console = new Scanner(System.in);
System.out.print("What is the name of the text file? ");
String fileName = console.nextLine();
Scanner input = new Scanner(new File(fileName));
final String[] AVOID_WORDS = new String[]{"is", "third"};
// count occurrences
Map<String, Integer> wordCounts = new HashMap<String, Integer>();
while (input.hasNext()) {
String next = input.next().toLowerCase();
if(Arrays.asList(AVOID_WORDS).contains(next)) continue;
if (!wordCounts.containsKey(next)) {
wordCounts.put(next, 1);
} else {
wordCounts.put(next, wordCounts.get(next) + 1);
}
}
// get cutoff and report frequencies
System.out.println("Total words = " + wordCounts.size());
int min = 1;
Map<String, Integer> sortedMap = sortMapByValue(wordCounts);
//System.out.println(sortedMap);
//wordCounts.entrySet().stream().sorted(Entry.comparingByValue()).collect(Collectors.toMap(Entry::getKey, Entry::getValue, (e1,e2) -> e1, TreeMap::new));
for(Map.Entry<String, Integer> eachEntry: sortedMap.entrySet()) {
System.out.println(eachEntry.getKey() + " " + eachEntry.getValue());
}
}
public static TreeMap<String, Integer> sortMapByValue(Map<String, Integer> map){
Comparator<String> comparator = new ValueComparator(new HashMap<>(map));
//TreeMap is a map sorted by its keys.
//The comparator is used to sort the TreeMap by keys.
TreeMap<String, Integer> result = new TreeMap<String, Integer>(comparator);
result.putAll(map);
return result;
}
}
//a comparator that compares Strings
class ValueComparator implements Comparator<String>{
HashMap<String, Integer> map = new HashMap<String, Integer>();
public ValueComparator(HashMap<String, Integer> map){
this.map.putAll(map);
}
@Override
public int compare(String s1, String s2) {
if(map.get(s1) >= map.get(s2)){
return -1;
}else{
return 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment