Created
October 11, 2018 15:25
-
-
Save pluto-atom-4/22996cbeeb4c5eb52a3bdc20219c5711 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 pramp.string; | |
import java.util.*; | |
public class WordCountEngine { | |
static String[][] count(String document) { | |
Map<String, Integer> map = new HashMap<>(); | |
String[] tokens = document.toLowerCase().replaceAll("[^a-z ]", "").split("\\s+"); | |
List<String> list = new ArrayList<>(); | |
for (String word : tokens) { | |
if (map.containsKey(word)) { | |
map.put(word, map.get(word) + 1); | |
} else { | |
map.put(word, 1); | |
list.add(word); | |
} | |
} | |
Map<Integer, ArrayList<String>> reducedMap = new TreeMap<>(new Comparator<Integer>() { | |
@Override | |
public int compare(Integer o1, Integer o2) { | |
return o1.compareTo(o2) * -1; | |
} | |
}); | |
for (String word : list) { | |
if (!reducedMap.containsKey(map.get(word))) { | |
reducedMap.put(map.get(word), new ArrayList<>()); | |
} | |
reducedMap.get(map.get(word)).add(word); | |
} | |
String[][] toBeReturned = new String[list.size()][2]; | |
int j = 0; | |
for (Integer i : reducedMap.keySet()) { | |
for (String word : reducedMap.get(i)) { | |
toBeReturned[j++] = new String[]{word, String.valueOf(i)}; | |
} | |
} | |
return toBeReturned; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment