-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordCountEngine.java
More file actions
49 lines (42 loc) · 1.67 KB
/
Copy pathWordCountEngine.java
File metadata and controls
49 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package pramp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;
public final class WordCountEngine {
private static final Pattern PUNCTUATION = Pattern.compile("[^a-zA-Z ]");
private static final Pattern SPACES = Pattern.compile("\\s+");
public static final String[][] STRINGS = new String[0][];
private static String[][] wordCountEngine(String document) {
if (document.isEmpty()) {
return STRINGS;
}
final String[] words = SPACES.split(PUNCTUATION.matcher(document).replaceAll(""));
final Map<String, Integer> map = new LinkedHashMap<>();
final Map<Integer, List<String>> map2 = new HashMap<>();
final List<String[]> res = new ArrayList<>();
for (String word : words) {
map.merge(word.toLowerCase(), 1, Integer::sum);
}
for (Entry<String, Integer> e : map.entrySet()) {
map2.computeIfAbsent(e.getValue(), v -> new ArrayList<>()).add(e.getKey());
}
for (int max = map.size(); max >= 0; max--) {
for (String word : map2.getOrDefault(max, Collections.emptyList())) {
res.add(new String[] { word, String.valueOf(max) });
}
}
return res.toArray(STRINGS);
}
public static void main(String[] args) {
final String[][] res = wordCountEngine("practice makes perfect practice makes perfect yeah");
for (String[] r : res) {
System.out.println(Arrays.toString(r));
}
}
}