-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetWords.java
59 lines (52 loc) · 2.39 KB
/
GetWords.java
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
59
import java.io.*;
import java.util.*;
public class GetWords {
public static HashMap<String, Integer> getWords(File file) {
try {
ArrayList<String> data = ReadFile.readFile(file);
HashMap<String, Integer> wordCounter = new HashMap<>();
for (String line : data) {
String[] step = line.split(">");
String[] allData = step[1].trim().split(" ");
for (String word : allData) {
wordCounter.merge(word, 1, Integer::sum);
}
}
return wordCounter;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void run() throws IOException, NullPointerException {
long startTime = System.nanoTime();
File[] files = LoadFiles.loadFiles(Constants.rawYear());
HashMap<String, Integer> words = new HashMap<>();
for (File file : files) {
Map<String, Integer> fileData = getWords(file);
assert fileData != null;
fileData.forEach((word, count) -> words.merge(word, count, Integer::sum));
}
ArrayList<String> outMessages = new ArrayList<>();
LinkedHashMap<String, Integer> sortedWordCounter = new LinkedHashMap<>();
words.entrySet().stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.forEachOrdered(x -> sortedWordCounter.put(x.getKey(), x.getValue()));
int uniqueWords = sortedWordCounter.size();
int totalWords = sortedWordCounter.values().stream().mapToInt(Integer::intValue).sum();
outMessages.add(String.format("%d unique words with %d words total\n", uniqueWords, totalWords));
int i = 1;
for (Map.Entry<String, Integer> entry : sortedWordCounter.entrySet()) {
int wordCount = entry.getValue();
String word = entry.getKey();
if (word.isBlank()) {
word = "<message deleted> (timeout by moderator)";
}
outMessages.add(String.format("%d: %d uses of %s", i++, wordCount, word));
}
File outfile = new File(Constants.pathFolder() + "words.log");
WriteFile.writeFile(outfile, outMessages);
double finalTime = (double) (System.nanoTime() - startTime) / 1_000_000_000;
System.out.printf(("[+] Log generated (%fs)"), finalTime);
}
}