python利用nltk读取电子书,提取中文,输出高频词,绘制词汇离散图,绘制词云图

#读取在线电子书
from urllib.request import urlopen
url='https://www.gutenberg.org/cache/epub/25606/pg25606-images.html'
html=urlopen(url).read()
html=html.decode('utf-8')
from nltk.book import *
from nltk.corpus import PlaintextCorpusReader
import nltk
#提取中文
htmlls=[]
for i in html:
    if '\u4e00'<=i<='\uefa5':
        htmlls.append(i)
fdist=FreqDist(htmlls) 
print(fdist.most_common(5))#输出高频词
#绘制词汇离散图
import matplotlib as plt
plt.rcParams [ 'font.sans-serif']='SimHei'#使图的标注中文显示
nltk.draw.dispersion.dispersion_plot(html,['八月', '兵','人力', '天下', '今日'])


#读取本地电子书
with open("E:\围城.txt",'r') as f:
    txt=f.read()
#re提取中文,jieba分词
import jieba
import re
cleaned_txt=''.join(re.findall('[\u4e00-\u9fa5]',txt))
wordlist=jieba.lcut(cleaned_txt)
text=nltk.Text(wordlist)
fdist=FreqDist(txt)
words=['方鸿渐','赵新楣','孙柔嘉','苏文纨','唐晓芙']
nltk.draw.dispersion.dispersion_plot(text,words,title='词汇离散图')
#绘制词云图
from wordcloud import WordCloud
import matplotlib.pyplot as plt
fdist_dict=dict(FreqDist(text))
wc=WordCloud(font_path="simhei.ttf", background_color="white", width=800, height=600).fit_words(fdist_dict)
plt.imshow(wc)

如果字体报错可能是pillow的版本问题,尝试更新pillow包。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值