import json from collections import Counter, OrderedDict import matplotlib.pyplot as plt # 读取数据 file_path = '/share/project/sunshuang/deep_search/data_for_rl/musique_tagged/final_selected_dataset.json' with open(file_path, 'r') as f: data = json.load(f) # 提取 key_points 并统计每个关键词的出现次数 key_points_counter = Counter() for entry in data: domain_keypoints = entry.get('domain_keypoints', {}) key_points = domain_keypoints.get('key_points', []) key_points_counter.update(key_points) key_points_counter = OrderedDict(sorted(key_points_counter.items(), key=lambda item: item[1], reverse=True)) # 打印统计结果 # print("Key Points Count:") # for key, count in key_points_counter.items(): # print(f"{key}: {count}") # 将统计结果保存为 JSON 文件 key_points_count_file = 'key_points_count.json' with open(key_points_count_file, 'w') as f: json.dump(dict(key_points_counter), f, indent=4) print(f"Key points count saved to '{key_points_count_file}'") # 绘制饼图 labels = [f'{key} ({count})' for key, count in key_points_counter.items()] sizes = list(key_points_counter.values()) plt.figure(figsize=(12, 7)) plt.pie( sizes, labels=labels, autopct='%1.1f%%', startangle=140 ) plt.title('Key Points Distribution') plt.tight_layout() plt.savefig('key_points_distribution_pie.png', bbox_inches='tight') plt.close() print("Pie chart saved as 'key_points_distribution_pie.png'")