-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_qa_shuffle.py
More file actions
76 lines (61 loc) · 2.49 KB
/
generate_qa_shuffle.py
File metadata and controls
76 lines (61 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
"""
JSONファイルシャッフルスクリプト
qa_all_1030.jsonを元に、選択肢をシャッフルしたqa_all_shuffle_1030.jsonを作成する
"""
import json
import random
from pathlib import Path
from typing import List, Dict, Any
def shuffle_question(question: Dict[str, Any]) -> Dict[str, Any]:
"""
問題の選択肢をシャッフルし、correct_answerを削除する
"""
shuffled_question = question.copy()
# choice配列が存在する場合のみシャッフル
if 'choice' in shuffled_question and isinstance(shuffled_question['choice'], list):
shuffled_choice = shuffled_question['choice'].copy()
random.shuffle(shuffled_choice)
shuffled_question['choice'] = shuffled_choice
# correct_answerを削除
if 'correct_answer' in shuffled_question:
del shuffled_question['correct_answer']
return shuffled_question
def main():
"""
メイン処理
"""
input_file = Path('qa_all_1030.json')
output_file = Path('qa_all_shuffle_1030.json')
# 入力ファイルの存在確認
if not input_file.exists():
print(f"エラー: {input_file} が見つかりません。")
return
try:
# 入力ファイルを読み込み
print(f"読み込み中: {input_file}")
with open(input_file, 'r', encoding='utf-8') as f:
questions = json.load(f)
if not isinstance(questions, list):
print(f"エラー: {input_file} は配列形式ではありません。")
return
print(f"問題数: {len(questions)}")
# 各問題をシャッフル
shuffled_questions = []
for question in questions:
shuffled_question = shuffle_question(question)
shuffled_questions.append(shuffled_question)
# 出力ファイルに書き込み
print(f"書き込み中: {output_file}")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(shuffled_questions, f, ensure_ascii=False, indent=2)
print(f"\n=== 処理完了 ===")
print(f"入力ファイル: {input_file}")
print(f"出力ファイル: {output_file}")
print(f"シャッフルされた問題数: {len(shuffled_questions)}")
except json.JSONDecodeError as e:
print(f"エラー: {input_file} のJSON解析エラー: {e}")
except Exception as e:
print(f"エラー: 処理中にエラーが発生しました: {e}")
if __name__ == '__main__':
main()