本文整理汇总了Python中wordcloud.WordCloud类的典型用法代码示例。如果您正苦于以下问题:Python WordCloud类的具体用法?Python WordCloud怎么用?Python WordCloud使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WordCloud类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: genwordcloud
def genwordcloud(texts,mask=None,font_path=None,background_color='white'):
'''生成词云
parameter
----------
mask: RGBA模式数组,最后一个分量是alpha通道, 默认会生成一个900*1200的椭圆
font_path: 采用的字体,建议采用安卓默认字体DroidSansFallback.ttf
return
-------
img:可以直接img.save('test.png')
'''
from PIL import Image
try:
from wordcloud import WordCloud
except:
#raise Exception('wordcloud need install wordcloud package.')
print('wordcloud need install wordcloud package.')
return None
if mask is None:
tmp=np.zeros((900,1200),dtype=np.uint8)
for i in range(tmp.shape[0]):
for j in range(tmp.shape[1]):
if (i-449.5)**2/(430**2)+(j-599.5)**2/(580**2)>1:
tmp[i,j]=255
mask=np.zeros((900,1200,4),dtype=np.uint8)
mask[:,:,0]=tmp
mask[:,:,1]=tmp
mask[:,:,2]=tmp
mask[:,:,3]=255
else:
mask=np.array(Image.open(mask))
wordcloud = WordCloud(background_color = background_color,font_path=font_path, mask = mask)
wordcloud.generate(texts)
img=wordcloud.to_image()
return img
示例2: cloudplot
def cloudplot(person):
person = re.sub(r'\+', ' ', person)
text = GetTextRange(Emails, person)
text = rmBoring(rmNonAlpha(text)).decode('ascii', 'ignore')
plt.clf()
d = path.dirname(path.abspath(__file__))
hilcolor = np.array(Image.open(path.join(d, "static/img/hillarylogo.jpg")))
wc = WordCloud(background_color="white", max_words=150, mask=hilcolor,
stopwords=STOPWORDS.add("said"),
max_font_size=80, random_state=42,
relative_scaling = 0.5)
wc.generate(text)
image_colors = ImageColorGenerator(hilcolor)
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis("off")
fig = plt.gcf()
img = StringIO.StringIO()
fig.savefig(img)
img.seek(0)
return send_file(img, mimetype='image/png')
示例3: generate_image
def generate_image(words, image):
graph = np.array(image)
wc = WordCloud(font_path=os.path.join(CUR_DIR, 'fonts/simhei.ttf'),
background_color='white', max_words=MAX_WORDS, mask=graph)
wc.generate_from_frequencies(words)
image_color = ImageColorGenerator(graph)
return wc, image_color
示例4: twittersearch
def twittersearch():
api = twitter.Api(
consumer_key=twitter_consumer_key,
consumer_secret=twitter_consumer_secret,
access_token_key=twitter_access_token_key,
access_token_secret=twitter_access_token_secret
)
search = api.GetSearch(term='DevOps', lang='en', result_type='recent', count=100, max_id='')
item = 0
to_wordcloud = ''
to_display = '<html><body bgcolor="#0033FF"><font color="white"> <h2>Tweets about DevOps.... or <a href="'+my_url+'">click here to go back</a></h2>'
to_display += '<ol>'
for t in search:
item += 1
#to_display += str(item) + '. '
to_display += '<li>'
to_display += t.user.screen_name
to_display += ' ('
to_display += t.created_at
to_display += ') : '
to_display += t.text
to_wordcloud += t.text
to_display += '</li>'
# Generate a word cloud image
#wordcloud = WordCloud().generate(to_wordcloud)
wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(to_wordcloud)
wcimage = wordcloud.to_image()
random_name = 'static/'+str(random.randint(0,888888))+'-image-wc.png'
wcimage.save('./'+random_name)
to_display += '</ol></font>'
to_display += '<center><img src="/'+random_name+'" width="80%" height="80%"></center></body></html>'
return to_display
示例5: create_wc
def create_wc(words_in):
"""Create WordCloud object.
Parameters
----------
words_in : list of tuple
Words to plot, with their corresponding frequencies.
Returns
-------
wc : WordCloud() object
Wordcloud definition.
"""
# Create the WordCloud object
wc = WordCloud(background_color=None,
mode='RGBA',
width=800,
height=400,
prefer_horizontal=1,
relative_scaling=0.5,
min_font_size=25,
max_font_size=80).generate_from_frequencies(words_in)
# Change colour scheme to grey
wc.recolor(color_func=_grey_color_func, random_state=3)
return wc
示例6: run
def run():
f = open(u'words2.txt', 'r').read()
words = list(jieba.cut(f))
a = []
for w in words:
if len(w) > 1:
a.append(w)
text = r' '.join(a)
bg = np.array(Image.open('bg.jpg'))
wordcloud = WordCloud(
background_color = 'white',
#width = 1500,
#height = 960,
#margin = 10,
mask = bg,
font_path='C:/Windows/Fonts/simkai.ttf',
).generate(text)
image_colors=ImageColorGenerator(bg)
plt.imshow(wordcloud.recolor(color_func=image_colors))
plt.axis('off')
plt.show()
wordcloud.to_file('words_result3.png')
return
示例7: draw_wordCloud
def draw_wordCloud():
'''
画出词云图
:return:
'''
## 读取wordList,转化为str
global wordList
cut_text = ""
for word in wordList:
cut_text = cut_text + word + " "
## 生成词云
os.chdir(r"D:\STUDYING\MyProjects\pycharm\music163_EasonComments")
d = path.dirname(__file__) # 当前文件文件夹所在目录
color_mask = imread("Eason.jpg") # 读取背景图片
plt.imshow(color_mask)
cloud = WordCloud(
font_path=path.join(d, 'simsun.ttc'),
background_color='white',
mask=color_mask,
max_words=2000,
max_font_size=40,
)
word_cloud = cloud.generate(cut_text) # 产生词云
## show
plt.imshow(word_cloud, interpolation="bilinear")
plt.axis('off')
plt.show()
示例8: draw_tag_cloud
def draw_tag_cloud(users_tokens):
from PIL import Image
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
trump_coloring = np.array(Image.open("pics/trump.png"))
freqs = get_full_frequencies(users_tokens)
freq_pairs = freqs.items()
wc = WordCloud(max_words=2000, mask=trump_coloring,
max_font_size=40, random_state=42)
wc.generate_from_frequencies(freq_pairs)
image_colors = ImageColorGenerator(trump_coloring)
# plt.imshow(wc)
# plt.axis("off")
#
# plt.figure()
plt.imshow(wc.recolor(color_func=image_colors))
# recolor wordcloud and show
# we could also give color_func=image_colors directly in the constructor
# plt.imshow(trump_coloring, cmap=plt.cm.gray)
plt.axis("off")
plt.show()
示例9: generate_cloud
def generate_cloud():
d = path.dirname(__file__)
janice = open(path.join(d, 'messages.txt')).read()
group_mask = misc.imread(path.join(d, "mask.png"), flatten=True)
wc = WordCloud(background_color="white", max_words = 2000, mask=group_mask)
wc.generate(text)
wc.to_file(path.join(d, "masked.jpg"))
示例10: wordCloud
def wordCloud(text_array,name,keyword=""):
new_text_arr=[]
if keyword is not "":
keyword=keyword.split(" ")[1]
for text in text_array:
if keyword in text:
new_text_arr.append(text)
text_array=new_text_arr
cloud_text=""
for text in text_array:
cloud_text+=text+" "
m_stopwords=['police','traffic','sir']
for word in m_stopwords:
STOPWORDS.add(word)
image_mask = os.path.join(BASE_DIR, 'static/tool/img/nebula.png')
coloring = imread(image_mask)
wordcloud = WordCloud(stopwords=STOPWORDS,background_color="white",mask=coloring,ranks_only=True,max_words=50).generate(cloud_text)
filename=os.path.join(BASE_DIR, 'static/tool/img/'+name+'.png')
image_colors = ImageColorGenerator(coloring)
wordcloud.recolor(color_func=image_colors)
wordcloud.to_file(filename)
data_uri = open(filename, 'rb').read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}" style="height:400px;">'.format(data_uri)
layout=wordcloud.layout_
words_colours={}
count=1
for lo in layout:
entry={}
entry['word']=lo[0][0]
color=lo[len(lo)-1]
color=color[4:]
color=color[:-1]
color_split=color.split(',')
color_num=[int(x) for x in color_split]
color_hex='#%02x%02x%02x' % tuple(color_num)
# print color_num
entry['color']=color_hex
words_colours[count]=entry
count+=1
# print words_colours
list_html=""
cap=51
if cap>len(words_colours):
cap=len(words_colours)
for i in range(1,cap):
list_html+='<li class="list-group-item" ><a class="cloud-key-'+name+'" href="#" style="color:'+words_colours[i]['color']+'">'
list_html+="#"+str(i)+" "+words_colours[i]['word']+'</a></li>'
return (img_tag,list_html)
示例11: cal_and_show_jd_hot_words
def cal_and_show_jd_hot_words(self, jd_dir='../spider/jd'):
"""
calculate and show hot words of Job Description (JD)
:param jd_dir:
:return:
"""
if not os.path.exists(jd_dir) or len(os.listdir(jd_dir)) == 0:
print('Error! No valid content in {0}'.format(jd_dir))
sys.exit(0)
else:
jd_and_dir = {_.split('.')[0]: os.path.join(jd_dir, _) for _ in os.listdir(jd_dir)}
for k, v in jd_and_dir.items():
text = "".join(pd.read_excel(v)['详情描述'])
jieba.analyse.set_stop_words(STOPWORDS_PATH)
jieba.load_userdict(USER_CORPUS)
hot_words_with_weights = jieba.analyse.extract_tags(text, topK=30, withWeight=True, allowPOS=())
frequencies = {_[0]: _[1] for _ in hot_words_with_weights}
print(frequencies)
x, y = np.ogrid[:300, :300]
mask = (x - 150) ** 2 + (y - 150) ** 2 > 130 ** 2
mask = 255 * mask.astype(int)
wordcloud = WordCloud(font_path='./msyh.ttf', width=600, height=300, background_color="white",
repeat=False,
mask=mask)
wordcloud.generate_from_frequencies(frequencies)
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()
示例12: generate_word_cloud
def generate_word_cloud(img_bg_path,top_words_with_freq,font_path,to_save_img_path,background_color = 'white'):
# 读取背景图形
img_bg = imread(img_bg_path)
# 创建词云对象
wc = WordCloud(font_path = font_path, # 设置字体
background_color = background_color, # 词云图片的背景颜色,默认为白色
max_words = 500, # 最大显示词数为1000
mask = img_bg, # 背景图片蒙版
max_font_size = 50, # 字体最大字号
random_state = 30, # 字体的最多模式
width = 1000, # 词云图片宽度
margin = 5, # 词与词之间的间距
height = 700) # 词云图片高度
# 用top_words_with_freq生成词云内容
wc.generate_from_frequencies(top_words_with_freq)
# 用matplotlib绘出词云图片显示出来
plt.imshow(wc)
plt.axis('off')
plt.show()
# 如果背景图片颜色比较鲜明,可以用如下两行代码获取背景图片颜色函数,然后生成和背景图片颜色色调相似的词云
#img_bg_colors = ImageColorGenerator(img_bg)
#plt.imshow(wc.recolor(color_func = img_bg_colors))
# 将词云图片保存成图片
wc.to_file(to_save_img_path)
示例13: generate_wc
def generate_wc(self, background_color='#ffffff'):
"""generate wordcloud and save to file"""
# fig_kw = dict(figsize=(self.width/self.dpi, self.height/self.dpi),
# dpi=self.dpi)
self.get_exclude_words()
try:
imgpath = os.path.join(self.curdir, self.wordcloud_mask)
arr = np.array(Image.open(imgpath))
# Other masks can be extracted from
# Font-Awesome (http://minimaxir.com/2016/05/wordclouds/)
# Download font or use the default one
font_path = get_font(self.font_name)
if self.allow_font_change:
logger.info('Using {} font'.format(font_path))
# print(font_path)
wc = WordCloud(width=self.width, height=self.height,
font_path=font_path, colormap=self.cmap,
stopwords=self.exclude_words,
background_color=background_color, mode='RGBA',
mask=arr).generate(self.text)
self.make_img_file()
wc.to_file(self.img_file)
self.error_in_wordcloud_gen = None
self.font_name = None # reset to default
except Exception as e:
self.error_in_wordcloud_gen = e
示例14: topic_word_cloud
def topic_word_cloud(nmf, topic_idx, max_words=300, figsize=(14, 8), width=2400, height=1300, ax=None):
''' Create word cloud for a given topic
INPUT:
nmf: NMFClustering object
topic_idx: int
max_words: int
Max number of words to encorporate into the word cloud
figsize: tuple (int, int)
Size of the figure if an axis isn't passed
width: int
height: int
ax: None or matplotlib axis object
'''
wc = WordCloud(background_color='white', max_words=max_words, width=width, height=height)
word_freq = nmf.topic_word_frequency(topic_idx)
# Fit the WordCloud object to the specific topics word frequencies
wc.fit_words(word_freq)
# Create the matplotlib figure and axis if they weren't passed in
if not ax:
fig = plt.figure(figsize=figsize)
ax = fig.add_subplot(111)
ax.imshow(wc)
ax.axis('off')
示例15: test_repeat
def test_repeat():
short_text = "Some short text"
wc = WordCloud(stopwords=[]).generate(short_text)
assert_equal(len(wc.layout_), 3)
wc = WordCloud(max_words=50, stopwords=[], repeat=True).generate(short_text)
# multiple of word count larger than max_words
assert_equal(len(wc.layout_), 51)
# relative scaling doesn't work well with repeat
assert_equal(wc.relative_scaling, 0)
# all frequencies are 1
assert_equal(len(wc.words_), 3)
assert_array_equal(list(wc.words_.values()), 1)
frequencies = [w[0][1] for w in wc.layout_]
assert_array_equal(frequencies, 1)
repetition_text = "Some short text with text"
wc = WordCloud(max_words=52, stopwords=[], repeat=True)
wc.generate(repetition_text)
assert_equal(len(wc.words_), 4)
# normalized frequencies
assert_equal(wc.words_['text'], 1)
assert_equal(wc.words_['with'], .5)
assert_equal(len(wc.layout_), wc.max_words)
frequencies = [w[0][1] for w in wc.layout_]
# check that frequencies are sorted
assert_true(np.all(np.diff(frequencies) <= 0))