mirror of
https://github.com/WMK965/965-Python-Learning-Repo.git
synced 2025-04-27 14:03:22 +00:00
扩展Wordcloud_GUI项目功能
This commit is contained in:
parent
d7307bcf3d
commit
559c66b769
39
Wordcloud_GUI/WcModule.py
Normal file
39
Wordcloud_GUI/WcModule.py
Normal file
@ -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", {'王勃', '一'})
|
Loading…
Reference in New Issue
Block a user