python生成词云很慢吗_python生成词云,要求频率越小生成的字词越大,老师的要求,请各位大佬解答,感谢,急急急!!!...

importjiebafile=open('article_title','r',encoding='utf-8')duanzi=file.read()file.close()sep='''-/.。""'',!?;:~`·[]\,:;“”?!-、}{【】‘’'''exclude={'','\ue412','\x...

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')

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值