开放题词云爬取

zhihu、xinlang
pnhekgfuf 2 years ago
parent d09be084e2
commit c69c862d32

24
part1

@ -0,0 +1,24 @@
from wordcloud import WordCloud, STOPWORDS
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba
# 打开文件
text = open("2.txt", encoding="utf-8").read()
# 中文分词
text = ' '.join(jieba.cut(text))
print(text[:100])
# 生成对象
mask = np.array(Image.open("meng.jpg")) # 使用蒙版图片
wc = WordCloud(mask=mask,
font_path="STKAITI.TTF",
mode="RGBA",
background_color=None,
stopwords=STOPWORDS.add("的")
).generate(text)
# 显示词云
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
#保存文件
wc.to_file("3.wordcloud3.png")
Loading…
Cancel
Save