Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2015 21:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b5c2b77545157a89d0b9 to your computer and use it in GitHub Desktop.
Save anonymous/b5c2b77545157a89d0b9 to your computer and use it in GitHub Desktop.
package textstat;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.toList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.function.ToIntFunction;
public class TextStat {
private final HashMap<String, Integer> histogram = new HashMap<>();
public void add(String word) {
histogram.merge(word, 1, (a, b) -> (a == null? 0: a) + b);
}
public int totalWords() {
return histogram.values().stream().mapToInt(Integer::intValue).sum();
}
public int uniqueWords() {
return histogram.size();
}
public List<String> mostUsed() {
return mostUsed(1);
}
public List<String> mostUsed(int num) {
Comparator<Entry<String, Integer>> cmp = comparingInt(Entry::getValue);
return histogram.entrySet().stream().sorted(cmp.reversed()).map(Entry::getKey).limit(num).collect(toList());
}
public int averageWordLength() {
ToIntFunction<Entry<String, Integer>> weight = e -> e.getKey().length() * e.getValue();
return (int)histogram.entrySet().stream().mapToInt(weight).average().orElseThrow(ArithmeticException::new);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment