Como criar uma nuvem de palavras a partir de um corpus em Python?
de Criar um subconjunto de palavras de um corpo em R, o respondedor pode facilmente converter um {[[0]} numa nuvem de palavras facilmente.
Existe uma função semelhante nas bibliotecas python que leva um ficheiro de texto em bruto ou NLTK
corpus ou Gensim
Mmcorpus para uma nuvem de palavras?
28
5 answers
Aqui está um post no blog que faz exatamente isso: http://peekaboo-vision.blogspot.com/2012/11/a-wordcloud-in-python.html
Todo o código está aqui: https://github.com/amueller/word_cloud
44
Author: Marcin, 2013-05-24 16:17:17
No caso de necessitar destas nuvens word para as Mostrar no sítio Web ou na aplicação web, poderá converter os seus dados para o formato json ou csv e carregá-los para uma biblioteca de visualização JavaScript, como d3. Nuvens de palavra em d3
Se não, a resposta de Marcin é uma boa maneira de fazer o que descreves. 10
Author: valentinos, 2013-05-28 19:26:52
Exemplo do código de amueller em ação
Na linha de comandos / terminal:
sudo pip install wordcloud
Execute então o programa em 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)
7
Author: MyopicVisage, 2018-05-16 23:16:50
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'])
3
Author: HeadAndTail, 2018-05-21 08:43:57
Aqui está o código curto
#make wordcoud
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()
if __name__ == '__main__':
show_wordcloud(text_str)
0
Author: Ujjawal107, 2018-03-24 13:18:10