码迷,mamicode.com
首页 > 其他好文 > 详细

中文词频统计

时间:2018-03-28 01:40:05      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:port   读文件   div   出现   [1]   http   range   coding   col   

题目:

下载一长篇中文文章。

从文件读取待分析文本。

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

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

pip install jieba

import jieba

list(jieba.lcut(news))

生成词频统计

排序

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

输出词频最大TOP20

 

 

#coding=utf-8
import jieba

f=open(story.txt,r,encoding=utf-8)    #打开文件
text=f.read()      #读文件
wordlist=list(jieba.lcut(text))  #分解

Words={}
for i in set(wordlist):    #计算次数
    Words[i]=wordlist.count(i)

delete_word={, ,,我们, , 他们, 我的, 他的, 你的, , , ,,,,,,,,?,,,           ,,,,,,,,,,,,,, \n,(,),,,,便,,,,,,那里,           ,一个,,,,}

for i in delete_word:        #删除出现的过渡词
    if i in Words:
        del Words[i]

sort_word = sorted(Words.items(), key= lambda d:d[1], reverse = True)  # 排序
for i in range(20):
    print(sort_word[i])

 

截图:

技术分享图片

 

中文词频统计

标签:port   读文件   div   出现   [1]   http   range   coding   col   

原文地址:https://www.cnblogs.com/tiankongyiluozhiwu/p/8661060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!