2017-12-11 27 views
1
import pandas as pd 
import matplotlib.pyplot as plt 
from PIL import Image 
import numpy as np 
import wordcloud 
from wordcloud import WordCloud,STOPWORDS 

# Read the whole text. 
remarks = open(r'C:\Users\marmar\Documents\Remarks.txt').read() 

#Create words over an image 
mask = np.array(Image.open(r'C:\users\marmar\Documents\cloud.png')) 

#set the stopwords list 
stopwords= set(STOPWORDS) 

#append new words to the stopwords list 
new_words =open(r'C:\Users\marmar\comments.txt').read() 
new_stopwords=stopwords.union(new_words) 

#generate the word cloud with parameters 
wc = WordCloud(background_color="white", 
       max_words=2000, 
       mask=mask, 
       min_font_size =12, 
       max_font_size=20, 
       relative_scaling = 0.5, 
       stopwords=new_stopwords, 
       normalize_plurals= True) 
wc.generate(remarks) 
plt.figure(figsize=(25,25)) 
plt.imshow(wc, interpolation="bilinear") 
plt.axis("off") 

#Show the wordcloud 
plt.show() 

基本的に、Python 3(Jupyter Notebook)を使用して、実際のクラウド画像でワードクラウドを作成しています。 WordCloudパッケージには実際には独自のストップワード機能があります。しかし、ストップワードリストに私の雲の中に見たくない言葉を入れたい。 このテキストファイルにいくつかの単語を含めることを試みましたが、私の雲の中の言葉を見ることができます。 たとえば、テキストファイルは次のようになります。 カスタマー、CSRカスタマー、満足、アイテム完了ワードクラッドから単語を削除するにはどうすればよいですか? (Python 3)

リストに単語を追加するにはどうすればいいですか?私はこれらの機能の両方を追加して追加しようとしましたが、機能しません。

ありがとうございます。

+0

私はstopwords.add( 'CSR Comment')を試しましたが、私はまだクラウドで見ることができました! – marmar

+1

'WordCloud'コンストラクタへの呼び出しで、' stopwords = stopwords'を渡すように見えます。 'stopwords = new_stopwords'を使いたくないですか? – RagingRoosevelt

+0

また、すべてが単語ごとに分割されるようにファイルをトークン化してください。 'open(...)。read()。split()'のようなものを使うことができます。 – RagingRoosevelt

答えて

0

ああ!私はテキストファイルで自分の言葉をカンマで区切っていたからです。

ワードクラウドを構築する人にとっては、単語をスペースで区切って入力するだけです。句読点の必要はありません。 @RagingRooseveltは "分割"機能を使用したときに正しいものでした。

関連する問題