From 0397df2accdadc0e912c04dab5f62b8157c3bb24 Mon Sep 17 00:00:00 2001 From: p4oufk2iw <2488672761@qq.com> Date: Wed, 18 Sep 2024 03:43:07 +0800 Subject: [PATCH] ADD file via upload --- count_danmu.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 count_danmu.py diff --git a/count_danmu.py b/count_danmu.py new file mode 100644 index 0000000..13f4fe8 --- /dev/null +++ b/count_danmu.py @@ -0,0 +1,32 @@ +# 统计弹幕次数 + + +def count_danmu(): + # 打开TXT文件以读取数据 + file_path = '弹幕.txt' + + # 初始化一个空的文本字符串,用于累积所有文本数据 + danmu_list = [] + + with open(file_path, 'r', encoding='utf-8') as file: + for line in file: + # 在这里处理每一行的数据 + # 示例:将每一行的弹幕添加到danmu_list列表中 + danmu_list.append(line.strip()) + + # 使用Counter统计弹幕出现次数 + danmu_counter = Counter(danmu_list) + # 筛选与AI技术应用相关的弹幕 + ai_danmu_counter = {k: v for k, v in danmu_counter.items() if 'AI' in k or '人工智能' in k} + # 将筛选后的弹幕转换为Counter对象 + ai_danmu_counter = Counter(ai_danmu_counter) + # 获取AI技术应用方面数量排名前8的弹幕 + top_8_ai_danmus = ai_danmu_counter.most_common(8) + + # 打印排名前8的AI技术应用方面的弹幕及其出现次数 + for idx, (danmu, count) in enumerate(top_8_ai_danmus, 1): + print(f'排名 #{idx}: 弹幕 "{danmu}" 出现次数:{count}') + #top_76016_danmus = danmu_counter.most_common(76016) + # 将AI技术应用方面的统计数据写入Excel + df = pd.DataFrame(list(ai_danmu_counter.items()), columns=['弹幕', '次数']) + df.to_excel('AI技术应用弹幕统计.xlsx', index=False) \ No newline at end of file