创建 python 词云时如何对多词术语进行分组?
How can I group multi-word terms when creating a python wordcloud?
我正在尝试使用 python 从成分列表中创建词云,其中一些成分的名称中有多个单词。我希望 wordcloud 将这些名称视为单个元素,但我不知道如何实现。例如:
import wordcloud as w
import numpy as np
import matplotlib.pyplot as plt
ingredients = ['cabernet sauvignon', 'apple', 'black pepper',
'rice', 'smoked salmon',
'dried tomato', 'butter', 'mushroom', 'goat cheese']
frequencies = [55, 83, 33, 42, 19, 23, 5, 69, 1]
# Wordcloud asks for a string, and I have tried separating the terms with ',' and '~'
text = ''
for i, word in enumerate(ingredients):
text = text + frequencies[i] * (word + ',')
wordcloud = w.WordCloud(collocations = False).generate(text)
plt.imshow(wordcloud, interpolation = 'bilinear')
plt.axis("off")
plt.show()
生成的词云如下。但是,例如,我希望术语 "cabernet sauvignon" 只显示为一个词。
以 {phrase: count, ...}
的形式创建一个 dict
并使用 generate_from_frequencies
:
d = dict(zip(ingredients, frequencies))
wordcloud = w.WordCloud(collocations=False).generate_from_frequencies(d)
我正在尝试使用 python 从成分列表中创建词云,其中一些成分的名称中有多个单词。我希望 wordcloud 将这些名称视为单个元素,但我不知道如何实现。例如:
import wordcloud as w
import numpy as np
import matplotlib.pyplot as plt
ingredients = ['cabernet sauvignon', 'apple', 'black pepper',
'rice', 'smoked salmon',
'dried tomato', 'butter', 'mushroom', 'goat cheese']
frequencies = [55, 83, 33, 42, 19, 23, 5, 69, 1]
# Wordcloud asks for a string, and I have tried separating the terms with ',' and '~'
text = ''
for i, word in enumerate(ingredients):
text = text + frequencies[i] * (word + ',')
wordcloud = w.WordCloud(collocations = False).generate(text)
plt.imshow(wordcloud, interpolation = 'bilinear')
plt.axis("off")
plt.show()
生成的词云如下。但是,例如,我希望术语 "cabernet sauvignon" 只显示为一个词。
以 {phrase: count, ...}
的形式创建一个 dict
并使用 generate_from_frequencies
:
d = dict(zip(ingredients, frequencies))
wordcloud = w.WordCloud(collocations=False).generate_from_frequencies(d)