基于这个问题如何从Python的语料库中创建单词云?,我做了一个字云,使用阿穆勒氏库.然而,我不知道如何用更多的文本集来满足云的需求。以下是我到目前为止尝试过的:
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我找不到库的任何手册,所以我不知道该如何进行,是吗?:)
实际上,正如您在这里看到的:Python中以不同类型的数组为值的字典,我有这样的数据结构:
category = { "World news": [2, "foo bla content of", "content of 2nd article"],
"Politics": [1, "only 1 article here"],
...
}我想在世界云"foo bla内容“和”第二篇文章的内容“后面加上。
发布于 2016-01-19 22:23:51
最简单的解决方案是使用更新的语料库重新生成wordcloud。
要用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([" ".join(value[1:]) for value in category.values()])可以翻译为:
首先,将每个键的所有元素粘合在一起,除了第一个(因为它是一个计数器)。然后将所有产生的字符串粘合在一起。
发布于 2016-01-19 21:57:35
简单地浏览一下云/blob/主/cloud云/WordCloud.py中的类,就没有一个update方法,所以您需要重新生成wordcloud或添加一个update方法。
最简单的方法可能是维护原始源文本,并将其添加到末尾,然后重新生成。
https://stackoverflow.com/questions/34887551
复制相似问题