| 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 |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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: |
| |
| 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__": |
| |
| |
| |
| |
| file_path = "/run/determined/NAS1/public/lixinyuan/interleaved-umm/data/questions/task1/train/train_1.jsonl" |
|
|
| analyze_angles_in_jsonl(file_path) |