本文整理汇总了Python中wordcloud.WordCloud类的典型用法代码示例。如果您正苦于以下问题:Python WordCloud类的具体用法?Python WordCloud怎么用?Python WordCloud使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WordCloud类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:gasongjian,项目名称:reportgen,代码行数:35,代码来源:report.py
示例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')
开发者ID:abhik1368,项目名称:teamhrc,代码行数:31,代码来源:__init__.py
示例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
开发者ID:LazarusRS,项目名称:words_image,代码行数:7,代码来源:words_image.py
示例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
开发者ID:mingkai1688,项目名称:hello-python,代码行数:34,代码来源:hello.py
示例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
开发者ID:njr175,项目名称:ERP_SCANR,代码行数:28,代码来源:wc.py
示例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
开发者ID:usedrose,项目名称:Python,代码行数:26,代码来源:wordCloud_test.py
示例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()
开发者ID:Eajack,项目名称:py_spider,代码行数:30,代码来源:commentAnalysis.py
示例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()
开发者ID:arssher,项目名称:sphere_dm,代码行数:25,代码来源:hw4_collect_tweets.py
示例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"))
开发者ID:Raytray,项目名称:gmail_wordcloud,代码行数:7,代码来源:main.py
示例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)
开发者ID:sbagroy986,项目名称:PoliceOSMDashboard,代码行数:60,代码来源:graphing.py
示例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()
开发者ID:EclipseXuLu,项目名称:LagouJob,代码行数:35,代码来源:hot_words_generator.py
示例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)
开发者ID:dnxbjyj,项目名称:python-basic,代码行数:29,代码来源:santi_cloud.py
示例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
开发者ID:dennissergeev,项目名称:atmosscibot,代码行数:31,代码来源:atmosscibot.py
示例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')
开发者ID:jbgalvanize,项目名称:election_analysis,代码行数:25,代码来源:plots.py
示例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))
开发者ID:StoveJunJun,项目名称:word_cloud,代码行数:25,代码来源:test_wordcloud.py
示例16: get_tagcloud
def get_tagcloud(self, tags, tag_limit=None):
tag_limit = tag_limit or len(tags)
tags = sorted(tags, key=lambda kv: -kv['count'])[:tag_limit] # Get top X tags
tag_dict = {t['tag_name']: t['count'] for t in tags}
# Generate a word cloud image
wordcloud = WordCloud(
background_color='white',
min_font_size=10,
max_font_size=60,
width=self.tagcloud_width,
height=self.tagcloud_height or 30 * len(tags) / 2 + 10,
font_path=os.path.sep.join([settings.STATIC_ROOT, 'fonts', 'OpenSans-Regular.ttf'])
).generate_from_frequencies(tag_dict)
tag_counts = [t['count'] for t in tags]
step = (float(max(tag_counts))) / len(self.color_selection)
thresholds = list(reversed([int(round(i * step)) for i in range(len(self.color_selection))]))
def get_color(word, font_size, position, orientation, random_state=None, **kwargs):
index = next((i for i, t in enumerate(thresholds) if tag_dict[word] >= t), 0)
return self.color_selection[index]
wordcloud.recolor(color_func=get_color)
image = wordcloud.to_image()
filepath = self.get_write_to_path(ext="png")
image.save(filepath)
return encode_file_to_base64(filepath, "data:image/png;base64,")
开发者ID:fle-internal,项目名称:content-curation,代码行数:28,代码来源:export_writer.py
示例17: make_word_cloud
def make_word_cloud(text, save_path, background_color='black'):
# text expected to a string or a list of [(word, count), ...]
from wordcloud import WordCloud
import os
def col_fun(word, *args, **kw):
return '#333'
if type(text) == str:
big_string = text
else:
big_string = ''
for word in text:
big_string = big_string + ''.join((word[0]+' ') * word[1])
# print 'trying to make cloud: %s' % save_path
# print os.getcwd()
wc = WordCloud(background_color=background_color,
color_func=col_fun,
max_words=10000,
height=200,
width=700,
font_path='app/static/fonts/NanumScript.ttc').generate(big_string)
wc.generate(big_string)
wc.to_file('app/%s' % save_path)
开发者ID:silky,项目名称:beautifulcity,代码行数:25,代码来源:tagsf.py
示例18: make_cloud
def make_cloud(words, image, size=10, filename='figures/cloud.png', max_words=200, horizontal=0.8):
# Remove URLs, 'RT' text, screen names, etc
my_stopwords = ['RT', 'amp', 'lt']
words_no_urls = ' '.join([word for word in words.split()
if word not in my_stopwords])
# Add stopwords, if needed
stopwords = STOPWORDS.copy()
stopwords.add("RT")
stopwords.add('amp')
stopwords.add('lt')
# Load up a logo as a mask & color image
logo = imread(image)
# Generate colors
image_colors = ImageColorGenerator(logo)
# Generate plot
wc = WordCloud(stopwords=stopwords, mask=logo, color_func=image_colors, scale=0.8,
max_words=max_words, background_color='white', random_state=42, prefer_horizontal=horizontal)
wc.generate(words_no_urls)
plt.figure(figsize=(size, size))
plt.imshow(wc)
plt.axis("off")
plt.savefig(filename)
开发者ID:dr-rodriguez,项目名称:conventions_2016,代码行数:29,代码来源:wordcloud.py
示例19: wcloud
def wcloud(wf, color, save_as=None):
"""Create a word cloud based on word frequencies,
`wf`, using a color function from `wc_colors.py`
Parameters
----------
wf : list
(token, value) tuples
color : function
from `wc_colors.py`
save_as : str
filename
Returns
-------
None
"""
wc = WordCloud(background_color=None, mode='RGBA',
width=2400, height=1600, relative_scaling=0.5,
font_path='/Library/Fonts/Futura.ttc')
wc.generate_from_frequencies(wf)
plt.figure()
plt.imshow(wc.recolor(color_func=color, random_state=42))
plt.axis("off")
if save_as:
plt.savefig(save_as, dpi=300, transparent=True)
开发者ID:juanshishido,项目名称:okcupid,代码行数:26,代码来源:plotting.py
示例20: create_wordclouds
def create_wordclouds(self, text, name_of_cloud, additional_stop_list, max_words, width, height, bigram = False):
text_nopunc = self.remove_punctuation(text, "", "")
text_lower = text_nopunc.lower()
stop = self.stopwords
stop.extend(additional_stop_list)
text_nostop = self.remove_stopword(text_lower, stop)
tokens = wt(text_nostop)
text_lem = self.lemmatize(tokens)
tokens_lem = wt(text_lem)
my_bigrams = nltk.bigrams(tokens_lem)
if bigram:
bigram_merged=list()
for line in my_bigrams:
bigram_merged.append(line[0]+' ' + line[1])
counts = collections.Counter(bigram_merged)
else:
counts = collections.Counter(tokens_lem)
final = counts.most_common(max_words)
max_count = max(final, key=operator.itemgetter(1))[1]
final = [(name, count / float(max_count))for name, count in final]
# tags = make_tags(final, maxsize=max_word_size)
# create_tag_image(tags, name_of_cloud+'.png', size=(width, height), layout=3, fontname='Crimson Text', background = (255, 255, 255))
# temp_cloud = " ".join(text for text, count in final)
word_cloud = WordCloud(font_path="fonts/Georgia.ttf",
width=width, height=height, max_words=max_words, stopwords=stop)
word_cloud.fit_words(final)
word_cloud.to_file(name_of_cloud + ".png")
开发者ID:nus-iss-ca,项目名称:text-mining,代码行数:29,代码来源:util.py
注:本文中的wordcloud.WordCloud类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论