-
-
Save anonymous/b5c2b77545157a89d0b9 to your computer and use it in GitHub Desktop.
This file contains 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
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