ADD file via upload

main
pmgp6jfbh 3 months ago
parent ea16b69620
commit b8eb1ffd90

@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
import os # 新增导入os模块用于文件检查
class DanmakuAnalyzer:
def __init__(self, danmu_path="./data/all_danmu.txt"):
self.danmu_list = []
# 新增:检查文件是否存在
if not os.path.exists(danmu_path):
print(f"错误:弹幕文件 {danmu_path} 不存在")
return
# 新增:异常处理
try:
with open(danmu_path, "r", encoding="utf-8") as f:
self.danmu_list = [line.strip() for line in f if line.strip()]
except Exception as e:
print(f"读取弹幕文件失败:{e}")
def analyze(self):
"""分析用户观点"""
if not self.danmu_list: # 新增:检查数据是否有效
print("无有效弹幕数据,无法进行分析")
return
categories = {
"应用领域": ["学习", "办公", "编程", "写作"],
"成本": ["免费", "付费", ""],
"担忧": ["失业", "隐私", "错误"]
}
counts = {k:0 for k in categories}
for danmu in self.danmu_list:
for cat, keywords in categories.items():
if any(kw in danmu for kw in keywords):
counts[cat] += 1
print("\n用户观点分析:")
total = len(self.danmu_list)
for cat, cnt in counts.items():
print(f"- {cat}{cnt}次提及(占比{cnt/total:.1%}")
if __name__ == "__main__":
analyzer = DanmakuAnalyzer()
analyzer.analyze()
Loading…
Cancel
Save