Created
November 22, 2012 17:27
-
-
Save dimo414/4132270 to your computer and use it in GitHub Desktop.
Concise word frequency script in Java: http://hopsage.blogspot.com/2012/11/evidence-that-java-should-be-avoided-in.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
import com.google.common.collect.*; | |
public class WordFreq { | |
public static void main(String[] args) throws FileNotFoundException { | |
final HashMultiset<String> counts = HashMultiset.create( | |
ImmutableList.copyOf(new Scanner(new File(args[0])))); | |
List<String> words = Lists.newArrayList(counts.elementSet()); | |
Collections.sort(words, new Comparator<String>() { | |
public int compare(String l, String r) { | |
return counts.count(r) - counts.count(l); | |
}}); | |
for(String word : words.subList(0, Integer.parseInt(args[1]))){ | |
System.out.println(word + " : " + counts.count(word)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment