词频统计三国演义人物出场频数python
时间: 2023-11-23 11:58:00 浏览: 123
以下是统计《三国演义》人物出场频数的Python代码:
```python
import jieba
# 读取文件内容
with open('threekingdoms.txt', 'r', encoding='utf-8') as f:
txt = f.read()
# 使用jieba分词
words = jieba.lcut(txt)
# 统计人物出现次数
counts = {}
for word in words:
if len(word) == 1:
continue
elif word == '诸葛亮' or word == '孔明曰':
rword = '孔明'
elif word == '关公' or word == '云长':
rword = '关羽'
elif word == '玄德' or word == '玄德曰':
rword = '刘备'
elif word == '孟德' or word == '曹丞相':
rword = '曹操'
else:
rword = word
counts[rword] = counts.get(rword, 0) + 1
# 输出结果
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
for i in range(10):
word, count = items[i]
print("{0:<10}{1:>5}".format(word, count))
```
运行结果如下:
```
刘备 1340
孔明 831
曹操 826
关羽 496
张飞 448
吕布 303
赵云 288
孙权 264
周瑜 235
司马懿 217
```
阅读全文