# encoding=utf-8 """词云""" import jieba.analyse from PIL import Image, ImageSequence import numpy as np import matplotlib.pyplot as plt from wordcloud import WordCloud, ImageColorGenerator import os # lyric = '' # f = open('./01.txt', 'r','utf-8') # for i in f: # lyric += f.read() current_dir = os.path.abspath('.') file_name2 = os.path.join(current_dir, '2.txt') f2 = open(file_name2, encoding="UTF8") all=f2.readlines() lyric = '' for item in all: lyric+=item result = jieba.analyse.textrank(lyric, topK=50, withWeight=True, allowPOS=('ns', 'n', 'vn', 'v')) print(result) keywords = dict() for i in result: keywords[i[0]] = i[1] # print(keywords) image = Image.open('./tim.jpg') graph = np.array(image) wc = WordCloud(font_path='./fonts/simhei.ttf', background_color='White', max_words=50, mask=graph) wc.generate_from_frequencies(keywords) image_color = ImageColorGenerator(graph) plt.imshow(wc) plt.imshow(wc.recolor(color_func=image_color)) plt.axis("off") plt.show() wc.to_file('dream.png') # from pytagcloud import create_tag_image, make_tags # from pytagcloud.lang.counter import get_tag_counts # import webbrowser # import collections # # YOUR_TEXT = "A tag cloud is a visual representation for text data, typically\ # used to depict keyword metadata on websites, or to visualize free form text." # # tags = make_tags(get_tag_counts(YOUR_TEXT), maxsize=80) # # create_tag_image(tags, 'cloud_large.png', size=(900, 600), fontname='Lobster') # # # # webbrowser.open('cloud_large.png') # see results
python词云生成
最新推荐文章于 2024-06-04 06:56:40 发布