创建 python 词云时如何对多词术语进行分组?

Jua*_*opo 3 python text visualization

我正在尝试使用 python 从一系列成分中创建一个词云,其中一些成分的名称中包含多个单词。我希望词云将这些名称视为单个元素,但我不知道如何实现这一点。例如:

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()
Run Code Online (Sandbox Code Playgroud)

生成的词云如下。但是,例如,我希望术语“cabernet sauvignon”仅显示为一个单词。

https://i.stack.imgur.com/yuHns.png

tob*_*s_k 5

dict在表单中创建{phrase: count, ...}并使用generate_from_frequencies

d = dict(zip(ingredients, frequencies))
wordcloud = w.WordCloud(collocations=False).generate_from_frequencies(d)
Run Code Online (Sandbox Code Playgroud)