博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
中文词频统计
阅读量:6908 次
发布时间:2019-06-27

本文共 1880 字,大约阅读时间需要 6 分钟。

作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2773

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

 来自红楼梦的一小章内容:

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

text=open('123.txt','r',encoding='utf-8').read()

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

pip install jieba

import jieba

jieba.lcut(text)

 

import jiebawordsls=jieba.lcut(text)

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

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

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

参考词库下载地址:https://pinyin.sogou.com/dict/

转换代码:scel_to_text

词库:

worddict1=[line.strip() for line in open('23.txt',encoding='utf-8').readlines()]jieba.load_userdict(worddict1)

5. 生成词频统计

wcdict={}for word in wordsls:    if word not in worddict2:(7.排除语法型)      if len(word)==1:        continue      else:        wcdict[word]=wcdict.get(word,0)+1

6. 排序

wcls=list(wcdict.items())wcls.sort(key=lambda  x:x[1],reverse=True) 

7. 排除语法型词汇,代词、冠词、连词等停用词。

文件:

 

stops

worddict2=[line.strip() for line in open('stops_chinese.txt',encoding='utf-8').readlines()]

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

import pandas as pdpd.DataFrame(data=word).to_csv('E:/1234.csv',encoding='utf-8')

9. 生成词云。

wl_split=" ".join(wordsls) from wordcloud import WordCloudimport matplotlib.pyplot as pltmywc = WordCloud().generate(wl_split)plt.imshow(mywc)plt.axis("off")plt.show()

10.最总代码总和和截图:

import jiebatext=open('D://123.txt','r',encoding='utf-8').read()worddict1=open('D://23.txt','r',encoding='utf-8').read()worddict2=open('D://stops_chinese.txt','r',encoding='utf-8').read()wordsls=jieba.lcut(text)wcdict={}for word in wordsls:    if word not in worddict2:      if len(word)==1:        continue      else:        wcdict[word]=wcdict.get(word,0)+1wcls=list(wcdict.items())wcls.sort(key=lambda  x:x[1],reverse=True)for i in range(25):    print(wcls[i])wl_split=" ".join(wordsls) from wordcloud import WordCloudimport matplotlib.pyplot as pltmywc = WordCloud().generate(wl_split)plt.imshow(mywc)plt.axis("off")plt.show()

 

转载于:https://www.cnblogs.com/fulanjiang/p/10550961.html

你可能感兴趣的文章
python docopt模块详解
查看>>
第二阶段冲刺-05
查看>>
ConcurrentHashMap(转)
查看>>
数据库
查看>>
Python自学笔记-time模块(转)
查看>>
z-index
查看>>
Oracle表空间满处理方式
查看>>
js时间戳转化为日期格式,日期格式转化为时间戳
查看>>
android起源
查看>>
linux 标准输出和后台运行
查看>>
this
查看>>
nginx.conf
查看>>
python 基础复习 09 之基础函数
查看>>
jQuery返回顶层与返回底部
查看>>
/Date(1410019200000+0800)/如何转换为date对象
查看>>
1.1 Python 安装
查看>>
英特尔2011年IDF四月在京召开
查看>>
基于数组的无锁队列(译)
查看>>
【7】解决:移动端点击a链接出现蓝色边框
查看>>
自动化测试和手工测试
查看>>