美文网首页
用jieba提词,pyecharts的WordCloud绘制词云

用jieba提词,pyecharts的WordCloud绘制词云

作者: _armour | 来源:发表于2018-07-31 11:35 被阅读0次
引入3个库
pip3 install collections
pip3 install pyecharts
pip3 install jieba
生成的文件
image.png
效果图
image.png
完整代码
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)

相关文章

网友评论

      本文标题:用jieba提词,pyecharts的WordCloud绘制词云

      本文链接:https://www.haomeiwen.com/subject/tebcvftx.html