Repository navigation
Expand file tree
/
Copy pathWriter.java
More file actions
139 lines (120 loc) · 5.11 KB
/
Copy pathWriter.java
File metadata and controls
139 lines (120 loc) · 5.11 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.*;
import java.util.*;
// Handles final data aggregation and file writing (Thread 0 only).
public class Writer {
public static void finalizeAndWriteFiles() throws IOException {
// 1. Process Categories
Collections.sort(SharedData.globalCatPairs);
writeGroupedData(SharedData.globalCatPairs);
// 2. Process Languages
Collections.sort(SharedData.globalLangPairs);
writeGroupedData(SharedData.globalLangPairs);
// 3. Process Keywords (Aggregate counts)
Collections.sort(SharedData.globalKeywords);
List<ResultInfo> kwCounts = new LinkedList<>();
if (!SharedData.globalKeywords.isEmpty()) {
Iterator<String> it = SharedData.globalKeywords.iterator();
String curr = it.next();
int count = 1;
while (it.hasNext()) {
String w = it.next();
if (w.equals(curr)) {
count++;
} else {
kwCounts.add(new ResultInfo(curr, count));
curr = w;
count = 1;
}
}
kwCounts.add(new ResultInfo(curr, count));
}
Collections.sort(kwCounts); // Sort by count desc
try (BufferedWriter bw = new BufferedWriter(new FileWriter("keywords_count.txt"))) {
for (ResultInfo kc : kwCounts) {
bw.write(kc.key + " " + kc.count);
bw.newLine();
}
}
// 4. Write global list and reports
writeAllArticles();
generateAndSaveReports(kwCounts);
}
private static void generateAndSaveReports(List<ResultInfo> keywordStats) throws IOException {
List<String> reportLines = new LinkedList<>();
reportLines.add("duplicates_found - " + SharedData.duplicatesFound);
reportLines.add("unique_articles - " + SharedData.validArticles.size());
reportLines.add("best_author - " + getAuthorStats());
reportLines.add("top_language - " + getLanguageStats());
reportLines.add("top_category - " + getCategoryStats());
reportLines.add("most_recent_article - " + getRecentArticleStats());
reportLines.add("top_keyword_en - " + getKeywordStats(keywordStats));
try (BufferedWriter writer = new BufferedWriter(new FileWriter("reports.txt"))) {
for (String line : reportLines) {
writer.write(line);
writer.newLine();
}
}
}
private static String getAuthorStats() {
List<String> extraction = new LinkedList<>();
for (Article a : SharedData.validArticles) {
if (a.author != null) extraction.add(a.author);
}
ResultInfo info = Utils.getTopEntity(extraction);
return info.key + " " + info.count;
}
private static String getLanguageStats() {
List<String> extraction = new LinkedList<>();
for (Article a : SharedData.validArticles) {
if (a.language != null) extraction.add(a.language);
}
ResultInfo info = Utils.getTopEntity(extraction);
return info.key + " " + info.count;
}
private static String getCategoryStats() {
List<String> cats = Utils.collectAllCategories();
ResultInfo info = Utils.getTopEntity(cats);
return info.key + " " + info.count;
}
private static String getRecentArticleStats() {
if (SharedData.validArticles.isEmpty()) return "- -";
return SharedData.validArticles.get(0).getFormattedDateAndUrl();
}
private static String getKeywordStats(List<ResultInfo> stats) {
if (stats == null || stats.isEmpty()) return "- 0";
ResultInfo top = stats.get(0);
return top.key + " " + top.count;
}
// Writes sorted data to separate files based on the key.
private static void writeGroupedData(List<ResultInfo> pairs) throws IOException {
if (pairs.isEmpty()) return;
Iterator<ResultInfo> it = pairs.iterator();
ResultInfo first = it.next();
String currentKey = first.key;
List<String> currentBuffer = new LinkedList<>();
currentBuffer.add(first.value);
while (it.hasNext()) {
ResultInfo p = it.next();
if (!p.key.equals(currentKey)) {
Utils.writeListToFile(currentKey + ".txt", currentBuffer);
currentKey = p.key;
currentBuffer.clear();
currentBuffer.add(p.value);
} else {
String lastVal = ((LinkedList<String>)currentBuffer).getLast();
if (!p.value.equals(lastVal)) currentBuffer.add(p.value);
}
}
Utils.writeListToFile(currentKey + ".txt", currentBuffer);
}
private static void writeAllArticles() throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("all_articles.txt"))) {
for (Article a : SharedData.validArticles) {
if(a.uuid != null && a.published != null) {
bw.write(a.uuid + " " + a.published);
bw.newLine();
}
}
}
}
}