|
|
|
import json |
|
from pathlib import Path |
|
|
|
def map_answer_to_index(answer_letter): |
|
"""Convert answer letter to numerical index (0-based)""" |
|
return {'a': 0, 'b': 1, 'c': 2, 'd': 3}[answer_letter.lower()] |
|
|
|
def find_correct_answer(question): |
|
"""Correct answer is always A / 0""" |
|
return 0 |
|
|
|
def transform_question(question): |
|
"""Transform a single question from original format to target format""" |
|
|
|
|
|
transformed = { |
|
"language": "de", |
|
"country": "Germany", |
|
"file_name": "fragenkatalog3b.json", |
|
"source": "https://www.bundesnetzagentur.de/SharedDocs/Downloads/DE/Sachgebiete/Telekommunikation/Unternehmen_Institutionen/Frequenzen/Amateurfunk/Fragenkatalog/PruefungsfragenZIP.html - Prüfungsfragen zum Erwerb von Amateurfunkprüfungsbescheinigungen, Bundesnetzagentur, 3. Auflage, März 2024, (www.bundesnetzagentur.de/amateurfunk), Datenlizenz Deutschland – Namensnennung – Version 2.0 (www.govdata.de/dl-de/by-2-0)", |
|
"license": "DL-DE->BY-2.0", |
|
"level": f"Official Exam (Class {question['class']})", |
|
"category_en": "Amateur Radio", |
|
"category_original_lang": "Amateurfunk", |
|
"original_question_num": question["number"], |
|
"question": question["question"].strip(), |
|
"parallel_question_id": None, |
|
"image_png": None, |
|
"image_information": None, |
|
"image_type": None |
|
} |
|
|
|
|
|
if any(key.startswith('picture_') for key in question.keys()): |
|
|
|
if 'picture_a' in question: |
|
|
|
transformed["options"] = [ |
|
question['picture_a'] + ".png", |
|
question['picture_b'] + ".png", |
|
question['picture_c'] + ".png", |
|
question['picture_d'] + ".png" |
|
] |
|
else: |
|
|
|
transformed["options"] = [ |
|
question['answer_a'].strip(), |
|
question['answer_b'].strip(), |
|
question['answer_c'].strip(), |
|
question['answer_d'].strip() |
|
] |
|
if 'picture_question' in question: |
|
transformed["image_png"] = question["picture_question"] + ".png" |
|
transformed["image_information"] = "essential" |
|
transformed["image_type"] = "diagram" |
|
else: |
|
|
|
transformed["options"] = [ |
|
question['answer_a'].strip(), |
|
question['answer_b'].strip(), |
|
question['answer_c'].strip(), |
|
question['answer_d'].strip() |
|
] |
|
|
|
|
|
transformed["answer"] = find_correct_answer(question) |
|
|
|
return transformed |
|
|
|
def convert_questions(input_file): |
|
"""Convert all questions from input file to new format""" |
|
with open(input_file, 'r', encoding='utf-8') as f: |
|
data = json.load(f) |
|
|
|
transformed_questions = [] |
|
|
|
def process_section(section): |
|
if 'questions' in section: |
|
for q in section['questions']: |
|
transformed_questions.append(transform_question(q)) |
|
if 'sections' in section: |
|
for subsection in section['sections']: |
|
process_section(subsection) |
|
|
|
process_section(data) |
|
|
|
return transformed_questions |
|
|
|
def main(): |
|
input_file = Path('fragenkatalog3b.json') |
|
output_file = input_file.with_stem(input_file.stem + '_transformed') |
|
|
|
transformed = convert_questions(input_file) |
|
|
|
with open(output_file, 'w', encoding='utf-8') as f: |
|
json.dump(transformed, f, ensure_ascii=False, indent=2) |
|
|
|
if __name__ == "__main__": |
|
main() |