鍍金池/ 問答/Python  網(wǎng)絡(luò)安全/ wordcloud 詞云 關(guān)鍵字重復(fù)多次

wordcloud 詞云 關(guān)鍵字重復(fù)多次

我通過wordcloud來制作詞云,過程順利只是最后生成的圖片出現(xiàn)了多次關(guān)鍵字重復(fù):
圖片描述

關(guān)鍵的代碼如下:

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

如何解決呢?

回答
編輯回答
舊時光

與collocations參數(shù)有關(guān),默認collocations=True,會統(tǒng)計搭配詞。比如你的text是“我在拜訪客戶”,當(dāng)collocations為True時,就會把“拜訪客戶”也當(dāng)作一個詞進行統(tǒng)計,所以會出現(xiàn)重復(fù)。

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

2017年6月5日 00:12
編輯回答
愛是癌

用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) 
2018年8月20日 18:02
編輯回答
維她命

用unique去掉重復(fù),在輸出

2017年4月23日 03:22