From 7f9b9a41224aa78d25528d136f61dccf308874f6 Mon Sep 17 00:00:00 2001 From: pucvkrxa8 <773824858@qq.com> Date: Thu, 12 Sep 2024 14:14:00 +0800 Subject: [PATCH] ADD file via upload --- 筛选AI关键字和词云图.py | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 筛选AI关键字和词云图.py diff --git a/筛选AI关键字和词云图.py b/筛选AI关键字和词云图.py new file mode 100644 index 0000000..d88eb1d --- /dev/null +++ b/筛选AI关键字和词云图.py @@ -0,0 +1,35 @@ +from wordcloud import WordCloud +from collections import Counter +import matplotlib.pyplot as plt + +final_danmu=[]#所有弹幕合集 +final_danmu_ai=[]#所有ai有关弹幕合计 +for num in range(1,301):#遍历爬好的弹幕 + text_name=f"第{num}个视频弹幕.txt" + danmu=[] + key=[ + '人工智能','AI' + ] + try: + with open(f"{text_name}",mode='r',encoding='utf-8') as f: + for i in f: + danmu.append(i.strip()) + danmu_ai=[i for i in danmu if any(j in i for j in key)] + final_danmu_ai+=danmu_ai + final_danmu+=danmu + except: + continue +aicount=Counter(final_danmu_ai) +danmucount=Counter(final_danmu) +#分别计数 +for i in aicount.most_common(8):#AI有关弹幕排名前8 + print(f"弹幕:{i[0]}\n次数:{i[1]}\n") +wordcloud = WordCloud(width=800, height=400, background_color='white', + font_path='msyh.ttc', # 设置字体路径,可以根据系统字体路径调整 + max_words=100, colormap='viridis').generate_from_frequencies(danmucount) + +# 显示词云图 +plt.figure(figsize=(10, 5)) +plt.imshow(wordcloud, interpolation='bilinear') +plt.axis('off') # 不显示坐标轴 +plt.show() \ No newline at end of file