-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordCountUtil.java
174 lines (158 loc) · 6.05 KB
/
WordCountUtil.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package app.util;
import app.pojo.EsPage;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.util.*;
import java.util.function.BinaryOperator;
/**
* 词频统计工具类
*
* @author faith.huan 2019-07-21 10:40
*/
@Slf4j
public class WordCountUtil {
/**
* 统计词频
*
* @param baseDir 文件所在路径
* @param resultFileName 统计结果文件名
*/
public static void wordCount(String baseDir, String resultFileName) {
try {
TreeMap<String, Integer> resultMap = new TreeMap<>();
List<String> subFiles = getSubFiles(new File(baseDir));
for (String subFile : subFiles) {
Map<String, Integer> map = count(subFile);
addToTreeMap(resultMap, map);
}
saveToFile(resultMap, resultFileName);
} catch (Exception e) {
log.error("统计词频发生异常", e);
}
}
/**
* 统计单个文件词频
*
* @param fileName 文件名
*/
private static Map<String, Integer> count(String fileName) {
try {
String string = IOUtils.toString(new FileInputStream(fileName));
EsPage esPage = JSON.parseObject(string, EsPage.class);
log.info("file:{},esPage:{}", fileName, esPage.getTitle());
if (StringUtils.isAnyBlank(esPage.getContent(), esPage.getTitle())) {
return Collections.emptyMap();
}
String content = esPage.getContent() + " " + esPage.getTitle();
content = content.replaceAll("[@`;“”®\":.?!()$<%*#}{\\[\\]\\d]+", "").toLowerCase();
Optional<HashMap<String, Integer>> reduce = Arrays.stream(content.split(" "))
.flatMap(s -> Arrays.stream(s.split("[’'/…—_+\n\\-]+")))
.filter(s -> StringUtils.isNotBlank(s) && !StringUtils.containsAny(s, ",") && s.length() > 4)
.map(s -> {
HashMap<String, Integer> map = new HashMap<String, Integer>(4) {{
put(s, 1);
}};
return map;
}).reduce(new BinaryOperator<HashMap<String, Integer>>() {
@Override
public HashMap<String, Integer> apply(HashMap<String, Integer> map1, HashMap<String, Integer> map2) {
map2.keySet().forEach(key -> {
if (map1.containsKey(key)) {
map1.put(key, map1.get(key) + map2.get(key));
} else {
map1.put(key, map2.get(key));
}
});
return map1;
}
});
if (reduce.isPresent()) {
return reduce.get();
} else {
return Collections.emptyMap();
}
} catch (IOException e) {
log.error("统计文件{}词频发生异常", fileName, e);
return Collections.emptyMap();
}
}
/**
* 将统计结果写入文件
* 会写两个文件
* ${resultFileName}SortedByKey.txt:存储按字母排序的结果
* ${resultFileName}SortedByValue.txt:存储按词频逆序排序的结果
*
* @param treeMap 统计结果
* @param resultFileName 文件名
*/
private static void saveToFile(TreeMap<String, Integer> treeMap, String resultFileName) {
try {
StringBuilder stringBuilder = new StringBuilder();
treeMap.forEach((key, val) -> {
stringBuilder.append(key).append(":")
.append(val).append("\n");
});
IOUtils.write(stringBuilder.toString(), new FileOutputStream(resultFileName + "SortedByKey.txt"));
// 排序
List<Map.Entry<String, Integer>> resultList = sort(treeMap);
StringBuilder stringBuilderSorted = new StringBuilder();
resultList.forEach(e -> {
stringBuilderSorted.append(e.getKey()).append(":")
.append(e.getValue()).append("\n");
});
IOUtils.write(stringBuilderSorted.toString(), new FileOutputStream(resultFileName + "SortedByValue.txt"));
} catch (IOException e) {
System.out.println("写入发生异常");
}
}
/**
* 按词频逆序排列统计结果
*/
private static List<Map.Entry<String, Integer>> sort(Map<String, Integer> resultMap) {
List<Map.Entry<String, Integer>> list = new ArrayList<>(resultMap.entrySet());
list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
return list;
}
/**
* 将单个文件结果汇总到合计中
*
* @param resultMap 合计map
* @param map 单个文件map
*/
private static void addToTreeMap(Map<String, Integer> resultMap, Map<String, Integer> map) {
map.keySet().forEach(key -> {
if (resultMap.containsKey(key)) {
resultMap.put(key, resultMap.get(key) + map.get(key));
} else {
resultMap.put(key, map.get(key));
}
});
}
/**
* 取子文件列表
*
* @param f 父文件
* @return 子文件列表
*/
private static List<String> getSubFiles(File f) {
List<String> subFiles = new ArrayList<>();
if (f.isDirectory()) {
File[] fs = f.listFiles();
if (fs != null) {
for (File file : fs) {
if (file.isDirectory()) {
subFiles.addAll(getSubFiles(file));
} else {
subFiles.add(file.getAbsolutePath());
}
}
}
} else {
subFiles.add(f.getAbsolutePath());
}
return subFiles;
}
}