59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
|
||
import jieba
|
||
import jieba.posseg as pseg
|
||
import pandas as pd
|
||
from wordcloud import WordCloud
|
||
import matplotlib.pyplot as plt
|
||
|
||
from ..file import LocalFileManager
|
||
from ..chat_session import ChatSessionManager
|
||
|
||
chat_session = ChatSessionManager()
|
||
data = chat_session.get_chat_history("2025-10-11 00:00:00","2025-10-18 23:59:59")
|
||
lfm = LocalFileManager("../chat_history")
|
||
|
||
# 加载用户词典(使用正确的路径)
|
||
jieba.load_userdict("../chat_history/userdict.txt")
|
||
|
||
# 获取停用词
|
||
skip_words_content = lfm.get_file("stopwords.txt").decode('utf-8')
|
||
skip_words = skip_words_content.splitlines() + [" ", '\n']
|
||
# 生成词频字典(需提前完成分词统计)
|
||
|
||
if __name__ == '__main__':
|
||
|
||
words = {}
|
||
for i in data:
|
||
for m in pseg.cut(i[1]):
|
||
word = m.word
|
||
flag = m.flag
|
||
# 正确的比较逻辑:使用word而不是m对象
|
||
if word not in skip_words and flag not in ["a", "b", "c", "d", "e", "f","r","nrt"] and len(word) > 1:
|
||
if flag == "eng" and len(word) >= 24:
|
||
print("skip")
|
||
else:
|
||
if word not in words:
|
||
words[word] = (1,flag)
|
||
else:
|
||
words[word] = (words[word][0] + 1,flag)
|
||
# print("分词结果统计:")
|
||
# for word, count in sorted(words.items(), key=lambda x: x[1], reverse=True):
|
||
# print(f"{word}: {count}")
|
||
df = pd.DataFrame([[word,count[0],count[1]] for word,count in words.items()],columns=["word","count","flag"])
|
||
df.to_csv("../chat_history/wordcloud.csv",index=False)
|
||
word_freq = {word:count[0] for word,count in words.items()}
|
||
# 创建词云对象
|
||
wc = WordCloud(
|
||
font_path='msyh.ttc', # 微软雅黑字体文件路径
|
||
width=800,
|
||
height=600,
|
||
background_color='white',
|
||
max_words=100
|
||
)
|
||
# 生成词云
|
||
wc.generate_from_frequencies(word_freq)
|
||
# 显示图形
|
||
plt.figure(figsize=(10, 6))
|
||
plt.imshow(wc, interpolation='bilinear')
|
||
plt.axis('off')
|
||
plt.savefig("../chat_history/wordcloud.png") |