代码拉取完成,页面将自动刷新
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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。