import os from textblob import TextBlob import pandas as pd directory = 'E:\\前端\\软件工程\\莎莎和陈梦\\弹幕收集' #合并所有文件的文本内容 all_text = "" for filename in os.listdir(directory): if filename.endswith('.txt'): filepath = os.path.join(directory, filename) with open(filepath, 'r', encoding='utf-8') as file: all_text += file.read() + "\n" #将合并后的文本保存为一个新的文件 merged_file_path = os.path.join(directory, 'merged_text.txt') with open(merged_file_path, 'w', encoding='utf-8') as file: file.write(all_text) #定义情感分析函数 def analyze_sentiment(text): blob = TextBlob(text) return blob.sentiment.polarity, blob.sentiment.subjectivity #对合并的文本进行情感分析 polarity, subjectivity = analyze_sentiment(all_text) #打印分析结果 print(f"弹幕的情感极性: {polarity}")#-1 表示非常负面,0 表示中立,1 表示非常正面 print(f"弹幕的情感主观性: {subjectivity}")#0 表示完全客观,1 表示完全主观 #保存分析结果到CSV文件 results = { 'Filename': ['merged_text.txt'], 'Polarity': [polarity], 'Subjectivity': [subjectivity] } results_df = pd.DataFrame(results) results_df.to_csv(os.path.join(directory, 'sentiment_analysis_results.csv'), index=False)