ADD file via upload

main
piw4f8lbj 2 months ago
parent 37bb209fee
commit d94d4794da

@ -0,0 +1,36 @@
import os
import re
from collections import Counter
import pandas as pd
#提取txt文件里面的弹幕并存于列表中
def read_danmu_files(directory):
danmu_data = []
for filename in os.listdir(directory):
if filename.endswith('paris_olympics_danmak.txt'):
with open(os.path.join(directory, filename), 'r', encoding='utf-8') as file:
danmu_data.extend(file.readlines())
return danmu_data
def count_danmu():
danmu_data = read_danmu_files('./') # 当前目录下
#用正则表达式提取含有ai的弹幕
ai_danmaku = [danmaku for danmaku in danmu_data if
re.search(r'(?<![a-zA-Z])((?:AI|人工智能))(?![a-zA-Z])', danmaku)]
unique_ai_danmaku = list(set(ai_danmaku))
#获取与ai弹幕相关的数量
danmaku_counts = [(danmaku, ai_danmaku.count(danmaku)) for danmaku in unique_ai_danmaku]
#对与ai相关弹幕按照数量数量进行排序
sorted_danmaku_counts = sorted(danmaku_counts, key=lambda x: x[1], reverse=True)
#得出排名前八的弹幕
top_8_danmaku = sorted_danmaku_counts[:8]
#将数据导入excel表
data = {'弹幕内容': [danmaku[0] for danmaku in top_8_danmaku]}
df = pd.DataFrame(data)
df.to_excel('2025巴黎奥运AI弹幕统计.xlsx', index=False)
Loading…
Cancel
Save