我就廢話不多說了,大家還是直接看代碼吧~
#! python3
# -*- coding: utf-8 -*-
import os, codecs
import jieba
from collections import Counter
def get_words(txt):
seg_list = jieba.cut(txt)
c = Counter()
for x in seg_list:
if len(x)>1 and x != '\r\n':
c[x] += 1
print('常用詞頻度統(tǒng)計(jì)結(jié)果')
for (k,v) in c.most_common(100):
print('%s%s %s %d' % (' '*(5-len(k)), k, '*'*int(v/3), v))
if __name__ == '__main__':
with codecs.open('19d.txt', 'r', 'utf8') as f:
txt = f.read()
get_words(txt)
樣本:十九大報(bào)告全文
常用詞頻度統(tǒng)計(jì)結(jié)果
發(fā)展 ********************************************************************** 212
中國 ******************************************************** 168
人民 **************************************************** 157
建設(shè) ************************************************* 148
社會主義 ************************************************ 146
堅(jiān)持 ******************************************* 130
國家 ****************************** 90
全面 ***************************** 88
制度 *************************** 83
實(shí)現(xiàn) *************************** 83
推進(jìn) *************************** 81
政治 ************************** 80
社會 ************************** 80
特色 ************************** 79
加強(qiáng) *********************** 71
體系 ********************** 68
文化 ********************** 66
我們 ********************* 64
時代 ********************* 63
必須 ******************** 61
經(jīng)濟(jì) ******************* 59
偉大 ******************* 58
完善 ***************** 51
我國 **************** 50
推動 *************** 47
現(xiàn)代化 *************** 47
安全 *************** 46
更加 ************** 44
民主 ************** 44
補(bǔ)充:jieba讀取txt文檔并進(jìn)行分詞、詞頻統(tǒng)計(jì),輸出詞云圖
代碼實(shí)現(xiàn)
# 庫的引用
import jieba
import matplotlib as mpl
import matplotlib.pyplot as plt
from wordcloud import WordCloud
#定義一個空字符串
final = ""
#文件夾位置
filename = r"D:\python\pra\推薦系統(tǒng)1-500.txt"
#打開文件夾,讀取內(nèi)容,并進(jìn)行分詞
with open(filename,'r',encoding = 'utf-8') as f:
for line in f.readlines():
word = jieba.cut(line)
for i in word:
final = final + i +" "
運(yùn)行結(jié)果
![](/d/20211017/7e84bdd974b6bdb61db2c4fd3173489c.gif)
# 圖云打印
word_pic = WordCloud(font_path = r'C:\Windows\Fonts\simkai.ttf',width = 2000,height = 1000).generate(final)
plt.imshow(word_pic)
#去掉坐標(biāo)軸
plt.axis('off')
#保存圖片到相應(yīng)文件夾
plt.savefig(r'D:\python\pra\6.png')
圖云輸出圖
![](/d/20211017/460611a96f784aff8003bb9c83d29714.gif)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- Python 統(tǒng)計(jì)數(shù)據(jù)集標(biāo)簽的類別及數(shù)目操作
- Python統(tǒng)計(jì)可散列的對象之容器Counter詳解
- Python 統(tǒng)計(jì)列表中重復(fù)元素的個數(shù)并返回其索引值的實(shí)現(xiàn)方法
- Python實(shí)戰(zhàn)之單詞打卡統(tǒng)計(jì)
- python之cur.fetchall與cur.fetchone提取數(shù)據(jù)并統(tǒng)計(jì)處理操作
- python自動統(tǒng)計(jì)zabbix系統(tǒng)監(jiān)控覆蓋率的示例代碼
- python 統(tǒng)計(jì)代碼耗時的幾種方法分享
- Python統(tǒng)計(jì)列表元素出現(xiàn)次數(shù)的方法示例
- python統(tǒng)計(jì)RGB圖片某像素的個數(shù)案例
- 利用Python3實(shí)現(xiàn)統(tǒng)計(jì)大量單詞中各字母出現(xiàn)的次數(shù)和頻率的方法
- 使用Python 統(tǒng)計(jì)文件夾內(nèi)所有pdf頁數(shù)的小工具
- python 統(tǒng)計(jì)list中各個元素出現(xiàn)的次數(shù)的幾種方法
- python調(diào)用百度AI接口實(shí)現(xiàn)人流量統(tǒng)計(jì)
- Python代碼覆蓋率統(tǒng)計(jì)工具coverage.py用法詳解
- python 爬蟲基本使用——統(tǒng)計(jì)杭電oj題目正確率并排序
- 利用python匯總統(tǒng)計(jì)多張Excel
- python統(tǒng)計(jì)mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼
- 用python實(shí)現(xiàn)監(jiān)控視頻人數(shù)統(tǒng)計(jì)