python生成词云,要求频率越小生成的字词越大,老师的要求,请各位大佬解答,感谢,急急急!!!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python生成词云,要求频率越小生成的字词越大,老师的要求,请各位大佬解答,感谢,急急急!!!相关的知识,希望对你有一定的参考价值。
import jieba
file = open('article_title', 'r', encoding='utf-8')
duanzi = file.read()
file.close()
sep = '''-/.。""'',!?;:~`·[] \ ,:;“”?!-、【】‘’'''
exclude = ' ','\ue412','\x01','我','了','的','你','来','我们','被','……','…'
for char in sep:
duanzi = duanzi.replace(char,'')
duanziList = list(jieba.cut(duanzi))#分词
duanziDict =
duanziciyun =
duanzis = list(set(duanziList)-exclude)#删除非中国汉语字符
for d in range(0,len(duanzis)):
duanziDict[duanzis[d]] = duanzi.count(str(duanzis[d]))
dictList = list(duanziDict.items())
dictList.sort(key=lambda x:x[1],reverse=False)
f = open('count.txt','a',encoding='utf-8')
for i in range(0, len(dictList)):
print(dictList[i])
f.write(dictList[i][0] + ':' + str(dictList[i][1]) + '\n')
duanziciyun[dictList[i][0]] = dictList[i][1]
f.close()
# 生成词云
from PIL import Image, ImageSequence
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
font = r'zhongwen.ttf'
image = Image.open('3.jpg')
graph = np.array(image)
wc = WordCloud(font_path=font, background_color='White', max_words=5000, mask=graph)
wc.generate_from_frequencies(duanziciyun)
image_color = ImageColorGenerator(graph)
plt.imshow(wc)
plt.axis("off")
plt.show()
wc.to_file(r'new.png')
以上是关于python生成词云,要求频率越小生成的字词越大,老师的要求,请各位大佬解答,感谢,急急急!!!的主要内容,如果未能解决你的问题,请参考以下文章