import json import re from collections import Counter import os def analyze_angles_in_jsonl(file_path): """ 统计 JSONL 文件中 question 字段里角度出现的次数。 针对两种特定模板进行了优化。 """ if not os.path.exists(file_path): print(f"错误: 找不到文件 {file_path}") return angles = [] total_lines = 0 unmatched_count = 0 # --- 针对你的两个模板修改后的正则表达式 --- # 解释: # 1. (?: ... ) : 非捕获组,用于逻辑“或” # 2. rotates : 匹配模板 1 中的 "... camera rotates 81 degrees" # 3. | : 或者 # 4. move\s+the\s+camera : 匹配模板 2 中的 "... move the camera 81 degrees" # 5. \s+ : 匹配中间的空格 # 6. (\d+) : 捕获组,提取角度数字 # 7. \s+degrees : 匹配后缀 " degrees" pattern = re.compile(r"(?:rotates|move\s+the\s+camera)\s+(\d+)\s+degrees", re.IGNORECASE) print(f"正在处理文件: {file_path} ...") try: with open(file_path, 'r', encoding='utf-8') as f: for line_number, line in enumerate(f, 1): line = line.strip() if not line: continue total_lines += 1 try: data = json.loads(line) question = data.get('question', '') match = pattern.search(question) if match: # group(1) 是捕获到的数字 angle = int(match.group(1)) angles.append(angle) else: unmatched_count += 1 # 仍然保留调试打印,以防万一还有第三种情况 if unmatched_count <= 5: print(f"\n[未匹配样本 #{unmatched_count} - 行 {line_number}]") # 打印出关键部分上下文,方便查看 print(f"Question片段: ...{question[100:300]}...") except json.JSONDecodeError: print(f"警告: 第 {line_number} 行不是有效的 JSON 数据") except Exception as e: print(f"读取文件时发生错误: {e}") return # 统计频率 angle_counts = Counter(angles) # 输出结果 print("\n" + "="*30) print("统计结果 (角度: 出现次数)") print("="*30) if not angle_counts: print("未提取到任何角度数据。") else: sorted_counts = sorted(angle_counts.items(), key=lambda x: x[0]) for angle, count in sorted_counts: print(f"{angle}° : {count} 次") print("="*30) print(f"总行数: {total_lines}") print(f"成功提取: {len(angles)}") print(f"未匹配到: {unmatched_count}") if unmatched_count == 0: print("\n完美!所有数据都已成功匹配。") else: print("\n注意:仍有部分数据未匹配,请检查上方日志。") if __name__ == "__main__": # 你可以直接在这里修改文件名,或者通过命令行参数传入 # 假设你的文件名为 data.jsonl # 方式 1: 直接修改这里的文件路径 file_path = "/run/determined/NAS1/public/lixinyuan/interleaved-umm/data/questions/task1/train/train_1.jsonl" analyze_angles_in_jsonl(file_path)