alv*_*vas 40 python corpus nltk word-cloud gensim
从创建R中的语料库中的单词子集,应答者可以轻松地将term-document matrix词汇转换为词云.
python库是否有一个类似的功能,它将原始文本文件或NLTK语料库或GensimMmcorpus带入词云?
结果看起来有点像这样:

Hea*_*ail 14
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
stopwords = set(STOPWORDS)
def show_wordcloud(data, title = None):
wordcloud = WordCloud(
background_color='white',
stopwords=stopwords,
max_words=200,
max_font_size=40,
scale=3,
random_state=1 # chosen at random by flipping a coin; it was heads
).generate(str(data))
fig = plt.figure(1, figsize=(12, 12))
plt.axis('off')
if title:
fig.suptitle(title, fontsize=20)
fig.subplots_adjust(top=2.3)
plt.imshow(wordcloud)
plt.show()
show_wordcloud(Samsung_Reviews_Negative['Reviews'])
show_wordcloud(Samsung_Reviews_positive['Reviews'])
Run Code Online (Sandbox Code Playgroud)
amueller的代码在行动中的示例
在命令行/终端:
sudo pip install wordcloud
Run Code Online (Sandbox Code Playgroud)
然后运行python脚本:
## Simple WordCloud
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS
text = 'all your base are belong to us all of your base base base'
def generate_wordcloud(text): # optionally add: stopwords=STOPWORDS and change the arg below
wordcloud = WordCloud(font_path='/Library/Fonts/Verdana.ttf',
relative_scaling = 1.0,
stopwords = {'to', 'of'} # set or space-separated string
).generate(text)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()
generate_wordcloud(text)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79851 次 |
| 最近记录: |