Ai
4 Star 0 Fork 0

陈岳/MyGit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
2.py 2.19 KB
一键复制 编辑 原始数据 按行查看 历史
陈岳 提交于 2020-04-05 16:16 +08:00 . .
import jieba
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
class HlmNameCount():
def showNameBar(self,name_list_sort,name_list_count):
x = np.arange(len(name_list_sort))
plt.rcParams['font.sans-serif'] = ['SimHei']
bars = plt.bar(x,name_list_count)
plt.xticks(x,name_list_sort)
i = 0
for bar in bars:
plt.text((bar.get_x() + bar.get_width() / 2), bar.get_height(), '%d' % name_list_count[i], ha='center', va='bottom')
i += 1
plt.show()
def showNamePie(self, name_list_sort, name_list_fracs):
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.pie(name_list_fracs, labels=name_list_sort, autopct='%1.2f%%', shadow=True)
plt.show()
def getNameTimesSort(self,name_list,txt_path):
for k in name_list:
jieba.add_word(k)
file_obj = open(txt_path, 'rb').read()
jieba_cut = jieba.cut(file_obj)
book_counter = Counter(jieba_cut)
name_dict ={}
name_total_count = 0
for k in name_list:
if k == '熙凤':
name_dict['凤姐'] += book_counter[k]
else:
name_dict[k] = book_counter[k]
name_total_count += book_counter[k]
name_counter = Counter(name_dict)
name_list_sort = []
name_list_fracs = []
name_list_count = []
for k,v in name_counter.most_common():
name_list_sort.append(k)
name_list_fracs.append(round(v/name_total_count,2)*100)
name_list_count.append(v)
self.showNameBar(name_list_sort, name_list_count)
self.showNamePie(name_list_sort,name_list_fracs)
if __name__ == '__main__':
name_list = ['宝玉', '黛玉', '宝钗', '元春', '探春', '湘云', '妙玉', '迎春', '惜春', '凤姐', '熙凤', '巧姐', '李纨', '可卿', '贾母', '贾珍', '贾蓉', '贾赦', '贾政', '王夫人', '贾琏', '薛蟠', '香菱', '宝琴', '袭人', '晴雯', '平儿', '紫鹃', '莺儿']
txt_path = 'D:/123.txt'
hnc = HlmNameCount()
hnc.getNameTimesSort(name_list,txt_path)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/chenyue666/MyGit.git
git@gitee.com:chenyue666/MyGit.git
chenyue666
MyGit
MyGit
master

搜索帮助