"""
Created on Sun Mar 8 11:01:44 2020
@author: toto
"""
import os,jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np
cwd = os.getcwd()
jieba.load_userdict("UserDict.txt")
book = "Solitude.txt"
stop_word = "stop_word.txt"
top_word = "TopWord.txt"
type_pic = "Marquez.jpeg"
cloud = "Solitudecloud.png"
fb = open(book,'r',encoding='UTF-8')
book_txt = fb.read()
ff = open(stop_word,'r',encoding='UTF-8')
stop_word_txt = ff.read()
word_ls = jieba.lcut(book_txt,cut_all=False,HMM=True)
word_dict = {}
for i in word_ls:
if i not in stop_word_txt:
word_dict[i] = word_dict.get(i, 0) + 1
word_ls = sorted(word_dict.items(), key=lambda x: x[1], reverse=True)
print(word_ls[0:101])
top_word = []
for j in range(200):
top_word.append(word_ls[j][0])
top_word_txt = " ".join(top_word)
Marquez_mask = np.array(Image.open(type_pic))
wordcloud_Solitude = WordCloud(
background_color="white",
max_words=200,
mask=Marquez_mask,
font_path = os.path.join(cwd,"SIMKAI.TTF"),
contour_width=3,
min_font_size=2,
)
wordcloud_Solitude.generate(top_word_txt)
wordcloud_Solitude.to_file(cloud)
fb.close()
ff.close()