本文整理汇总了Python中wordcloud.WordCloud.generate方法的典型用法代码示例。如果您正苦于以下问题:Python WordCloud.generate方法的具体用法?Python WordCloud.generate怎么用?Python WordCloud.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wordcloud.WordCloud
的用法示例。
在下文中一共展示了WordCloud.generate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wordcloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def wordcloud(datafile):
#remove stop words, the most common words in a language
vectorizer=CountVectorizer(stop_words='english')
for word in vectorizer.get_stop_words():
STOPWORDS.add(word)
STOPWORDS.add("said")
pony_mask = np.array(Image.open("../pinkyB.jpg"))
wc = WordCloud(background_color="black", max_words=2000, mask=pony_mask, stopwords=STOPWORDS)
#init dictionary with the five categories
categoriesSet = set(datafile["Category"])
categoriesDict = dict.fromkeys(categoriesSet,"")
#Conditional Selection
# business = datafile.ix[datafile["Category"]=="Business"]
# print business["Content"].size
#fill index with data from cv
for index, row in datafile.iterrows():
categoriesDict[row["Category"]] += str(row["Content"])
for category, text in categoriesDict.iteritems():
wc.generate(text)
image = wc.to_image()
image.save("../wordcloud/wordcloud_" + category + ".jpg")
return
示例2: generateWordCloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def generateWordCloud(node, contribs, wordsToShow=None, normalize=True, normMin=0, normMax=1):
contrib = contribs[node]
if (normalize):
contrib = normalizeContrib(contrib, normMin, normMax)
# generate text
text = generateText(contrib, wordsToShow)
# load mask
d = path.dirname(__file__)
circle_mask = imread(path.join(d, "black_circle_mask_whitebg.png"))
# gen word cloud
wc = WordCloud(background_color="white", max_words=2000, mask=circle_mask)
wc.generate(text)
# store to file
wc.to_file(path.join(d, "node.png"))
# show
useColorFunc = False #normalize
if (useColorFunc):
plt.imshow(wc.recolor( color_func=pos_neg_color_func ))
else:
plt.imshow(wc)
plt.axis("off")
plt.show()
示例3: txt2pic
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def txt2pic(txt_file, out_png, font_path, mask_file):
text_address = path.abspath(txt_file)
text = open(text_address).read() #读取文本
text_cut = jieba.cut(text) #分词
new_textlist = ' '.join(text_cut) #组合
pic_address = path.abspath(mask_file)
pic = imread(pic_address) #读取图片
pic_color = ImageColorGenerator(pic) #根据图片生成颜色函数
wc = WordCloud(background_color='white', #构造wordcloud类
mask=pic,
width = 750,
height = 750,
max_font_size = 80,
random_state=30,
font_path=font_path,
max_words=500,
min_font_size=2,
color_func=pic_color
)
wc.generate(new_textlist) #生成词云图
plt.figure() #画图
plt.imshow(wc)
plt.axis("off")
plt.show()
wc.to_file(out_png) #保存图片
示例4: main
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def main():
wr=WordReader()
# wlist=wr.word_reader('data1/dt01.txt')
wcount=''
for root,dirs,files in os.walk('data2'):
for file in files:
file_path=os.path.join(root,file)
wlist=wr.word_reader(file_path)
wcount+=wlist
back_coloring = np.array(Image.open("./sky.png"))
wc = WordCloud(
background_color="white", #背景颜色
max_words=1000,# 词云显示的最大词数
mask=back_coloring,#设置背景图片
max_font_size=150, #字体最大值
random_state=42,
)
wc.generate(wcount)
#
# wc.generate_from_frequencies(word_list)
# wc.fit_words(word_list)
plt.figure()
plt.imshow(wc)
plt.axis("off")
plt.show()
示例5: run_yt
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def run_yt():
yt = ds.acquire_youtube()
ytimg = imread("ytlogo.png")
wc = WordCloud(mask=ytimg)
image_colors = ImageColorGenerator(ytimg)
wc.generate(word_list_yt(ds.mean(yt[0]), ds.standard_deviation(yt[0])))
plt.imshow(wc.recolor(color_func = image_colors))
plt.axis("off")
plt.savefig('popularWordsYT.png', bbox_inches = 'tight', dpi = 200)
words, vidcount = ds.word_count_yt('title', ds.mean(yt[0]), ds.standard_deviation(yt[0]))
data1 = []
data2 = []
labels = []
count = 0
for w in sorted(words, key=words.get, reverse=True):
labels.append(w)
data1.append(1000 * words[w]/vidcount)
count +=1
if count == 10:
break
words, vidcount = ds.word_count_dailymotion( 0, 0 )
for w in labels:
data2.append(1000 * words[w]/vidcount)
create_dualbargraph(data1, data2, labels, 'wordUseCompYT.png')
Theta = da.yt_thetas()
for x in xrange(len(Theta)):
Theta[x] = Theta[x]/10000
print Theta
create_bargraph(Theta,('duration', 'date created', 'y-intercept'), 'barGraphYT.png')
示例6: test_repeat
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
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))
示例7: generate_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
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"))
示例8: wordcloudplot_focus
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def wordcloudplot_focus(self, yizhongzhazha=None, backimage=None):
"""Do wordcloud plots for contacts. need to run relationship()
first to get self._relationship.
Parameters
yizhongzhazha: pandas object by loading the data
backimage: background image file's directory
Returns: basic word cloud plots saved in files
"""
if yizhongzhazha is None:
print("Need load message table first.")
return
if self._contacts_topN is None:
print("need to run relationship() first.")
return
if backimage is not None:
custompic = imread(backimage)
else:
custompic = None
if not os.path.exists('./wordcloud'):
os.makedirs('./wordcloud')
wordcloud = WordCloud(background_color="white", mask=custompic,
max_words=2000,scale=3)
for k in range(len(self._contacts_topN)):
text=self._relationship.iloc[:,k]
text_to_wordcloud=[]
for i in range(len(text)):
text_to_wordcloud.append((text.index.values[i]+' ')*text[i])
text=''.join(text_to_wordcloud)
wordcloud.generate(text)
wordcloud.to_file("./wordcloud/"+self._relationship.columns[k]+'2.png')
示例9: genwordcloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
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
示例10: run_dm
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def run_dm():
dm = ds.acquire_dailymotion()
dmimg = imread("dmlogo.png")
# Read the whole text.
wc = WordCloud(mask=dmimg)
image_colors = ImageColorGenerator(dmimg)
wc.generate(word_list_dailymotion(ds.mean(dm[0]), ds.standard_deviation(dm[0])))
# Open a plot of the generated image.
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis("off")
plt.savefig('popularWordsDM.png', bbox_inches = 'tight', dpi = 200)
words, vidcount = ds.word_count_dailymotion(ds.mean(dm[0]), ds.standard_deviation(dm[0]))
data1 = []
data2 = []
labels = []
count = 0
for w in sorted(words, key=words.get, reverse=True):
labels.append(w)
data1.append(1000 * words[w]/vidcount)
count +=1
if count == 10:
break
words, vidcount = ds.word_count_dailymotion( 0, 0 )
for w in labels:
data2.append(1000 * words[w]/vidcount)
create_dualbargraph(data1, data2, labels, 'wordUseCompDM.png')
create_bargraph(da.dm_thetas(),('fans','duration','date created', 'y-intercept'), 'barGraphDM.png')
示例11: create_word_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def create_word_cloud(ballots, chart_directory, image_name, mask_file,
stop_words, word_counts=None):
"""
Generates a word cloud from given ballots.
"""
if word_counts is None:
word_counts=[25, 50, 100, 1000]
text = ''
for ballot in ballots:
text = ''.join((text, ballot.feedback,))
all_stop_words = STOPWORDS
all_stop_words |= set(stop_words)
for word_count in word_counts:
if mask_file:
color_mask = imread(mask_file)
image_colors = ImageColorGenerator(color_mask)
wc = WordCloud(background_color="white", max_words=word_count,
mask=color_mask,
stopwords=all_stop_words,
color_func=image_colors,
max_font_size=80, random_state=42)
else:
wc = WordCloud(background_color="white", max_words=word_count,
stopwords=all_stop_words,
max_font_size=80, random_state=42)
wc.generate(text)
axis_image = plt.imshow(wc)
plt.axis("off")
image_name_with_count = '{0}-{1}.png'.format(image_name, str(word_count))
logger.info('...creating word cloud {0}'.format(image_name_with_count))
save_location = os.path.join(chart_directory, image_name_with_count)
plt.savefig(save_location)
plt.close()
示例12: make_word_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
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)
示例13: word_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def word_cloud(posts):
text = u' '.join(post['content'] for post in posts)
'''
if os.path.isdir('/home/public/stanford-corenlp-full-2015-04-20/'):
proc = CoreNLP("pos", corenlp_jars=["/home/public/stanford-corenlp-full-2015-04-20/*"])
sentenses = proc.parse_doc(text)['sentences']
text = ''
for sentence in sentenses:
text += u' '.join(sentence['lemmas']) + u' '
'''
wordcloud = WordCloud(background_color="white", width=1200, height=900, margin=0)
wordcloud.generate(text)
fig = plt.gcf()
# fig.set_size_inches(15, 8.5)
# Open a plot of the generated image.
plt.imshow(wordcloud)
plt.axis("off")
imgdata = StringIO.StringIO()
fig.savefig(imgdata, format='png', bbox_inches='tight')
imgdata.seek(0) # rewind the data
plt.close()
uri = urllib.quote(base64.b64encode(imgdata.buf))
return uri
示例14: main
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def main(save_files = False, db_filename = '../output/database.sqlite'):
conn = sqlite3.connect(db_filename)
c = conn.cursor()
# Retrieve papers
c.execute('''SELECT *
FROM Papers''')
paper_content = c.fetchall()
conn.close()
titles = ''
for pc in paper_content:
titles += pc[1]
# A Marvin Minsky mask
mask = np.array(Image.open("../files/minsky_mask.png"))
wc = WordCloud(background_color="white", max_words=2000, mask=mask, stopwords=STOPWORDS.copy())
# Generate word cloud
wc.generate(titles)
if (save_files):
# Store to file
wc.to_file("../files/title_cloud.png")
# Show word cloud
plt.imshow(wc)
plt.axis("off")
# Show mask
# plt.figure()
# plt.imshow(mask, cmap=plt.cm.gray)
# plt.axis("off")
plt.show()
示例15: get_word_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate [as 别名]
def get_word_cloud(content, file_name,
dict = BASE_DICT,
folder_path=BASE_PATH+os.sep+"WordCloud",
font_path=BASE_PATH+os.sep+"WordCloud"+os.sep+"幼圆.ttf",
width=400,
height=200,
margin=5,
ranks_only=False,
prefer_horizontal=0.9,
mask=None,
scale=1,
color_func=None,
max_words=200,
stopwords=None,
random_state=None,
background_color='white',
max_font_size=None):
wc = WordCloud(font_path=font_path,width=width,height=height,margin=margin,ranks_only=ranks_only,
prefer_horizontal=prefer_horizontal,mask=mask,scale=scale,max_words=max_words,
stopwords=stopwords,random_state=random_state,background_color=background_color,max_font_size=max_font_size#,color_func=color_func
)
# if dict:
# jieba.load_userdict(dict)
# after = ' '.join(jieba.cut(content, cut_all=False))
file_with_path = "{BASE_PATH}{sep}{file}".format( BASE_PATH=folder_path ,sep=os.sep,file=file_name)
wc.generate(content)
print(file_with_path)
wc.to_file(file_with_path)
return file_name