词云制作(中文分词+英文)

#英文文本制作词云图

#pip install wordcloud

filename="XX.txt"    # 将目标文本文件放入这里   

with open(filename,encoding="utf8")as f:

        mytext=f.read()

mytext    #查看该变量中存储的文本


from wordcloud import WordCloud

wordcloud = WordCloud().generate(mytext)


import matplotlib.pyplot as plt

plt.imshow(wordcloud,interpolation='bilinear')

plt.axis("off")    #将成图的周围的刻度隐藏

#每次成图的内容不一定一致,但是大体上该出现的都会予以显示

#中文文本制作词云图(中文分词+中文词云制作)
#pip install jieba
filename = "XX-chinese.txt"    #这里放入你的目标中文文本文件
with open(filename,encoding="utf-8")as f:
    mytext = f.read()

print(mytext)    #不同于英文文本,中文文本需要用到print()显示

import jieba
mytext=" ".join(jieba.cut(mytext))    #中文文本分词

print(mytext)    #分词后显示查看

from wordcloud import WordCloud
wordcloud = WordCloud(font_path="simsun.ttf").generate(mytext) #由于词云包缺少中文字体,所以这里要指定汉字字体便于显示
import matplotlib.pyplot as plt
plt.imshow(wordcloud,interpolation='bilinear')
plt.axis("off")

好的,这是一个比较复杂的问题,需要梳理出多个步骤来完成。 1. 爬取京东评论:可以使用 Python 的 requests 库和 BeautifulSoup 库来实现。首先需要通过 requests 库获取目标网页的 HTML 内容,然后使用 BeautifulSoup 库解析 HTML 内容,提取出评论信息。 2. 分词+词频统计:可以使用 Python 的 jieba 库来进行中文分词,将评论分词后,使用 Python 的 collections 库中的 Counter 类来进行词频统计。 3. 词云图展示:可以使用 Python 的 wordcloud 库来生成词云图。在生成词云图之前,需要先将分词后的数据转换成字符串形式,然后根据词频大小来设置词云图中不同词的大小。 下面是一个简单的代码示例,供参考: ``` import requests from bs4 import BeautifulSoup import jieba from collections import Counter from wordcloud import WordCloud import matplotlib.pyplot as plt # 爬取京东评论 url = 'https://item.jd.com/100003235876.html#comment' html = requests.get(url).text soup = BeautifulSoup(html, 'html.parser') comments = soup.find_all('div', {'class': 'comment-con'}) # 分词+词频统计 words_list = [] for comment in comments: words = jieba.cut(comment.text) words_list.extend(words) word_count = Counter(words_list) # 词云图展示 wordcloud = WordCloud(width=800, height=800, background_color='white') wordcloud.generate_from_frequencies(frequencies=word_count) plt.figure() plt.imshow(wordcloud, interpolation="bilinear") plt.axis("off") plt.show() ``` 这段代码可以爬取京东商品页面的评论,并生成对应的词云图展示。需要注意的是,这只是一个简单的示例代码,实际应用中需要考虑更多的细节问题,比如异常处理、数据清洗、图形美化等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值