AdelaCorbeanu commited on
Commit
f2c37cd
·
verified ·
1 Parent(s): ea11826

Add initial script and data

Browse files
Files changed (2) hide show
  1. main.py +220 -0
  2. questions.json +0 -0
main.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import re
3
+ import PyPDF2
4
+
5
+
6
+ class Entry:
7
+ def __init__(self, question: str, options: list[str], answer_type: str, description: str = "", right_answer: str = ""):
8
+ """
9
+ Initialize an Entry object.
10
+
11
+ :param question: The question text
12
+ :param options: A list of possible answers
13
+ :param right_answer: The correct answer from the options
14
+ :param answer_type: Type of question (e.g., "multiple-choice", "single-choice")
15
+ :param description: A short description of the entry
16
+ """
17
+ self.question = question
18
+ self.options = options
19
+ self.right_answer = right_answer
20
+ self.type = answer_type
21
+ self.description = description
22
+
23
+ def __str__(self):
24
+ """String representation of the entry."""
25
+ return (
26
+ f"Question: {self.question}\n"
27
+ f"Options: {', '.join(self.options)}\n"
28
+ f"Correct Answer: {self.right_answer}\n"
29
+ f"Type: {self.type}\n"
30
+ f"Description: {self.description}"
31
+ )
32
+
33
+
34
+ def discard_beginning(text_lines):
35
+ # Find the line starting with '1.'
36
+ start_index = None
37
+ for index, line in enumerate(text_lines):
38
+ if line.strip().startswith("1."):
39
+ start_index = index
40
+ break
41
+
42
+ # Return the content starting from the found index
43
+ if start_index is not None:
44
+ return text_lines[start_index:]
45
+ else:
46
+ return []
47
+
48
+
49
+ def read_pdf(filename: str):
50
+ pdf_reader = PyPDF2.PdfReader(filename)
51
+ pages = [str(page.extract_text()) for page in pdf_reader.pages]
52
+ text_lines = ('\n'.join(pages)).split('\n')
53
+
54
+ return text_lines
55
+
56
+
57
+ def process_file(filename: str):
58
+ text_lines = read_pdf(filename)
59
+ text_lines = discard_beginning(text_lines)
60
+
61
+ entries = []
62
+
63
+ line_index = 0
64
+ single_choice = True
65
+ finished = False
66
+ next_number = 1
67
+ while line_index < len(text_lines) and not finished:
68
+ line = text_lines[line_index]
69
+
70
+ if single_choice:
71
+ match = re.match(r"^(\d+)\. ", line.strip())
72
+ if match and next_number - 7 <= int(match[0][:-2]) <= next_number + 7:
73
+ next_number += 1
74
+ question = ""
75
+ while line.strip()[:2] != "A." and line_index < len(text_lines) - 1:
76
+ question += line
77
+ line_index += 1
78
+ line = text_lines[line_index]
79
+
80
+ optionA = ""
81
+ while line.strip()[:2] != "B." and line_index < len(text_lines) - 1:
82
+ optionA += line
83
+ line_index += 1
84
+ line = text_lines[line_index]
85
+
86
+ optionB = ""
87
+ while line.strip()[:2] != "C." and line_index < len(text_lines) - 1:
88
+ optionB += line
89
+ line_index += 1
90
+ line = text_lines[line_index]
91
+
92
+ optionC = ""
93
+ while line.strip()[:2] != "D." and line_index < len(text_lines) - 1:
94
+ optionC += line
95
+ line_index += 1
96
+ line = text_lines[line_index]
97
+
98
+ optionD = ""
99
+ while (not re.match(r"^\d+\. ", line.strip())) and (len(line.strip()) > 2):
100
+ optionD += line
101
+ line_index += 1
102
+ line = text_lines[line_index]
103
+
104
+ options = [optionA, optionB, optionC, optionD]
105
+ entry = Entry(question, options, "single-choice")
106
+ entries.append(entry)
107
+
108
+ if len(entries) == 30:
109
+ single_choice = False
110
+
111
+ else:
112
+ line_index += 1
113
+
114
+ else:
115
+ match = re.match(r"^(\d\d)\. ", line.strip())
116
+ if match and next_number - 7 <= int(match[0][:-2]) <= next_number + 7:
117
+ next_number += 1
118
+ question = ""
119
+ while line.strip()[:2] != "1." and line_index < len(text_lines) - 1:
120
+ question += line
121
+ line_index += 1
122
+ line = text_lines[line_index]
123
+
124
+ option1 = ""
125
+ while line.strip()[:2] != "2." and line_index < len(text_lines) - 1:
126
+ option1 += line
127
+ line_index += 1
128
+ line = text_lines[line_index]
129
+
130
+ option2 = ""
131
+ while line.strip()[:2] != "3." and line_index < len(text_lines) - 1:
132
+ option2 += line
133
+ line_index += 1
134
+ line = text_lines[line_index]
135
+
136
+ option3 = ""
137
+ while line.strip()[:2] != "4." and line_index < len(text_lines) - 1:
138
+ option3 += line
139
+ line_index += 1
140
+ line = text_lines[line_index]
141
+
142
+ option4 = ""
143
+ while (not re.match(r"^\d\d\. ", line.strip())) and (len(line.strip()) > 2):
144
+ option4 += line
145
+ line_index += 1
146
+ line = text_lines[line_index]
147
+
148
+ options = [option1, option2, option3, option4]
149
+ entry = Entry(question, options, "multiple-choice")
150
+ entries.append(entry)
151
+
152
+ if len(entries) == 60:
153
+ finished = True
154
+
155
+ else:
156
+ line_index += 1
157
+
158
+ return entries
159
+
160
+
161
+ def process_answer_file(filename, entries):
162
+ text_lines = read_pdf(filename)
163
+ text_lines = discard_beginning(text_lines)
164
+
165
+ line_index = 0
166
+ while line_index < len(text_lines):
167
+ line = text_lines[line_index]
168
+ matches = re.findall(r'(\d+)\.\s*([A-Z])', line)
169
+
170
+ for number, answer in matches:
171
+ if int(number) <= 60:
172
+ entries[int(number) - 1].right_answer = answer
173
+
174
+ line_index += 1
175
+
176
+ return entries
177
+
178
+
179
+ def add_description(filename, entries):
180
+ description = re.search(r'(?<=data\\)(.*?)(?=\\subiect)', filename)[0]
181
+ for entry in entries:
182
+ entry.description = description
183
+
184
+ return entries
185
+
186
+
187
+ def save_as_json(filename, entries):
188
+ entries_dict = [entry.__dict__ for entry in entries]
189
+ with open(filename, 'w') as file:
190
+ json.dump(entries_dict, file, indent=4)
191
+
192
+
193
+ question_paths = [
194
+ "data\\IX\\nationala\\teoretic\\2014\\subiect_2014.pdf",
195
+ "data\\IX\\nationala\\teoretic\\2015\\subiect_2015.pdf",
196
+ "data\\IX\\nationala\\teoretic\\2016\\subiect_2016.pdf",
197
+ "data\\IX\\nationala\\teoretic\\2018\\subiect_2018.pdf",
198
+ "data\\IX\\nationala\\teoretic\\2019\\subiect_2019.pdf",
199
+ "data\\IX\\nationala\\teoretic\\2022\\subiect_2022.pdf",
200
+ "data\\IX\\nationala\\teoretic\\2023\\subiect_2023.pdf",
201
+ "data\\IX\\nationala\\teoretic\\2024\\subiect_2024.pdf",
202
+ ]
203
+
204
+ root_path = "C:\\Users\\adela\\Downloads\\data-20241108T235902Z-001"
205
+
206
+ all_data = []
207
+ for question_path in question_paths:
208
+ full_path = root_path + "\\" + question_path
209
+ parsed_entries = process_file(full_path)
210
+
211
+ answer_path = full_path.replace("subiect", "barem")
212
+ parsed_entries = process_answer_file(answer_path, parsed_entries)
213
+
214
+ parsed_entries = add_description(question_path, parsed_entries)
215
+
216
+ all_data += parsed_entries
217
+
218
+ save_as_json("questions.json", all_data)
219
+
220
+
questions.json ADDED
The diff for this file is too large to render. See raw diff