引入3个库
pip3 install collections
pip3 install pyecharts
pip3 install jieba
生成的文件
效果图
完整代码
from collections import Counter
from pyecharts import WordCloud
import jieba.analyse
# 将counter拆分成两个list
def counter2list(counter):
keyList,valueList = [],[]
for c in counter:
keyList.append(c[0])
valueList.append(c[1])
return keyList,valueList
# 使用jieba提取关键词并计算权重
def extractTag(content,tagsList):
keyList,valueList = [],[]
if content:
tags = jieba.analyse.extract_tags(content, topK=100, withWeight=True)
for tex, widget in tags:
tagsList[tex] += int(widget*10000)
def drawWorldCloud(content,count):
outputFile = './测试词云.html'
cloud = WordCloud('词云图', width=1000, height=600, title_pos='center')
cloud.add(
' ',content,count,
shape='circle',
background_color='white',
max_words=200
)
cloud.render(outputFile)
if __name__ == '__main__':
c = Counter() #建一个容器
filePath = './新建文本文档.txt' #分析的文档路径
with open(filePath) as file_object:
contents = file_object.read()
extractTag(contents, c)
contentList,countList = counter2list(c.most_common(200))
drawWorldCloud(contentList, countList)