从多组文本生成 WordCloud
Generate WordCloud from multiple sets of text
基于这个问题 How to create a word cloud from a corpus in Python?, I a did build a word cloud, using amueller's 库。但是,我看不出如何为云提供多个文本集。到目前为止,这是我尝试过的方法:
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
stopwords=STOPWORDS.add("said"))
wc.generate(set_of_words)
wc.generate("foo") # this overwrites the previous line of code
# but I would like this to be appended to the set of words
我找不到图书馆的任何手册,所以我不知道如何进行,你呢? :)
实际上,正如你在这里看到的:,我有这样的数据结构:
category = { "World news": [2, "foo bla content of", "content of 2nd article"],
"Politics": [1, "only 1 article here"],
...
}
我想附加到世界云"foo bla content of"和"content of 2nd article"。
从 https://github.com/amueller/word_cloud/blob/master/wordcloud/wordcloud.py 中的 class 的简要浏览来看,没有更新方法,因此您需要重新生成词云或添加更新方法。
最简单的方法可能是保留原始源文本,并添加到此末尾,然后重新生成。
最简单的解决方案是使用更新后的语料库重新生成词云。
要使用 category
数据结构(针对所有主题)中包含的文本构建语料库,您可以使用以下理解:
# Update the corpus
corpus = " ".join([" ".join(value[1:]) for value in category.values()])
# Regenerate the word cloud
wc.generate(corpus)
为数据结构中的单个键构建词云(例如政治):
# Update the corpus
corpus = " ".join(category["Politics"][1:])
# Regenerate the word cloud
wc.generate(corpus)
解释:
- join 将多个字符串粘合在一起,由给定的分隔符分隔
- [1:] 从列表中取出除第一个元素之外的所有元素
- dict.values()给出字典中所有值的列表
表达式" ".join([" ".join(value[1:]) for value in category.values()])
因此可以翻译为:
首先将每个键的所有元素粘合在一起,除了第一个(因为它是一个计数器)。然后将所有生成的字符串粘合在一起。
基于这个问题 How to create a word cloud from a corpus in Python?, I a did build a word cloud, using amueller's 库。但是,我看不出如何为云提供多个文本集。到目前为止,这是我尝试过的方法:
wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
stopwords=STOPWORDS.add("said"))
wc.generate(set_of_words)
wc.generate("foo") # this overwrites the previous line of code
# but I would like this to be appended to the set of words
我找不到图书馆的任何手册,所以我不知道如何进行,你呢? :)
实际上,正如你在这里看到的:
category = { "World news": [2, "foo bla content of", "content of 2nd article"],
"Politics": [1, "only 1 article here"],
...
}
我想附加到世界云"foo bla content of"和"content of 2nd article"。
从 https://github.com/amueller/word_cloud/blob/master/wordcloud/wordcloud.py 中的 class 的简要浏览来看,没有更新方法,因此您需要重新生成词云或添加更新方法。
最简单的方法可能是保留原始源文本,并添加到此末尾,然后重新生成。
最简单的解决方案是使用更新后的语料库重新生成词云。
要使用 category
数据结构(针对所有主题)中包含的文本构建语料库,您可以使用以下理解:
# Update the corpus
corpus = " ".join([" ".join(value[1:]) for value in category.values()])
# Regenerate the word cloud
wc.generate(corpus)
为数据结构中的单个键构建词云(例如政治):
# Update the corpus
corpus = " ".join(category["Politics"][1:])
# Regenerate the word cloud
wc.generate(corpus)
解释:
- join 将多个字符串粘合在一起,由给定的分隔符分隔
- [1:] 从列表中取出除第一个元素之外的所有元素
- dict.values()给出字典中所有值的列表
表达式" ".join([" ".join(value[1:]) for value in category.values()])
因此可以翻译为:
首先将每个键的所有元素粘合在一起,除了第一个(因为它是一个计数器)。然后将所有生成的字符串粘合在一起。