import json import re import PyPDF2 class Entry: def __init__(self, question: str, options: list[str], answer_type: str, description: str = "", right_answer: str = ""): """ Initialize an Entry object. :param question: The question text :param options: A list of possible answers :param right_answer: The correct answer from the options :param answer_type: Type of question (e.g., "multiple-choice", "single-choice") :param description: A short description of the entry """ self.question = question self.options = options self.right_answer = right_answer self.type = answer_type self.description = description def __str__(self): """String representation of the entry.""" return ( f"Question: {self.question}\n" f"Options: {', '.join(self.options)}\n" f"Correct Answer: {self.right_answer}\n" f"Type: {self.type}\n" f"Description: {self.description}" ) def discard_beginning(text_lines): # Find the line starting with '1.' start_index = None for index, line in enumerate(text_lines): if line.strip().startswith("1."): start_index = index break # Return the content starting from the found index if start_index is not None: return text_lines[start_index:] else: return [] def read_pdf(filename: str): pdf_reader = PyPDF2.PdfReader(filename) pages = [str(page.extract_text()) for page in pdf_reader.pages] text_lines = ('\n'.join(pages)).split('\n') return text_lines def process_file(filename: str): text_lines = read_pdf(filename) text_lines = discard_beginning(text_lines) entries = [] line_index = 0 single_choice = True finished = False next_number = 1 while line_index < len(text_lines) and not finished: line = text_lines[line_index] if single_choice: match = re.match(r"^(\d+)\. ", line.strip()) if match and next_number - 7 <= int(match[0][:-2]) <= next_number + 7: next_number += 1 question = "" while line.strip()[:2] != "A." and line_index < len(text_lines) - 1: question += line line_index += 1 line = text_lines[line_index] optionA = "" while line.strip()[:2] != "B." and line_index < len(text_lines) - 1: optionA += line line_index += 1 line = text_lines[line_index] optionB = "" while line.strip()[:2] != "C." and line_index < len(text_lines) - 1: optionB += line line_index += 1 line = text_lines[line_index] optionC = "" while line.strip()[:2] != "D." and line_index < len(text_lines) - 1: optionC += line line_index += 1 line = text_lines[line_index] optionD = "" while (not re.match(r"^\d+\. ", line.strip())) and (len(line.strip()) > 2): optionD += line line_index += 1 line = text_lines[line_index] options = [optionA, optionB, optionC, optionD] entry = Entry(question, options, "single-choice") entries.append(entry) if len(entries) == 30: single_choice = False else: line_index += 1 else: match = re.match(r"^(\d\d)\. ", line.strip()) if match and next_number - 7 <= int(match[0][:-2]) <= next_number + 7: next_number += 1 question = "" while line.strip()[:2] != "1." and line_index < len(text_lines) - 1: question += line line_index += 1 line = text_lines[line_index] option1 = "" while line.strip()[:2] != "2." and line_index < len(text_lines) - 1: option1 += line line_index += 1 line = text_lines[line_index] option2 = "" while line.strip()[:2] != "3." and line_index < len(text_lines) - 1: option2 += line line_index += 1 line = text_lines[line_index] option3 = "" while line.strip()[:2] != "4." and line_index < len(text_lines) - 1: option3 += line line_index += 1 line = text_lines[line_index] option4 = "" while (not re.match(r"^\d\d\. ", line.strip())) and (len(line.strip()) > 2): option4 += line line_index += 1 line = text_lines[line_index] options = [option1, option2, option3, option4] entry = Entry(question, options, "multiple-choice") entries.append(entry) if len(entries) == 60: finished = True else: line_index += 1 return entries def process_answer_file(filename, entries): text_lines = read_pdf(filename) text_lines = discard_beginning(text_lines) line_index = 0 while line_index < len(text_lines): line = text_lines[line_index] matches = re.findall(r'(\d+)\.\s*([A-Z])', line) for number, answer in matches: if int(number) <= 60: entries[int(number) - 1].right_answer = answer line_index += 1 return entries def add_description(filename, entries): description = re.search(r'(?<=data\\)(.*?)(?=\\subiect)', filename)[0] for entry in entries: entry.description = description return entries def save_as_json(filename, entries): entries_dict = [entry.__dict__ for entry in entries] with open(filename, 'w') as file: json.dump(entries_dict, file, indent=4) question_paths = [ "data\\IX\\nationala\\teoretic\\2014\\subiect_2014.pdf", "data\\IX\\nationala\\teoretic\\2015\\subiect_2015.pdf", "data\\IX\\nationala\\teoretic\\2016\\subiect_2016.pdf", "data\\IX\\nationala\\teoretic\\2018\\subiect_2018.pdf", "data\\IX\\nationala\\teoretic\\2019\\subiect_2019.pdf", "data\\IX\\nationala\\teoretic\\2022\\subiect_2022.pdf", "data\\IX\\nationala\\teoretic\\2023\\subiect_2023.pdf", "data\\IX\\nationala\\teoretic\\2024\\subiect_2024.pdf", ] root_path = "C:\\Users\\adela\\Downloads\\data-20241108T235902Z-001" all_data = [] for question_path in question_paths: full_path = root_path + "\\" + question_path parsed_entries = process_file(full_path) answer_path = full_path.replace("subiect", "barem") parsed_entries = process_answer_file(answer_path, parsed_entries) parsed_entries = add_description(question_path, parsed_entries) all_data += parsed_entries save_as_json("questions.json", all_data)