def generate_wordcloud_image(fname, msglist, enable_mask=False): cloud_words = [] global exclude_words global sugguest_words jieba.suggest_freq(sugguest_words, tune=True) for msg in msglist: words = list(jieba.cut(msg, HMM=False)) for word in words: if (word not in exclude_words) and len(word) > 1: # if 'iO' in word: # print(f"### {word}") cloud_words.append(word.strip()) msg_text = ",".join(cloud_words) wc: WordCloud = None font_file = "./SupportFiles/simsun.ttf" if enable_mask: cloud_mask = np.array(Image.open("./SupportFiles/heart.jpg")) wc = WordCloud(mask=cloud_mask, background_color="white", max_words=200, font_path=font_file) else: wc = WordCloud( background_color="white", max_words=200, font_path=font_file, width=800, height=600, ) wc.collocations = False # with open("./words.txt", "w") as text_file: # text_file.write(msg_text) wc.generate(msg_text) now = datetime.now() timestamp = int(datetime.timestamp(now)) imagedir = "./images" if not os.path.exists(imagedir): os.makedirs(imagedir) output_file = f"{imagedir}/{fname}_{timestamp}.png" wc.to_file(output_file) # print(wc.words_) print(f'export cloud image {output_file} success.')
def create_wordcloud(raw_data, title, bigrams=True): ''' Generate Word Cloud -- Word_Cloud Library: https://github.com/amueller/word_cloud ''' # Black to Grey def custom_color_func(word, font_size, position, orientation, random_state=None, **kwargs): return "hsl(0, 0%%, %d%%)" % random.randint(10, 60) d = path.dirname(__file__) wordcloud = WordCloud(font_path=path.join(d, "fonts/raleway/raleway-light.ttf"), width=500, height=500, margin=20, background_color='white', max_words=30, stopwords=STOP_WORDS, color_func=custom_color_func, random_state=20) wordcloud.collocations = bigrams wordcloud.generate(raw_data) plt.imshow(wordcloud) wordcloud.to_file("word_clouds/" + title + ".png") print ('Created Word Cloud')