From 559c66b769b93434e6f763f72a399302fb37c58e Mon Sep 17 00:00:00 2001 From: 965 Date: Sun, 28 May 2023 22:32:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95Wordcloud=5FGUI=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Wordcloud_GUI/WcModule.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Wordcloud_GUI/WcModule.py diff --git a/Wordcloud_GUI/WcModule.py b/Wordcloud_GUI/WcModule.py new file mode 100644 index 0000000..0a97d17 --- /dev/null +++ b/Wordcloud_GUI/WcModule.py @@ -0,0 +1,39 @@ +import jieba +import wordcloud +from PIL import Image +import numpy as np +import matplotlib.pyplot as plt + +size = None # 210 +maxword = None # 75 +font = None # "AdobeHeitiStd-Regular.otf" +file = None # "../resources/111.txt" +mask = None # "../resources/mask.png" +stopwords = None # {'王勃', '一'} +codec = None + + +def generate(size, maxword, font, file, mask, stopwords, codec): + raw_data = open(file, encoding=f"{codec}").read() + ls = jieba.lcut(raw_data) + for word in ls: + if len(word) == 1: + ls.remove(word) + else: + continue + text = ' '.join(ls) + open(file, encoding="utf-8").close() + mask = np.array(Image.open(mask)) + wc = wordcloud.WordCloud(font_path=font, + mask=mask, + background_color='white', + max_font_size=size, + max_words=maxword, + stopwords=stopwords) + wc.generate(text) + plt.imshow(wc) + plt.axis("off") + plt.show() + + +#generate(210, 75, "AdobeHeitiStd-Regular.otf", "C:\\Users\\965\\Builds\PythonLearning\\resources\\333.txt", "../resources/mask.png", {'王勃', '一'})