本文整理汇总了Python中wordcloud.WordCloud.generate_from_frequencies方法的典型用法代码示例。如果您正苦于以下问题:Python WordCloud.generate_from_frequencies方法的具体用法?Python WordCloud.generate_from_frequencies怎么用?Python WordCloud.generate_from_frequencies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wordcloud.WordCloud
的用法示例。
在下文中一共展示了WordCloud.generate_from_frequencies方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: draw_tag_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
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()
示例2: generate_word_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
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)
示例3: generate_image
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
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: make_word_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def make_word_cloud(product, sentiment):
if sentiment == "all":
pos, neg = get_top_five_phrases(product,sentiment)
pos.index = range(0,len(pos))
neg.index = range(0,len(neg))
pos_words_array = []
neg_words_array = []
for i in range(0,len(pos)):
pos_words_array.append((pos["vocab"][i].upper(), float(pos["count"][i])))
for i in range(0,len(neg)):
neg_words_array.append((neg["vocab"][i].upper(), float(neg["count"][i])))
wc = WordCloud(background_color="white", max_words=2000,
max_font_size=300, random_state=42)
# generate word cloud for positive
positive_name = '../app/static/img/pos_wordcloud.png'
wc.generate_from_frequencies(pos_words_array)
wc.recolor(color_func=pos_color_func, random_state=3)
wc.to_file(positive_name)
# generate word cloud for negative
negative_name = '../app/static/img/neg_wordcloud.png'
wc.generate_from_frequencies(neg_words_array)
wc.recolor(color_func=neg_color_func, random_state=3)
wc.to_file(negative_name)
return positive_name, negative_name
示例5: wcloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
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)
示例6: cal_and_show_jd_hot_words
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
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()
示例7: generate_image
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def generate_image(files, src_image):
content = get_content(files)
graph = np.array(Image.open(src_image))
wc = WordCloud(font_path=os.path.join(CUR_DIR, 'fonts/simhei.ttf'),
background_color='white', max_words=MAX_WORDS, mask=graph)
words = process_text(content)
wc.generate_from_frequencies(words)
image_color = ImageColorGenerator(graph)
return wc, image_color
示例8: create_Cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def create_Cloud(self, data):
print ('creating wordpair graph...')
self.twitter_mask = np.array(Image.open(path.join(path.dirname(__file__), 'MASK/twitter_mask.png')))
for word in data:
wordcloud = WordCloud(font_path=path.join(path.dirname(__file__), 'FONT/CabinSketch-Bold.ttf'), relative_scaling=.5, width=1800, height=1400, stopwords=None, mask=self.twitter_mask)
wordcloud.generate_from_frequencies(list(data[word].items()))
wordcloud.to_file(path.join(path.dirname(__file__), 'WORDPAIRS/'+word+'.png'))
return
示例9: create_wordcloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def create_wordcloud(wordcloud_data):
mask = imread(MASK_PATH)
wordcloud = WordCloud(max_words=1000, mask=mask, stopwords=None, margin=10, random_state=1,
font_path=FONT_PATH, prefer_horizontal=1.0, width=WORD_CLOUD_WIDTH,
height = WORD_CLOUD_HEIGHT, background_color='black', mode='RGBA')
word_importance_list = [(dct['word'], dct['importance']) for dct in wordcloud_data['words']]
partisanship_list = [dct['partisanship'] for dct in wordcloud_data['words']]
kwargs = {'word_partisanship': partisanship_list}
wordcloud.generate_from_frequencies(word_importance_list, **kwargs)
return wordcloud
示例10: makeImage
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def makeImage(text):
alice_mask = np.array(Image.open("alice_mask.png"))
wc = WordCloud(background_color="white", max_words=1000, mask=alice_mask)
# generate word cloud
wc.generate_from_frequencies(text)
# show
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
示例11: generateWordCloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def generateWordCloud():
words_old = [ #some words to visualize
{
'word': 'this',
'size': 55,
'color': COLOR_RED,
'font': '\'Indie Flower\', cursive',
'angle': '45'
},
{
'word': 'Test',
'size': 73,
'color': COLOR_BLUE,
'font': '\'Open Sans\', sans-serif',
'angle': '-30'
},
{
'word': 'kinDA',
'size': 153,
'color': COLOR_GREEN,
'font': '\'Indie Flower\', cursive',
'angle': '-150'
},
{
'word': 'WERKS',
'size': 33,
'color': COLOR_PURPLE,
'font': '\'Open Sans\', sans-serif',
'angle': '90'
}
]
# Read the whole text.
words = [('chipotle', 55), ('McDonalds', 15), ('burgerking', 12), ('wendies', 41), ('using', 1), ('font', 2), ('randomize', 1), ('yet', 1), ('HHBs', 1), ('knowledge', 1), ('generator', 1), ('everything', 3), ('implementation', 2), ('simple', 2), ('might', 1), ('pixel', 1), ('real', 1), ('designs', 1), ('good', 1), ('without', 1), ('checking', 1), ('trees', 2), ('famous', 1), ('boxes', 1), ('every', 1), ('optimal', 1), ('front', 1), ('integer', 1), ('bit', 2), ('now', 2), ('easily', 1), ('shape', 1), ('fs', 1), ('stuff', 1), ('found', 1), ('works', 1), ('view', 1), ('right', 1), ('force', 1), ('generation', 3), ('hard', 1), ('back', 1), ('second', 1), ('sure', 1), ('Hopefully', 1), ('portrait', 1), ('best', 1), ('really', 2), ('speed', 1), ('method', 2), ('dataset', 2), ('figuring', 1), ('modify', 1), ('understanding', 1), ('represented', 1), ('come', 1), ('generate', 2), ('last', 2), ('fit', 1), ('Tweak', 1), ('study', 1), ('studied', 1), ('turn', 1), ('place', 2), ('isn', 1), ('uses', 2), ('implement', 1), ('sprites', 1), ('adjustable', 1), ('render', 1), ('color', 2), ('one', 1), ('fashion', 1), ('fake', 1), ('cloud', 5), ('size', 2), ('guess', 1), ('working', 1), ('Separate', 1), ('sake', 1), ('placing', 1), ('brute', 1), ('least', 2), ('insider', 1), ('lot', 1), ('basic', 1), ('prototype', 1), ('start', 1), ('empty', 1), ('sort', 1), ('testing', 1), ('spiral', 1), ('overlapping', 1), ('else', 1), ('controller', 1), ('part', 2), ('somewhat', 1), ('varying', 1), ('MySQL', 1), ('quad', 2), ('copy', 1), ('also', 1), ('bundled', 1), ('word', 9), ('algorithm', 2), ('typography', 1), ('will', 1), ('fll', 1), ('following', 2), ('bet', 1), ('perfecting', 1), ('proved', 1), ('orientation', 2), ('wordle', 1), ('JavaScript', 1), ('collision', 2), ('reads', 1), ('want', 1), ('ready', 1), ('compressing', 1), ('apparently', 1), ('check', 1), ('inefficient', 1), ('preferably', 1), ('end', 2), ('thing', 2), ('efficient', 1), ('make', 3), ('note', 1), ('python', 3), ('need', 3), ('complex', 1), ('instead', 1), ('hierarchical', 1), ('used', 1), ('ft', 1), ('see', 1), ('though', 2), ('moving', 1), ('preliminary', 1), ('data', 1), ('fm', 1), ('Figure', 2), ('database', 1), ('author', 1), ('together', 1), ('think', 1), ('provide', 1), ('definitely', 1), ('time', 1), ('position', 2), ('model', 2), ('D3', 1)]
alice_mask = np.array(Image.open(path.join(d,"alice_mask.png")))
burrito_mask = np.array(Image.open(path.join(d,"burrito2.png")))
print alice_mask.shape
print burrito_mask.shape
# Generate a word cloud image
wordcloud = WordCloud(
background_color="white",
max_words = 1500,
mask = burrito_mask)
wordcloud.generate_from_frequencies(words)
# The pil way (if you don't have matplotlib)
image = wordcloud.to_image()
#words = wordcloud.process_text(text)
#image.show()
return serveImg(image)
示例12: _save_word_cloud_img
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def _save_word_cloud_img(frequencies, file_path):
"""
ワードクラウドの画像ファイルを指定されたファイルパスに保存する。
参考:http://amueller.github.io/word_cloud/index.html
:param frequencies: タブル(単語, 出現頻度)のリスト
:param file_path: 画像ファイルのパス
"""
# 日本語フォントのパスが正しく設定されている必要がある。
font_path = config.JAPANESE_FONT_PATH
wc = WordCloud(background_color='white', max_font_size=320, font_path=font_path, width=900, height=500)
wc.generate_from_frequencies(frequencies)
wc.to_file(file_path)
示例13: create_word_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def create_word_cloud(df, mask_file, font_path):
mask = np.array(Image.open(mask_file))
wc = WordCloud(relative_scaling=0.5,
mask=mask,
prefer_horizontal=1.0,
background_color='white',
font_path=font_path)
wc.generate_from_frequencies(df.values)
return wc
示例14: word_cloud
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def word_cloud(dictionary,topic_index,topic_word):
wd={}
b_1 = np.argsort(topic_word[ipt,:])[::-1]
cloud_word = [str(dictionary[i])+' ' for i in b_1]
for j in b_1:
wd[str(dictionary[j])] = topic_word[topic_index,j]/np.sum(topic_word[topic_index,:])
huaji = imread('250px.png')
wc = WordCloud(width=1920, height=1080,background_color="white")
wc.generate_from_frequencies(wd.items())
plt.figure()
plt.imshow(wc)
plt.axis('off')
plt.show()
示例15: test_generate_from_frequencies
# 需要导入模块: from wordcloud import WordCloud [as 别名]
# 或者: from wordcloud.WordCloud import generate_from_frequencies [as 别名]
def test_generate_from_frequencies():
# test that generate_from_frequencies() takes input argument dicts
wc = WordCloud(max_words=50)
words = wc.process_text(THIS)
result = wc.generate_from_frequencies(words)
assert_true(isinstance(result, WordCloud))