中文词频

下载一长篇中文文章。

从文件读取待分析文本。

news = open('gzccnews.txt','r',encoding = 'utf-8')

安装与使用jieba进行中文分词。

pip install jieba

import jieba

list(jieba.lcut(news))

生成词频统计

排序

排除语法型词汇,代词、冠词、连词

输出词频最大TOP20

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import jieba
f=open('C:/Users/user/Desktop/122.txt','r',encoding='gbk')
s=f.read()
f.close()
print(s)
s1=list(jieba.lcut(s))
 
miss={',','。',' ',':','“','”','','','','','','\n'}
s2={}
for b in s1:
    s2[b]=s2.get(b,0)+1
 
for a in miss:
    if a in s2:
        del s2[a]
 
s3=sorted(s2.items(),key=lambda x:x[1],reverse=True)
for c in range(20):
    print(s3[c])

  

 

posted @   142黄浩峰  阅读(120)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示