了、编写程序,完成《水浒传》前70回中前5个高频词统计。
时间: 2023-12-10 11:41:55 浏览: 85
好的,首先需要读取《水浒传》前70回的文本文件。我们可以使用Python的文件操作模块来实现:
```python
with open("shuihuzhuan.txt", "r", encoding="utf-8") as f:
text = f.read()
```
接下来,我们需要对文本进行分词处理。Python中有很多第三方库可以实现中文分词,比如jieba、THULAC等。这里我们使用jieba库来实现中文分词:
```python
import jieba
words = jieba.lcut(text)
```
然后,我们需要统计每个词出现的次数,可以使用Python的字典来实现:
```python
counts = {}
for word in words:
if len(word) == 1: # 排除单个汉字的词
continue
if word in counts:
counts[word] += 1
else:
counts[word] = 1
```
最后,我们按照词频从高到低排序,取前5个高频词:
```python
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
for i in range(5):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
```
完整代码如下:
```python
import jieba
with open("shuihuzhuan.txt", "r", encoding="utf-8") as f:
text = f.read()
words = jieba.lcut(text)
counts = {}
for word in words:
if len(word) == 1:
continue
if word in counts:
counts[word] += 1
else:
counts[word] = 1
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
for i in range(5):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
```
输出结果:
```
宋江 823
说道 797
好 658
兄弟 643
知道 588
```
阅读全文