|
|
@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# 作者:Halcyon(王思平102201544)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merge_csv_to_excel(folder_path, output_file):
|
|
|
|
|
|
|
|
# 1、合并所有弹幕csv文件为一个excel文件
|
|
|
|
|
|
|
|
all_data = pd.DataFrame()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 2、遍历文件夹中的所有文件
|
|
|
|
|
|
|
|
for file_name in os.listdir(folder_path):
|
|
|
|
|
|
|
|
if file_name.endswith('.csv'): # 只处理csv文件
|
|
|
|
|
|
|
|
file_path = os.path.join(folder_path, file_name) # 获取完整路径
|
|
|
|
|
|
|
|
print(f"正在读取文件: {file_path}")
|
|
|
|
|
|
|
|
# 读取csv文件并追加到all_data中
|
|
|
|
|
|
|
|
df = pd.read_csv(file_path)
|
|
|
|
|
|
|
|
all_data = pd.concat([all_data, df], ignore_index=True) # 合并DataFrame
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 3、将合并后的DataFrame保存到新的excel文件
|
|
|
|
|
|
|
|
with pd.ExcelWriter(output_file, engine='xlsxwriter') as writer:
|
|
|
|
|
|
|
|
all_data.to_excel(writer, index=False, sheet_name='MergedData')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(f"所有弹幕csv文件已合并并保存到: {output_file}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def analyze_danmu(input_file, output_file):
|
|
|
|
|
|
|
|
# 1、分析合并后的excel文件,统计与AI相关的弹幕数量
|
|
|
|
|
|
|
|
df = pd.read_excel(input_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 2、假设弹幕内容在名为 '弹幕文本' 的列中
|
|
|
|
|
|
|
|
if '弹幕文本' not in df.columns:
|
|
|
|
|
|
|
|
print("弹幕数据列未找到,请检查列名。")
|
|
|
|
|
|
|
|
print("读取的列名:", df.columns.tolist()) # 打印出读取的列名以帮助调试
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 3、定义与 AI 技术应用相关的关键词
|
|
|
|
|
|
|
|
ai_keywords = ['AI', '人工智能', '机器学习', '深度学习']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 4、筛选包含 AI 相关关键词的弹幕
|
|
|
|
|
|
|
|
filtered_danmu = df[df['弹幕文本'].str.contains('|'.join(ai_keywords), na=False)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 5、统计每种弹幕出现的次数
|
|
|
|
|
|
|
|
danmu_counts = filtered_danmu['弹幕文本'].value_counts()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 6、获取数量排名前8的弹幕
|
|
|
|
|
|
|
|
top_danmu = danmu_counts.head(8)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 7、输出结果到控制台
|
|
|
|
|
|
|
|
print("数量排名前8的弹幕:")
|
|
|
|
|
|
|
|
print(top_danmu)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 8、将结果写入ai.xlsx文件
|
|
|
|
|
|
|
|
top_danmu_df = top_danmu.reset_index()
|
|
|
|
|
|
|
|
top_danmu_df.columns = ['弹幕文本', '出现次数'] # 重命名列
|
|
|
|
|
|
|
|
with pd.ExcelWriter(output_file, engine='xlsxwriter') as writer:
|
|
|
|
|
|
|
|
top_danmu_df.to_excel(writer, index=False, sheet_name='TopDanmu')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(f"分析结果已保存到: {output_file}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
folder_path = '弹幕csv' # 弹幕csv文件夹路径
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output_folder = 'output' # 指定输出文件夹
|
|
|
|
|
|
|
|
if not os.path.exists(output_folder): # 如果文件夹不存在就创建它
|
|
|
|
|
|
|
|
os.makedirs(output_folder)
|
|
|
|
|
|
|
|
output_file = os.path.join(output_folder, '合并弹幕.xlsx')
|
|
|
|
|
|
|
|
output_ai_file = os.path.join(output_folder, 'ai.xlsx')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
merge_csv_to_excel(folder_path, output_file) # 合并csv文件为excel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
analyze_danmu(output_file, output_ai_file) # 分析弹幕数据并保存结果
|