You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.8 KiB
55 lines
1.8 KiB
from PIL import Image
|
|
import jieba
|
|
from wordcloud import WordCloud
|
|
import numpy as np
|
|
import threading
|
|
|
|
mask1 = np.array(Image.open("C:\\Users\\lenovo\\Desktop\\qqmusic.jpg"))
|
|
mask2 = np.array(Image.open("C:\\Users\\lenovo\\Desktop\\qqmusicmask2.jpg"))
|
|
mask3 = np.array(Image.open("C:\\Users\\lenovo\\Desktop\\qqmusicmask3.png"))
|
|
|
|
def get_wordcloud(url, output_name, background_color, mask):
|
|
with open(url, 'r', encoding='utf-8') as fb:
|
|
t = fb.read()
|
|
|
|
ls = jieba.lcut(t)
|
|
|
|
# 去除一些常见词语
|
|
stopwords = {'的', '是', '在', '和', '等'}
|
|
unique_ls = [word for word in ls if word not in stopwords]
|
|
|
|
txt = ' '.join(unique_ls)
|
|
w = WordCloud(font_path='msyh.ttc', mask=mask, collocations=False, width=1200, height=1100,
|
|
background_color=background_color,
|
|
max_words=200, contour_color='steelblue', contour_width=2)
|
|
w.generate(txt)
|
|
|
|
w.to_file(output_name)
|
|
|
|
# 创建线程类
|
|
class MyThread(threading.Thread):
|
|
def __init__(self, url, output_name, background_color, mask):
|
|
threading.Thread.__init__(self)
|
|
self.url = url
|
|
self.output_name = output_name
|
|
self.background_color = background_color
|
|
self.mask = mask
|
|
|
|
def run(self):
|
|
get_wordcloud(self.url, self.output_name, self.background_color, self.mask)
|
|
|
|
# 创建线程对象
|
|
thread1 = MyThread("C:\\Users\\lenovo\\Desktop\\内地词云.txt", "内地词云.png", 'lightcyan', mask1)
|
|
thread2 = MyThread("C:\\Users\\lenovo\\Desktop\\香港词云.txt", "香港词云.png", 'pink', mask2)
|
|
thread3 = MyThread("C:\\Users\\lenovo\\Desktop\\欧美词云.txt", "欧美词云.png", 'yellow', mask3)
|
|
|
|
# 启动线程
|
|
thread1.start()
|
|
thread2.start()
|
|
thread3.start()
|
|
|
|
# 等待线程结束
|
|
thread1.join()
|
|
thread2.join()
|
|
thread3.join()
|