中文词频统计


下载一长篇中文文章。

从文件读取待分析文本。

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

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

pip install jieba

import jieba

list(jieba.lcut(news))

生成词频统计

排序

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

输出词频最大TOP20

复制代码
 1 # -*- coding: UTF-8 -*-
 2 # -*- author: yjw -*-
 3 import jieba
 4 
 5 
 6 fo=open('1.txt','r',encoding = 'utf-8')
 7 
 8 text=fo.read()
 9 text2=list(jieba.lcut(text))
10 
11 sign={'',' ','','我们', '', '他们', '我的', '他的', '你的', '', '', '','','','','','','','','?','','',
12            '','','','','','','','','','','','','','', '
','(',')','','','','便','','','','','','那里',
13            '','一个','','','','','她','都','道','好','还','’','‘','呢','来','得','你们','才','们'}
14 
15 text3={}
16 for i in text2:
17     text3[i]=text3.get(i,0)+1
18 
19 for i in sign:
20     if i in text3:
21         del text3[i]
22 
23 text4 =sorted(text3.items(),key=lambda x:x[1],reverse=True)
24 for i in range(20):
25     print(text4[i])


原文地址:https://www.cnblogs.com/verson/p/8663520.html