parent
0e72470f4a
commit
54e40721e4
@ -1,35 +0,0 @@
|
|||||||
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()
|
|
Loading…
Reference in new issue