文本挖掘HW3

import os
import os.path
import codecs
import pandas as pd
import numpy as np

filePaths = []
fileContents=[]
a=os.walk("C:/Users/dell/Desktop/datamining/2.1+语料库/2.1/SogouC.mini/Sample")
for root, dirs, files in a:
    for name in files:
        filePath=os.path.join(root,name)
        filePaths.append(filePath)
        f = codecs.open(filePath, 'r','utf-8')
        fileContent=f.read()
        f.close()
        fileContents.append(fileContent)

corpos = pd.DataFrame({'filePath': filePaths,'fileContent':fileContents})

corpos

segments=[]
filePaths =[]
for index, row in corpos.iterrows():
    filePath = row['filePath']
    fileContent = row['fileContent']
    segs = jieba.cut(fileContent)
    for seg in segs:
        segments.append(seg)
        filePaths.append(filePath)
segmentDataFrame = pd.DataFrame({'segment':segments,'filePath':filePaths})
segmentDataFrame
corpos.iterrows
segStat = segmentDataFrame.groupby(by='segment')["segment"].agg({"计数":np.size}).reset_index().sort_values('计数',ascending=False)

segmentDataFrame

我们发现存在jieba切分后有一些停用词在干扰,类似空格、标点以及一些中文中的介词助词等等。
所以,此时我们需要导入一个停用词库,停用词库中的词就不要放入切词数组中。

在这里需要注意的是,DataFrame没有sort这个属性,查了下,DataFrame有sort_values,
具体用法就是.sort_values('列名', ascending=False)

stopwords = pd.read_csv("C:\\Users\\dell\\Desktop\\datamining\\2.3\\StopwordsCN.txt",encoding='utf-8',index_col=False)

fSegStat = segStat[~segStat.segment.isin(stopwords.stopword)]

fSegStat

segments=[]
filePaths =[]
for index, row in corpos.iterrows():
    filePath = row['filePath']
    fileContent = row['fileContent']
    segs = jieba.cut(fileContent)
    for seg in segs:
        if seg not in stopwords.stopword.values and len(seg.strip())>1:
            segments.append(seg)
            filePaths.append(filePath)
segmentDataFrame = pd.DataFrame({'segment':segments,'filePath':filePaths})

词云图

地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#wordcloud
pip install wordcloud-1.4.1-cp36-cp36m-win_amd64.whl

segStat=segmentDataFrame.groupby(by='segment')['segment'].agg({'计数':np.size}).reset_index().sort_values('计数',ascending=False)
fSegStat = segStat[~segStat.segment.isin(stopwords.stopword)]
from wordcloud import WordCloud
import matplotlib.pyplot as plt
wordcloud=WordCloud(font_path='C:\\Users\\Data Engineer\\Desktop\\xx\\2.4 词云绘制\\2.4\\simhei.ttf',background_color='black')
words = fSegStat.set_index('segment').to_dict()
wordcloud.fit_words(words['计数'])
plt.imshow(wordcloud)
plt.show()

网上找了一篇有关燃料电池发展状况的分析报告:将文本转化为txt。

f = codecs.open('C:\\Users\\Data Engineer\\Desktop\\xx\\2.4 词云绘制\\2.4\\fuelcell.txt', 'r', 'gbk')

txt=f.read()

txtcontent=jieba.cut(txt)

contents=[]

for content in txtcontent:
    if content not in stopwords.stopword.values and len(content.strip())>1:
        contents.append(content)

contentDataFrame=pd.DataFrame({'content':contents})

contentStat=contentDataFrame.groupby(by='content')['content'].agg({'计数':np.size}).reset_index().sort_values('计数',ascending=False)

wordcloud=WordCloud(font_path='C:\\Users\\Data Engineer\\Desktop\\xx\\2.4 词云绘制\\2.4\\simhei.ttf',background_color='black')
words = contentStat.set_index('content').to_dict()
wordcloud.fit_words(words['计数'])
plt.imshow(wordcloud)
plt.show()
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 词频统计 - 语料库的构建 遍历路径下的所有文件和子目录 os.walk() 读取目录及其所有子目录 root —...
    WesleyLien阅读 1,959评论 0赞 3
  • 0.前言 上次查看了微信好友的位置信息,想了想,还是不过瘾,于是就琢磨起了把微信好友的个性签名拿到,然后分词,接着...
    伪君子_阅读 1,617评论 0赞 3
  • 一:前言 上次把斗鱼弹幕数据抓取搞定后,我就拿来试试用词云分析看看效果,简单学习一下。这是弹幕抓拍去分析的对象是斗...
    梅花鹿数据阅读 3,155评论 3赞 19
  • 前几天生成词云图,安装wordcloud包,结果遇到各种问题,折腾好久,终于安装好了,总结下。 常规方法:pip ...
    喻婷_5e77阅读 2,984评论 1赞 0
  • 本周为大家带来炫酷好玩的 wordcloud 词云构造库。使用 wordcloud 可以做出这样的图片: 还可以做...
    zx576阅读 4,892评论 2赞 8

友情链接更多精彩内容