import re
import jieba
import wordcloud
import sqlite3
import cv2
if __name__ == '__main__':
# connect database
conn = sqlite3.connect("C:/Users/lidy/Desktop/message_4.sqlite")
c = conn.cursor()
# data = c.execute("select name from sqlite_master where type='table' order by name")
# tabelname = "Chat_614b0f556ffe38704b6d4f0fc56df968"
# search = c.execute("select Message from Chat_54936638f4055d1cb530968188ec3754 where CreateTime > '1640966400'").fetchall()
search = c.execute("select Message from Chat_0c704438265400a21a7bb7c6e2350721 where CreateTime > '1640966400'").fetchall()
# for each message: cut into words
wordList = []
for each in search:
find = re.findall(r'[\u4e00-\u9fa5]+', each[0])
for sentence in find:
cutResult = jieba.cut(sentence, cut_all=False, HMM=True)
for word in cutResult:
wordList.append(word)
# set Stopword list
excludes = ['test']
# get list of needed words
result = []
for word in wordList:
if len(word) == 1:
continue
elif word in excludes:
continue
else:
result.append(word)
result = ' '.join(result)
# generate wordcloud
mask_jpg = cv2.imread("C:/Users/lidy/Desktop/test.jpg")
w = wordcloud.WordCloud(mask=mask_jpg, font_path='C:/Windows/Fonts/方正粗黑宋简体.ttf',
width=600, height=525, max_words=400, collocations=False,
max_font_size=110, background_color='black')
w.generate(result)
# set color2.jpg's colormap as the cloud's colormap
color_jpg = cv2.imread("C:/Users/lidy/Desktop/20190521234918909.jpg")
img_color = wordcloud.ImageColorGenerator(color_jpg)
w.recolor(color_func=img_color)
# write to file
w.to_file('images/result.png')