1. 词频统计:

 import jieba
txt = open("threekingdoms3.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(15):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))

结果是:

曹操 946
孔明 737
将军 622
玄德 585
却说 534
关公 509
荆州 413
二人 410
丞相 405
玄德曰 390
不可 387
孔明曰 374
张飞 358
如此 320
不能 318

进一步改进, 我想只知道人物出场统计,代码如下:

 import jieba
txt = open("threekingdoms3.txt", "r", encoding='utf-8').read()
names = {'曹操','孔明','刘备','关羽','张飞','吕布','赵云','孙权','周瑜','袁绍','黄忠','魏延'}
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
# for word in excludes:
# del counts[word]
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(40):
word, count = items[i]
if word in names:
print ("{0:<10}{1:>5}".format(word, count))

运行结果为:

曹操 1358
孔明 1265
刘备 1251
关羽 783
张飞 358
吕布 300
赵云 278
孙权 257
周瑜 217
袁绍 191

进一步的做词云图:

 import jieba
import os
import wordcloud def getText(file):
with open(file, 'r', encoding= 'UTF-8') as txt:
txt = txt.read()
jieba.lcut(txt)
return txt directoryname = os.getcwd()
filename = input()
txt = getText(filename + '.txt')
wordclouds = wordcloud.WordCloud(width=1000, height= 800, margin=2).generate(txt)
wordclouds.to_file('{}.png'.format(filename)) os.system('{}.png'.format(filename))

名称是可以进一步优化的,参见第二部分代码。

中文wordcloud库默认会出现乱码,解决方法参考 https://blog.csdn.net/Dick633/article/details/80261233

参考:https://blog.csdn.net/weixin_44521703/article/details/93058003

Python 中文文件统计词频 + 中文词云的更多相关文章

  1. R语言统计词频 画词云

    原始数据: 程序: #统计词频 library(wordcloud) # F:/master2017/ch4/weibo170.cut.txt text <- readLines("F ...

  2. 根据词频生成词云(Python wordcloud实现)

    网上大多数词云的代码都是基于原始文本生成,这里写一个根据词频生成词云的小例子,都是基于现成的函数. 另外有个在线制作词云的网站也很不错,推荐使用:WordArt 安装词云与画图包 pip3 insta ...

  3. python编写文件统计脚本

    python编写文件统计脚本 思路:用os模块中的一些函数(os.listdir().os.path.isdir().os.path.join().os.path.abspath()等) 实现功能:显 ...

  4. 利用python实现简单词频统计、构建词云

    1.利用jieba分词,排除停用词stopword之后,对文章中的词进行词频统计,并用matplotlib进行直方图展示 # coding: utf-8 import codecs import ma ...

  5. python 基于 wordcloud + jieba + matplotlib 生成词云

    词云 词云是啥?词云突出一个数据可视化,酷炫.以前以为很复杂,不想python已经有成熟的工具来做词云.而我们要做的就是准备关键词数据,挑一款字体,挑一张模板图片,非常非常无脑.准备好了吗,快跟我一起 ...

  6. python学习笔记(11)--词云

    中分词库  jieba 词云 wordcloud import jieba import wordcloud f = open("新时代中国特色社会主义.txt", "r ...

  7. Python脚本文件中使用中文

    Python做图形用户界面(GUI)开发时经常要在界面上显示中文,需要做如下处理(详见[1]和[2]2.3节): 在py文件的首行写上:# -- coding:utf-8 -- 保存py文件时要存为u ...

  8. python jieba 库分词结合Wordcloud词云统计

    import jieba jieba.add_word("福军") jieba.add_word("少安") excludes={"一个", ...

  9. Python词云的中文问题

    image= Image.open('F:/__identity/course/建模/九寨沟地震/四川地图.jpg') fig = plt.figure(figsize=(20, 16)) graph ...

随机推荐

  1. Qt编写自定义控件50-迷你仪表盘

    一.前言 这个控件取名叫迷你仪表盘,是以为该控件可以缩小到很小很小的区域显示,非常适合小面积区域展示仪表数据使用,还可以手动触摸调节进度,是我个人觉得最漂亮小巧的一个控件.初次看到类似的控件是在一个音 ...

  2. 华为OpenStack开源团队人才招募中

    职位要求: 1. 三年以上软件开发经验,编程技能良好. 2. 熟练使用Python.Java.Go或其他语言开发. 3. 有OpenStack经验或者存储经验优先考虑. 4. 良好的学习和沟通能力,责 ...

  3. 微信小程序企业付款到个人

    <?php /** * 小程序之企业付款到个人! */ class WxPayModel extends Model { public function sendMoneyToPerson($t ...

  4. Java中使用队列Queue

    示例代码: Queue<Integer> queue = new LinkedList<Integer>(); for (int i = 1; i <= 100; i + ...

  5. [转]Postgres-XL 10r1英文文档

    Postgres-XL 是一个完全满足ACID的.开源的.可方便进行水平扩展的.多租户安全的.基于PostgreSQL的数据库解决方案. Postgres-XL 可非常灵活的应用在各类场景中,比如: ...

  6. sonar:windows重启sonar

    登录后操作

  7. pycharm操作Django基础部分

    原文地址:https://www.cnblogs.com/feixuelove1009/p/5823135.html

  8. linux 环境下jmeter+ant+jenkins

    一.linux下的jenkins的安装: 下载链接:https://pan.baidu.com/s/1qZItZOC 密码:58da Jenkins 下载网址: http://jenkins-ci.o ...

  9. 日常工作问题解决:rhel7下配置多路径设备

    目录 1.情景描述 2.安装多路径软件 2.1 安装多路径 2.2检查安装情况 2.3 重启系统 2.4 将多路径软件添加至内核模块 2.5 检查内核添加情况 2.6 启动multipath服务 2. ...

  10. Jmeter进行接口流程测试

    Jmeter进行简单的流程测试 此次完成的流程:添加文章-删除文章 总的如下: 一.测试计划 用户定义的变量中,我定义了3个变量: 二.HTTP cookie管理器 填写要添加的cookie 三.JD ...