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. 随机数据构造-Faker

    from faker import Faker # fake = Faker() #本地化处理 fake = Faker('zh_CN') # print(fake.name()) # print(f ...

  2. 【420】链表实现Quack

    quack.h // quack.h: an interface definition for a queue/stack #include <stdio.h> #include < ...

  3. 123457123457#0#-----com.twoapp.FromPuzzle02--前拼后广--儿童农场拼图游戏jiemei

    com.twoapp.FromPuzzle02--前拼后广--儿童农场拼图游戏jiemei

  4. java springmvc 前端 跨域问题

    有个朋友在写扇贝插件的时候遇到了跨域问题.于是我对解决跨域问题的方式进行了一番探讨. 问题 API:查询单词URL: https://api.shanbay.com/bdc/search/?word= ...

  5. 完全解读 margin 标签

    你真的了解margin吗?你知道margin有什么特性吗?你知道什么是垂直外边距合并?margin在块元素.内联元素中的区别?什么时候该用 padding而不是margin?你知道负margin吗?你 ...

  6. antd ——按钮

    <ButtonGroup> <Button type="primary" htmlType="submit" onClick={this.ha ...

  7. Docker 镜像的内部结构(四)

    目录 一.base镜像 1.rootfs 2.base 镜像提供的是最小安装的 Linux 发行版. 3.支持运行多种 Linux OS 二.镜像的分层结构 可写的容器层 一.base镜像 base ...

  8. Centos7安装gitlab11 学习笔记之基础概念、部署安装、权限管理、issue管理

    一.基础介绍 1.简介 一个基于GIT的源码托管解决方案 基于rubyonrails开发 集成了nginx postgreSQL redis sidekiq等组件 2.安装要求 2g内存以上,有点占内 ...

  9. PO,VO,DAO,BO,POJO之间的区别与解释

    VO value object:值对象 通常用于业务层之间的数据传递,由new创建,由GC回收. PO persistant object:持久层对象 对应数据库中表的字段. VO和PO,都是属性加上 ...

  10. C++Primer 5th Chap3 Strings,Vectors, and Arrays

    使用名字空间成员的简单方法: using namespace ::name;例如:using std::cin; 头文件不应包含using声明 标准库类型string:(需要带有头文件#include ...