水浒传词频统计python123
时间: 2023-12-21 22:31:25 浏览: 379
基于python的三国演义词频分析
```python
import jieba
from collections import Counter
import matplotlib.pyplot as plt
from wordcloud import WordCloud
# 读取《水浒传》文本
with open('shuihuzhuan.txt', 'r', encoding='utf-8') as file:
text = file.read()
# 使用jieba分词
words = jieba.lcut(text)
# 读取停用词库
with open('stopwords.txt', 'r', encoding='utf-8') as file:
stopwords = file.read().splitlines()
# 进行词频统计
word_count = Counter([word for word in words if word not in stopwords])
# 生成词云
wordcloud = WordCloud(font_path="simhei.ttf").generate_from_frequencies(word_count)
# 可视化词云
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
```
阅读全文