-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTopPhrases.java
More file actions
58 lines (51 loc) · 1.85 KB
/
TopPhrases.java
File metadata and controls
58 lines (51 loc) · 1.85 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
50
51
52
53
54
55
56
57
58
package com.los;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class TopPhrases {
private PhrasesComparator phrasesComparator = new PhrasesComparator();
public Map<String, Long> countTopPhrases(String file) {
try (Stream<String> stream = Files.lines(Paths.get(file)).parallel()) {
return stream
.flatMap(line -> Stream.of(line.split("\\|")))
.map(String::trim)
.map(String::toLowerCase)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet()
.parallelStream()
.sorted(phrasesComparator::compare)
.limit(100000)
.collect(Collectors.toMap(
Entry::getKey,
Entry::getValue,
(v1, v2) -> v1,
LinkedHashMap::new)
);
} catch (IOException e) {
throw new FailFastException(e);
}
}
}
class PhrasesComparator implements Comparator, Serializable {
@Override
public int compare(Object o1, Object o2) {
Entry<String, Long> e1 = (Entry<String, Long>) o1;
Entry<String, Long> e2 = (Entry<String, Long>) o2;
if (e1.getValue().equals(e2.getValue())) {
return e1.getKey().compareTo(e2.getKey());
} else return -e1.getValue().compareTo(e2.getValue());
}
}
class FailFastException extends RuntimeException {
FailFastException(Exception e) {
super(e);
}
}