gzj137070928 发表于 2020-11-11 17:58:24

文本词频统计2


# 文本词频统计2
# threekingdomsV1:找一个三国演义的文件,保存为threekingdoms.txt,放在同一目录里面
# 记录输出出现次数最多的前十五个人物名输出
import jieba

filename = "threekingdoms.txt"
txt = open(filename, "r", encoding="utf-8").read()
words = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word)==1:
      continue
    else:
      counts = counts.get(word, 0) + 1
items = list(counts.items())
items.sort(key=lambda x:x,reverse=True)
# 将出现次数最多的前十五个人物名输出
for i in range(15):
    word, count = items
    print("{0:<10}{1:>5}".format(word, count))
页: [1]
查看完整版本: 文本词频统计2