File size: 7,439 Bytes
f2c37cd |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
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)
|