1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
from collections import Counter from zhon import hanzi import string import re import os import matplotlib.pyplot as plt import jieba from wordcloud import WordCloud, ImageColorGenerator
with open(‘mysql必知應會.txt‘,‘r‘, encoding=‘utf-8‘) as fp: alltext = ‘ ‘.join(fp.readlines())
alltext = re.sub("[%s]+" % hanzi.punctuation, " ", alltext) alltext = re.sub("[%s]+" % string.punctuation, " ", alltext)
seg_list = jieba.cut(alltext, cut_all=False) seg_list = list(seg_list)
counter = Counter(seg_list) top = counter.most_common(1000)
top_dict = dict(top)
stopwords_list = [] pwd = os.path.abspath(‘.‘) for file in os.listdir(os.path.join(pwd,‘stopwords‘)): filename = os.path.join(pwd,‘stopwords‘,file) with open(filename,‘r‘) as fp: stopwords_list.extend(fp.readlines())
stopwords_list = list(set(stopwords_list))
for stopword in stopwords_list: try: top_dict.pop(stopword.strip()) except: pass
wc = WordCloud(background_color="white", #背景顏色max_words=2000,# 詞雲顯示的最大詞數 font_path=‘simfang.ttf‘, max_font_size=50, #字型最大值 random_state=42)
wc.generate_from_frequencies(top_dict)
plt.imshow(wc) plt.axis(‘off‘)
plt.show() |