鱼C论坛

 找回密码
 立即注册
查看: 3155|回复: 0

[技术交流] 文本词频统计2

[复制链接]
发表于 2020-11-11 17:58:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

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

  2. filename = "threekingdoms.txt"
  3. txt = open(filename, "r", encoding="utf-8").read()
  4. words = jieba.lcut(txt)
  5. counts = {}
  6. for word in words:
  7.     if len(word)==1:
  8.         continue
  9.     else:
  10.         counts[word] = counts.get(word, 0) + 1
  11. items = list(counts.items())
  12. items.sort(key=lambda x:x[1],reverse=True)
  13. # 将出现次数最多的前十五个人物名输出
  14. for i in range(15):
  15.     word, count = items[i]
  16.     print("{0:<10}{1:>5}".format(word, count))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-29 12:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表