def create_cloud(self):
# Return Bing search snippets
text = self.return_txt()
# Get mask image from Bing
image_mask = np.array(self.return_img())
# potential feature
stopwords = set(STOPWORDS)
# stopwords.add(search_modifier)
wordcloud = WordCloud(background_color="white", mask=image_mask, stopwords=stopwords)
wordcloud.generate(text)
image_colors = ImageColorGenerator(image_mask)
plt.imshow(image_mask, cmap=plt.cm.gray, interpolation="None")
plt.imshow(wordcloud.recolor(color_func=image_colors), alpha=.8, interpolation='None')
plt.axis("off")
return plt
python类STOPWORDS的实例源码
def wcloud(text):
mask = np.array(Image.open("face_mask.png")) #choose mask
stopwords = set(STOPWORDS)
wc = WordCloud(background_color="white",
mask=mask,
max_words=80,
stopwords=stopwords,
width=800,
height=400,
mode="RGB",
relative_scaling=0.5,
)
text = clean_text(text)
wc.generate(text)
#save image
file_name = raw_input("Enter any name for the Word Cloud image:") +'.png'
wc.to_file(file_name)
return
def generateWordCloud(text):
# read the mask / color image
# taken from http://jirkavinse.deviantart.com/art/quot-Real-Life-quot-Alice-282261010
d = path.dirname(__file__)
cloud_coloring = np.array(Image.open(path.join(d, "us-mask-white.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wc = WordCloud(background_color="black", max_words=2000, mask=cloud_coloring,
stopwords=stopwords, max_font_size=40, random_state=42)
# generate word cloud
wc.generate(text)
# create coloring from image
image_colors = ImageColorGenerator(cloud_coloring)
# show
plt.imshow(wc)
plt.axis("off")
plt.show()
def generateTable(text, n=5):
# Start by getting a frequency dictionary
d = path.dirname(__file__)
cloud_coloring = np.array(Image.open(path.join(d, "us-mask-white.png")))
stopwords = set(STOPWORDS)
stopwords.add("said")
wc = WordCloud(background_color="black", max_words=2000, mask=cloud_coloring,
stopwords=stopwords, max_font_size=40, random_state=42)
frequenciesDict = wc.process_text(text)
words = frequenciesDict.keys()
freq = frequenciesDict.values()
frequencies = pd.DataFrame({ 'words' : words, 'frequencies' : freq })
frequencies.sort_values('frequencies', ascending = False, inplace = True)
print '\nTop 5 Terms\n'
print frequencies.head(n = n).to_string(index = False)
print '\n'
def title_word_cloud():
"""
???????
"""
text = ''
wc = WordCloud(background_color='white', # ??????
stopwords=STOPWORDS,
max_words=1000, # ?????????
font_path='C:/Python27/Lib/site-packages/matplotlib/mpl-data/fonts/ttf/simhei.ttf',
# ?????????????????
max_font_size=50, # ???????
random_state=30, # ??????????????????????
)
with open('rent_ave.csv') as csvfile:
reader = [each for each in csv.DictReader(csvfile)]
for row in reader:
text += row[u'title'] + ' '
print jieba_clear_text(text)
wc.generate(jieba_clear_text(text))
plt.imshow(wc)
plt.axis('off')
plt.show()
def drawWordCloud(word_text, filename):
mask = imread('hello.jpg')
my_wordcloud = WordCloud(
background_color='white', # ??????
mask=mask, # ??????
max_words=2000, # ?????????
stopwords=STOPWORDS, # ?????
font_path='/System/Library/Fonts/Hiragino Sans GB W6.ttc', # ?????????????????
max_font_size=50, # ???????
random_state=30, # ??????????????????????
scale=1
).generate(word_text)
image_colors = ImageColorGenerator(mask)
my_wordcloud.recolor(color_func=image_colors)
# ????????
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
# ????
my_wordcloud.to_file(filename=filename)
print()
def drawWordCloud(word_text, filename):
mask = imread('bike.jpg')
my_wordcloud = WordCloud(
background_color='white', # ??????
mask=mask, # ??????
max_words=2000, # ?????????
stopwords=STOPWORDS, # ?????
font_path='/System/Library/Fonts/Hiragino Sans GB W6.ttc', # ?????????????????
max_font_size=50, # ???????
random_state=30, # ??????????????????????
scale=1.3
).generate(word_text)
image_colors = ImageColorGenerator(mask)
my_wordcloud.recolor(color_func=image_colors)
# ????????
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
# ????
my_wordcloud.to_file(filename=filename)
print()