Python wordcloud.WordCloud() Examples
The following are 30
code examples of wordcloud.WordCloud().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
wordcloud
, or try the search function
.
Example #1
Source File: analysis.py From dl-eeg-review with MIT License | 8 votes |
def generate_wordcloud(df, save_cfg=cfg.saving_config): brain_mask = np.array(Image.open("./img/brain_stencil.png")) def transform_format(val): if val == 0: return 255 else: return val text = (df['Title']).to_string() stopwords = set(STOPWORDS) stopwords.add("using") stopwords.add("based") wc = WordCloud( background_color="white", max_words=2000, max_font_size=50, mask=brain_mask, stopwords=stopwords, contour_width=1, contour_color='steelblue') wc.generate(text) # store to file if save_cfg is not None: fname = os.path.join(save_cfg['savepath'], 'DL-EEG_WordCloud') wc.to_file(fname + '.' + save_cfg['format']) #, **save_cfg)
Example #2
Source File: nzj.py From spider_python with Apache License 2.0 | 6 votes |
def generate_word_cloud(): """ 生成词云 :return: """ with open(filename, 'r') as f: word_content = f.read() # 使用jieba去分割 wordlist = jieba.cut(word_content, cut_all=True) wl_space_split = " ".join(wordlist) font = r'/Users/xingag/Library/Fonts/SimHei.ttf' wordcloud = WordCloud(font_path=font, width=1080, height=1920, margin=2).generate(wl_space_split) # 显示图片 plt.imshow(wordcloud) plt.axis("off") # 按照设置保存到本地文件夹 wordcloud.to_file("./output.png")
Example #3
Source File: seg.py From SentimentAnalysis with MIT License | 6 votes |
def main(): seg = Seg() doc = '''自然语言处理: 是人工智能和语言学领域的分支学科。 在这此领域中探讨如何处理及运用自然语言;自然语言认知则是指让电脑“懂”人类的语言。 自然语言生成系统把计算机数据转化为自然语言。自然语言理解系统把自然语言转化为计算机程序更易于处理的形式。''' # res = seg.seg_from_doc(doc) datalist = seg.get_data_from_mysql(1000, 0) keywords = dict(seg.get_keyword_from_datalist(datalist)) font_path = root_path + '/data/simfang.ttf' bg_path = root_path + '/data/bg.jpg' #back_color = np.array(Image.open(bg_path)) back_color = imread(bg_path) image_colors = ImageColorGenerator(back_color) wordcloud = WordCloud(font_path=font_path, background_color="white", mask=back_color, max_words=2000, max_font_size=100, random_state=48, width=1000, height=800, margin=2) wordcloud.generate_from_frequencies(keywords) plt.figure() plt.imshow(wordcloud.recolor(color_func=image_colors)) plt.axis("off") plt.show() wordcloud.to_file(root_path + '/data/pic2.png')
Example #4
Source File: pyltp_segment.py From nlp_learning with MIT License | 6 votes |
def show_wordCloud(word_freq): """ 词云显示 """ font = r'C:\Windows\Fonts\msyh.ttc' # 指定字体,不指定会报错 color_mask = imread("resource/timg.jpg") # 读取背景图片 wcloud = WordCloud( font_path=font, # 背景颜色 background_color="white", # 词云形状 mask=color_mask, # 允许最大词汇 max_words=2000, # 最大号字体 max_font_size=80) wcloud.generate_from_frequencies(dict(word_freq)) # 以下代码显示图片 plt.imshow(wcloud) plt.axis("off") plt.show() wcloud.to_file("data/wcimage/三体词云_3.png")
Example #5
Source File: jieba_segment.py From nlp_learning with MIT License | 6 votes |
def show_wordCloud(word_freq): """ 词云显示 """ font = r'C:\Windows\Fonts\msyh.ttc' # 指定字体,不指定会报错 color_mask = imread("resource/timg.jpg") # 读取背景图片 wcloud = WordCloud( font_path=font, # 背景颜色 background_color="white", # 词云形状 mask=color_mask, # 允许最大词汇 max_words=2000, # 最大号字体 max_font_size=80) wcloud.generate_from_frequencies(dict(word_freq)) # 以下代码显示图片 plt.imshow(wcloud) plt.axis("off") plt.show() wcloud.to_file("data/wcimage/三体词云_3.png")
Example #6
Source File: gensim_jb.py From nlp_learning with MIT License | 6 votes |
def show_wordCloud(word_freq): """ 词云显示 """ font = r'C:\Windows\Fonts\msyh.ttc' # 指定字体,不指定会报错 color_mask = imread("resource/timg.jpg") # 读取背景图片 wcloud = WordCloud( font_path=font, # 背景颜色 background_color="white", # 词云形状 mask=color_mask, # 允许最大词汇 max_words=2000, # 最大号字体 max_font_size=80) wcloud.generate_from_frequencies(dict(word_freq)) # 以下代码显示图片 plt.imshow(wcloud) plt.axis("off") plt.show() wcloud.to_file("data/wcimage/雪中_1.png")
Example #7
Source File: weibo_cloud.py From weibo_wordcloud with MIT License | 6 votes |
def gen_img(texts, img_file): data = ' '.join(text for text in texts) image_coloring = imread(img_file) wc = WordCloud( background_color='white', mask=image_coloring, font_path='/Library/Fonts/STHeiti Light.ttc' ) wc.generate(data) # plt.figure() # plt.imshow(wc, interpolation="bilinear") # plt.axis("off") # plt.show() wc.to_file(img_file.split('.')[0] + '_wc.png')
Example #8
Source File: text_processing.py From Listed-company-news-crawl-and-text-analysis with MIT License | 6 votes |
def PrintWorfCloud(self,documents,backgroundImgPath,fontPath): '''Print out the word cloud of all news(articles/documents). # Arguments: documents: Overall raw documents. backgroundImgPath: Background image path. fontPath: The path of windows fonts that used to create the word-cloud. ''' from scipy.misc import imread import matplotlib.pyplot as plt from wordcloud import WordCloud corpora_documents = self.jieba_tokenize(documents) #分词 for k in range(len(corpora_documents)): corpora_documents[k] = ' '.join(corpora_documents[k]) corpora_documents = ' '.join(corpora_documents) color_mask = imread(backgroundImgPath) #"C:\\Users\\lenovo\\Desktop\\Text_Mining\\3.jpg" cloud = WordCloud(font_path=fontPath,mask=color_mask,background_color='white',\ max_words=2000,max_font_size=40) #"C:\\Windows\\Fonts\\simhei.ttf" word_cloud = cloud.generate(corpora_documents) plt.imshow(word_cloud, interpolation='bilinear') plt.axis("off")
Example #9
Source File: Data_analysis.py From Spider with MIT License | 6 votes |
def create_wordcloud(content,image='weibo.jpg',max_words=5000,max_font_size=50): cut_text = " ".join(content) cloud = WordCloud( # 设置字体,不指定就会出现乱码 font_path="HYQiHei-25J.ttf", # 允许最大词汇 max_words=max_words, # 设置背景色 # background_color='white', # 最大号字体 max_font_size=max_font_size ) word_cloud = cloud.generate(cut_text) word_cloud.to_file(image) # 分词并去除停用词
Example #10
Source File: password_cloud.py From cracke-dit with MIT License | 6 votes |
def run(db, args): if platform.system() == "Darwin": mpl.use("TkAgg") import matplotlib.pyplot as plt title = "Top reused passwords for {}".format(args.domain) passwords = db.all_passwords wc = WordCloud(background_color="black", width=1280, height=800, margin=5, max_words=1000, color_func=__get_password_color(passwords)) wc.generate(" ".join([password for password, score in passwords])) plt.title(title) plt.imshow(wc, interpolation="nearest", aspect="equal") plt.axis("off") plt.show()
Example #11
Source File: lyric_nlp.py From web-crawler-tutorial with MIT License | 6 votes |
def lyrics(): with open('lyrics.json', 'r', encoding='utf-8') as f: data = json.load(f) tokens = list() for v in data.values(): # 斷詞後的結果, 若非空白且長度為 2 以上, 則列入詞庫 tokens += [seg for seg in jieba.cut(v) if seg.split() and len(seg) > 1] # 計算 tokens 內各詞彙的出現次數 counter = Counter(tokens) print(counter.most_common(10)) # 文字雲, 要顯示中文需附上字型檔 wcloud = WordCloud(font_path='NotoSansMonoCJKtc-Regular.otf').generate(' '.join(tokens)) plt.imshow(wcloud) plt.axis('off') plt.show()
Example #12
Source File: test_topicmod_visualize.py From tmtoolkit with Apache License 2.0 | 6 votes |
def test_write_wordclouds_to_folder(tmpdir): try: import lda import PIL from wordcloud import WordCloud except ImportError: pytest.skip('at least one of lda, Pillow, wordcloud not installed') path = tmpdir.mkdir('wordclouds').dirname data = model_io.load_ldamodel_from_pickle('tests/data/tiny_model_reuters_5_topics.pickle') model = data['model'] vocab = data['vocab'] phi = model.topic_word_ assert phi.shape == (5, len(vocab)) topic_word_clouds = visualize.generate_wordclouds_for_topic_words(phi, vocab, 10) visualize.write_wordclouds_to_folder(topic_word_clouds, path, 'cloud_{label}.png') for label in topic_word_clouds.keys(): assert os.path.exists(os.path.join(path, 'cloud_{label}.png'.format(label=label)))
Example #13
Source File: news_hot.py From web_scraping_and_data_analysis with MIT License | 6 votes |
def draw_word_cloud(content): d = os.path.dirname(__file__) img = Image.open(os.path.join(d, "toutiao.jpg")) width = img.width / 80 height = img.height / 80 alice_coloring = np.array(img) my_wordcloud = WordCloud(background_color="white", max_words=500, mask=alice_coloring, max_font_size=200, random_state=42, font_path=(os.path.join(d, "../common/font/PingFang.ttc"))) my_wordcloud = my_wordcloud.generate_from_frequencies(content) image_colors = ImageColorGenerator(alice_coloring) plt.figure(figsize=(width, height)) plt.imshow(my_wordcloud.recolor(color_func=image_colors)) plt.imshow(my_wordcloud) plt.axis("off") # 通过设置subplots_adjust来控制画面外边框 plt.subplots_adjust(bottom=.01, top=.99, left=.01, right=.99) plt.savefig("toutiao_wordcloud.png") plt.show()
Example #14
Source File: analysis.py From web_scraping_and_data_analysis with MIT License | 6 votes |
def draw_word_cloud(content): d = os.path.dirname(__file__) img = Image.open(os.path.join(d, "changzuoren.jpg")) width = img.width / 80 height = img.height / 80 alice_coloring = np.array(img) my_wordcloud = WordCloud(background_color="white", max_words=500, mask=alice_coloring, max_font_size=200, random_state=42, font_path=(os.path.join(d, "../common/font/PingFang.ttc"))) my_wordcloud = my_wordcloud.generate_from_frequencies(content) image_colors = ImageColorGenerator(alice_coloring) plt.figure(figsize=(width, height)) plt.imshow(my_wordcloud.recolor(color_func=image_colors)) plt.imshow(my_wordcloud) plt.axis("off") # 通过设置subplots_adjust来控制画面外边框 plt.subplots_adjust(bottom=.01, top=.99, left=.01, right=.99) plt.savefig("changzuoren_wordcloud.png") plt.show()
Example #15
Source File: visualize.py From tmtoolkit with Apache License 2.0 | 6 votes |
def generate_wordclouds_for_topic_words(topic_word_distrib, vocab, top_n, topic_labels='topic_{i1}', which_topics=None, return_images=True, **wordcloud_kwargs): """ Generate wordclouds for the top `top_n` words of each topic in `topic_word_distrib`. :param topic_word_distrib: topic-word distribution; shape KxM, where K is number of topics, M is vocabulary size :param vocab: vocabulary array of length M :param top_n: number of top values to take from each row of `distrib` :param topic_labels: labels used for each row; determine keys in in result dict; either single format string with placeholders ``"{i0}"`` (zero-based topic index) or ``"{i1}"`` (one-based topic index), or list of topic label strings :param which_topics: if not None, a sequence of indices into rows of `topic_word_distrib` to select only these topics to generate wordclouds from :param return_images: if True, store image objects instead of :class:`wordcloud.WordCloud` objects in the result dict :param wordcloud_kwargs: pass additional options to :class:`wordcloud.WordCloud`; updates options in :data:`~tmtoolkit.topicmod.visualize.DEFAULT_WORDCLOUD_KWARGS` :return: dict mapping row labels to wordcloud images or instances generated from each topic """ return generate_wordclouds_from_distribution(topic_word_distrib, row_labels=topic_labels, val_labels=vocab, top_n=top_n, which_rows=which_topics, return_images=return_images, **wordcloud_kwargs)
Example #16
Source File: __init__.py From notes with Apache License 2.0 | 5 votes |
def draw_imgae(counter): font_path = "C:/Users/96363/Desktop/markdown/font/HYBeiKeHeiW.ttf" # background_image = numpy.array(Image.open("C:\\Users\\96363\\Downloads\\british-library-FQph76jy1mo-unsplash.jpg")) wd = WordCloud( font_path=font_path, height=400, width=800, background_color="black", # mask=background_image ).generate_from_frequencies(counter) wd.to_file("world.png") # 词图显示 image = Image.open("world.png") image.show()
Example #17
Source File: data_vis_project_part4.py From SIP-2018 with GNU General Public License v3.0 | 5 votes |
def AddFigure(filteredDictionary, plotnum, title): wordcloud = WordCloud().generate_from_frequencies(filteredDictionary) plt.subplot(plotnum) plt.imshow(wordcloud, interpolation='bilinear') plt.title(title) plt.axis("off") #Search term used for this tweet #We want to filter this out!
Example #18
Source File: wc.py From xi-iot with MIT License | 5 votes |
def wordCloud(): logging.info("Generating wordCloud from text, cnt = %d", msgCnt) wordcloud = WordCloud(stopwords=STOPWORDS, width=800, height=400, background_color="white", max_words=1000).generate(wordText) logging.debug("wc freq %s: ", wordcloud.words_) wordcloud.to_file(mountPath + "/wordcloud.png")
Example #19
Source File: update.py From All-About-the-GAN with MIT License | 5 votes |
def update_wordcloud_abbr(): """ Update the figure wordcloud_category.jpg """ data = pd.read_csv('AllGAN-r2.tsv',delimiter='\t', encoding='utf-8') wordcloud = WordCloud(stopwords=STOPWORDS,relative_scaling = 0.2, random_state=3, max_words=2000, background_color='white').generate(' '.join(data['Abbr.'])) plt.figure(figsize=(12,12)) plt.imshow(wordcloud, interpolation="bilinear") plt.axis("off") #plt.show() #plt.savefig('wordcloud_title.png') wordcloud.to_file('wordcloud_abbr.png') wordcloud.to_file('docs/png/wordcloud_abbr.png')
Example #20
Source File: update.py From All-About-the-GAN with MIT License | 5 votes |
def update_wordcloud_category(): """ Update the figure wordcloud_category.jpg """ data = pd.read_csv('AllGAN-r2.tsv',delimiter='\t', encoding='utf-8') wordcloud = WordCloud(stopwords=STOPWORDS,relative_scaling = 0.2, random_state=3, max_words=2000, background_color='white').generate(' '.join(data['Category'])) plt.figure(figsize=(12,12)) plt.imshow(wordcloud, interpolation="bilinear") plt.axis("off") #plt.show() #plt.savefig('wordcloud_title.png') wordcloud.to_file('wordcloud_category.png') wordcloud.to_file('docs/png/wordcloud_category.png')
Example #21
Source File: update.py From All-About-the-GAN with MIT License | 5 votes |
def update_wordcloud_title(): """ Update the figure wordcloud_title.jpg """ data = pd.read_csv('AllGAN-r2.tsv',delimiter='\t', encoding='utf-8') # tmp_data = data['Title'].split(" ") for x in data # count_list = list([list(x) for x in data['Title'].value_counts().reset_index().values]) # wordcloud = WordCloud(stopwords=STOPWORDS,relative_scaling = 0.2, # max_words=2000, background_color='white').generate_from_frequencies(tmp_data) stopwords = set(STOPWORDS) #ganstop = ['Generative','Adversarial', 'Networks', 'Network', 'GAN', 'GANs', 'using', 'Learning', 'Training', 'Generation', # 'Neural', 'Net', 'Model', 'Nets', 'Deep', 'Based', 'Via', 'Conditional', 'Models', 'Examples'] #stopwords.add(ganstop) stopwords.add('Generative') stopwords.add('Adversarial') stopwords.add('Networks') stopwords.add('Network') stopwords.add('GAN') stopwords.add('GANs') stopwords.add('using') stopwords.add('Learning') stopwords.add('Training') stopwords.add('Generation') stopwords.add('Neural') stopwords.add('Net') stopwords.add('Model') stopwords.add('Nets') stopwords.add('Deep') stopwords.add('Based') stopwords.add('Via') stopwords.add('Conditional') stopwords.add('Models') stopwords.add('Examples') wordcloud = WordCloud(stopwords=stopwords,relative_scaling = 0.2, random_state=3, max_words=2000, background_color='white').generate(' '.join(data['Title'])) plt.figure(figsize=(12,12)) plt.imshow(wordcloud, interpolation="bilinear") plt.axis("off") #plt.show() #plt.savefig('wordcloud_title.png') wordcloud.to_file('wordcloud_title.png') wordcloud.to_file('docs/png/wordcloud_title.png')
Example #22
Source File: wc.py From xi-iot with MIT License | 5 votes |
def wordCloud2(): logging.info("Generating wordCloud from freq, cnt = %d", msgCnt) wordcloud = WordCloud(stopwords=STOPWORDS, width=800, height=400, background_color="white", max_words=1000).generate_from_frequencies(wordCounter) wordcloud.to_file(mountPath + "/wordcloud2.png")
Example #23
Source File: zhihu_answer_wordcloud.py From Python-tools with MIT License | 5 votes |
def seg_answer(): answer_content = open(question_id + ".txt",'r',encoding='utf-8').read() print("segging answers...") answer_seg = jieba.cut(answer_content) stopwords = open("chineseStopWords.txt",'r',encoding='GBK').read() new_answer_seg = [] for text in answer_seg: if text not in stopwords: new_answer_seg.append(text) string = ' '.join(new_answer_seg) print("making wordcloud...") wc = WordCloud(font_path='C:/Windows/Fonts/simkai.ttf',background_color='white',width=1000,height=800).generate(string) wc.to_file(question[0]+'.png')
Example #24
Source File: kaggle18.py From modin with Apache License 2.0 | 5 votes |
def generate_wordcloud(tup): wordcloud = WordCloud( background_color="white", max_words=50, max_font_size=40, random_state=42 ).generate(str(tup)) return wordcloud
Example #25
Source File: report.py From reportgen with MIT License | 5 votes |
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
Example #26
Source File: Wc.py From NotSoBot with MIT License | 5 votes |
def make_wc(self, text, max): try: wc = WordCloud(background_color='black', width=1024, height=768, max_words=max).generate(' '.join(text)) img = wc.to_image() b = BytesIO() img.save(b, 'png') b.seek(0) return b except Exception as e: return str(e)
Example #27
Source File: Wc.py From NotSoBot with MIT License | 5 votes |
def make_wc_custom(self, mask, text, max): try: coloring = np.array(PIL.Image.open(mask)) wc = WordCloud(width=1024, height=768, max_words=max, mask=coloring) wc = wc.generate(' '.join(text)) image_colors = ImageColorGenerator(coloring) wc = wc.recolor(color_func=image_colors) img = wc.to_image() b = BytesIO() img.save(b, 'png') b.seek(0) return b except Exception as e: return str(e)
Example #28
Source File: cloud_generator.py From SIP-2018 with GNU General Public License v3.0 | 5 votes |
def AddFigure(dictionary, plotnum, title): wordcloud = WordCloud().generate_from_frequencies(dictionary) plt.subplot(plotnum) plt.imshow(wordcloud, interpolation='bilinear') plt.title(title) plt.axis("off") #Create a matplotlib figure
Example #29
Source File: generate_wordcloud.py From web_scraping_and_data_analysis with MIT License | 5 votes |
def wordCloud(wordList): from wordcloud import WordCloud, ImageColorGenerator import matplotlib.pyplot as plt import numpy as np import PIL.Image as Image import os d = os.path.dirname(__file__) img = Image.open(os.path.join(d, "jupiter.png")) width = img.width/80 height = img.height/80 alice_coloring = np.array(img) my_wordcloud = WordCloud(background_color="white", max_words=500, mask=alice_coloring, max_font_size=200, random_state=42, font_path=(os.path.join(d, "../font/PingFang.ttc"))) my_wordcloud = my_wordcloud.generate_from_frequencies(wordList) image_colors = ImageColorGenerator(alice_coloring) plt.figure(figsize=(width, height)) plt.imshow(my_wordcloud.recolor(color_func=image_colors)) plt.imshow(my_wordcloud) plt.axis("off") # 通过设置subplots_adjust来控制画面外边框 plt.subplots_adjust(bottom=.01, top=.99, left=.01, right=.99) plt.savefig("jupiter_wordcloud_1.png") plt.show()
Example #30
Source File: helper_wordcloud.py From services-to-wordcloud with MIT License | 5 votes |
def generate_word_cloud(self, fonts, masks, name_prefix='some-wordcloud', bg_color='white'): """ (obj, list, list, str) -> None Generating the word clouds with different masks and fonts and saving it as images. """ if name_prefix is None: name_prefix = 'some-wordcloud' BASE_FOLDER = self.wc_helper.save_dir STOPWORDS = self.STOPWORDS print BASE_FOLDER from scipy.misc import imread for mask_name in masks: _mask_file = imread(masks[mask_name], flatten=True) _mask_width = len(_mask_file[0]) + 1 _mask_height = len(_mask_file) + 1 for font_name in fonts: _font_file = fonts[font_name] _img_name = '%s-%s-%s-%s' % (str(name_prefix), str(font_name), str(mask_name), str(bg_color)) wordcloud = WordCloud( font_path=_font_file, stopwords=STOPWORDS, background_color=bg_color, width=_mask_width, height=_mask_height, mask=_mask_file ).generate(self.words) plt.imshow(wordcloud) plt.axis('off') plt.savefig(BASE_FOLDER + _img_name, dpi=300) print '[i] image %s.png was generated ' % _img_name