forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordFreqEx.java
More file actions
35 lines (27 loc) · 1.03 KB
/
WordFreqEx.java
File metadata and controls
35 lines (27 loc) · 1.03 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
package com.zetcode;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class WordFreqEx {
public static void main(String[] args) throws IOException {
var fileName = "src/resources/the-king-james-bible.txt";
var text = Files.readString(Path.of(fileName));
var regex = "[a-zA-Z']+";
var p = Pattern.compile(regex);
var matcher = p.matcher(text);
var words = matcher.results();
var res = words.collect(Collectors.groupingBy(MatchResult::group,
Collectors.counting()));
res.entrySet().stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.limit(10)
.forEach((e -> System.out.printf("%s %d%n", e.getKey(), e.getValue())));
}
}