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

中文词频统计

时间:2019-03-18 19:52:31      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:dict   article   \n   port   for   top   src   分词   下载   

中文词频统计

1. 下载一长篇中文小说。

2. 从文件读取待分析文本。

3. 安装并使用jieba进行中文分词。

pip install jieba

import jieba

ljieba.lcut(text)

4. 更新词库,加入所分析对象的专业词汇。

jieba.add_word(‘天罡北斗阵‘)  #逐个添加

jieba.load_userdict(word_dict)  #词库文本文件

5. 生成词频统计

6. 排序

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

8. 输出词频最大TOP20,把结果存放到文件里

9. 生成词云。

import jieba

article = open(test.txt,r).read()

jieba.add_word(北灵院)

words = list(jieba.lcut(article))
dictory = {}
dele = {\n, ,,,,‘‘,?,,,,
        ,,,,,,,,,
        ,,,,,,,,,
        ,,,,,}

set = set(words)-dele

for w in set:
        dictory[w] = words.count(w)

word = sorted(dictory.items(),key = lambda x:x[1], reverse = True)

for i in range(20):
    print(word[i])

 

技术图片

中文词频统计

标签:dict   article   \n   port   for   top   src   分词   下载   

原文地址:https://www.cnblogs.com/huangjianke123/p/10551095.html

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