词频统计系统:通过输入输出流读取文本数据,利用集合框架实现对文本中汉语词语出现频率的统计,并输出出现频率最高的前 n 个词语。
时间: 2024-03-12 20:48:53 浏览: 19
好的,这是一个Java程序,可以通过输入输出流读取文本数据,利用集合框架实现对文本中汉语词语出现频率的统计,并输出出现频率最高的前 n 个词语。
```java
import java.io.*;
import java.util.*;
public class ChineseWordFrequency {
public static void main(String[] args) throws IOException {
// 读取文本数据
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt"), "UTF-8"));
// 使用HashMap统计词频
Map<String, Integer> wordFreq = new HashMap<>();
String line;
while ((line = reader.readLine()) != null) {
List<String> words = ChineseSegmentation.segment(line); // 中文分词
for (String word : words) {
if (wordFreq.containsKey(word)) {
wordFreq.put(word, wordFreq.get(word) + 1);
} else {
wordFreq.put(word, 1);
}
}
}
reader.close();
// 输出出现频率最高的前n个词语
int n = 10; // 前n个
PriorityQueue<Map.Entry<String, Integer>> maxHeap = new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
maxHeap.addAll(wordFreq.entrySet());
System.out.println("出现频率最高的前" + n + "个词语:");
for (int i = 0; i < n; i++) {
Map.Entry<String, Integer> entry = maxHeap.poll();
System.out.println(entry.getKey() + " 出现次数:" + entry.getValue());
}
}
// 中文分词工具类
public static class ChineseSegmentation {
private static final String DICTIONARY_PATH = "dict"; // 词典路径
private static final JiebaSegmenter SEGMENTER = new JiebaSegmenter();
static {
SEGMENTER.initUserDict(DICTIONARY_PATH); // 加载自定义词典
}
// 中文分词
public static List<String> segment(String text) {
List<String> words = new ArrayList<>();
List<SegToken> segTokens = SEGMENTER.process(text, JiebaSegmenter.SegMode.INDEX);
for (SegToken segToken : segTokens) {
String word = segToken.word.trim();
if (word.length() > 0) {
words.add(word);
}
}
return words;
}
}
}
```
这个程序首先使用`BufferedReader`读取文本数据,并使用`InputStreamReader`指定编码为UTF-8。然后,使用中文分词工具类`ChineseSegmentation`对文本进行分词,并统计每个词语的词频。最后,使用`PriorityQueue`维护出现频率最高的前n个词语,并输出它们的词频。
需要注意的是,这个程序使用了开源的中文分词工具Jieba,需要在项目中导入相关依赖。同时,需要使用自定义的词典来提高分词的准确性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)