wordcloud 词云 关键字重复多次

新手上路,请多包涵

我通过wordcloud来制作词云,过程顺利只是最后生成的图片出现了多次关键字重复:
图片描述

关键的代码如下:

wcd=WordCloud(font_path='simsun.ttc',width=900,height=400,background_color='white',max_words=100,scale=1.5).generate(text)

其中text是一堆词语,print出来的一部分为:

clipboard.png

如何解决呢?

阅读 25.9k
3 个回答
新手上路,请多包涵

与collocations参数有关,默认collocations=True,会统计搭配词。比如你的text是“我在拜访客户”,当collocations为True时,就会把“拜访客户”也当作一个词进行统计,所以会出现重复。

wcd=WordCloud(font_path='simsun.ttc', collocations=False,width=900,height=400,background_color='white',max_words=100,scale=1.5).generate(text)

用unique去掉重复,在输出

新手上路,请多包涵

用jieba.analyse分出词频,然后用generate_from_frequencies(keywords)

示例如下:

result=jieba.analyse.textrank(text_from_file_with_apath,topK=300,withWeight=True)
keywords = dict()
for i in result:
    keywords[i[0]]=i[1]


wc = WordCloud(stopwords=stopwords, font_path=font, width = 1600, height = 900, margin=10, max_font_size = 360, min_font_size = 36, background_color="black", mask=mask).generate_from_frequencies(keywords) 
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题