-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
145 lines (112 loc) · 4.26 KB
/
Copy pathmain.py
File metadata and controls
145 lines (112 loc) · 4.26 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
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
import time
from concurrent.futures import ThreadPoolExecutor
import pymupdf
from config import *
from models.exam import Exam
from models.exam_subject import ExamSubject
from tools.downloader import *
from tools.parser import (
parse_exam_paper,
save_parse_result,
parse_exam_answer,
parse_exam_indicator,
parse_exam_explanation, )
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
years = [year for year in range(CAP_START_YEAR, CAP_END_YEAR + 1)]
# TODO: Support English Listening.
subjects = [s for s in CAPSubject if s != CAPSubject.ENGLISH_LISTENING]
def download_exam_papers():
start_time = time.time()
with ThreadPoolExecutor(max_workers=10) as executor:
for year in years:
for subject in subjects:
executor.submit(download_exam_paper, year, subject)
elapsed_time = time.time() - start_time
logger.info(
f"Finished downloading CAP exam papers in about {elapsed_time:.2f} seconds."
)
def download_exam_answers_and_indicators():
start_time = time.time()
with ThreadPoolExecutor(max_workers=10) as executor:
for year in years:
executor.submit(download_exam_answer, year)
executor.submit(download_exam_passing_rate, year)
executor.submit(download_exam_discrimination_index, year)
elapsed_time = time.time() - start_time
logger.info(
f"Finished downloading CAP exam answers in about {elapsed_time:.2f} seconds."
)
def download_exam_explanations():
start_time = time.time()
with ThreadPoolExecutor(max_workers=10) as executor:
for year in CAP_EXPLANATION_FILES:
executor.submit(
download_exam_explanation_files, year, CAP_EXPLANATION_FILES[year]
)
elapsed_time = time.time() - start_time
logger.info(
f"Finished downloading CAP exam explanations in about {elapsed_time:.2f} seconds."
)
def parse_exam_papers():
start_time = time.time()
exams: list[Exam] = []
for year in years:
exam_subjects: list[ExamSubject] = []
answers = parse_exam_answer(get_exam_file_path(year, ExamFileType.Answer))
try:
passing_rate = parse_exam_indicator(
get_exam_file_path(year, ExamFileType.Passing_Rate)
)
except pymupdf.FileNotFoundError:
passing_rate = {subject: [None] * 70 for subject in subjects}
print(f"Passing rate not found for year {year}.")
try:
discrimination_index = parse_exam_indicator(
get_exam_file_path(year, ExamFileType.DISCRIMINATION_INDEX)
)
except pymupdf.FileNotFoundError:
discrimination_index = {subject: [None] * 70 for subject in subjects}
print(f"Discrimination index not found for year {year}.")
for subject in subjects:
explanations = parse_exam_explanation(
get_explanation_file_path(year, subject)
)
questions = parse_exam_paper(
get_paper_file_path(year, subject),
subject,
answers,
passing_rate,
discrimination_index,
explanations,
)
exam_subjects.append(
ExamSubject(
subject.get_chinese_name(),
subject.get_duration(),
subject.value,
questions,
)
)
exam = Exam(year, f"{year} 年國中教育會考", exam_subjects)
exams.append(exam)
logger.info(f"Parsed CAP exam papers for year {year}.")
exams.reverse()
save_parse_result(exams)
elapsed_time = time.time() - start_time
logger.info(
f"Finished parsing CAP exam papers in about {elapsed_time:.2f} seconds."
)
def start():
start_time = time.time()
# Create a directory to store the downloaded files.
if not os.path.exists("temp"):
os.makedirs("temp")
download_exam_papers()
download_exam_answers_and_indicators()
download_exam_explanations()
parse_exam_papers()
elapsed_time = time.time() - start_time
logger.info(f"Finished all tasks in about {elapsed_time:.2f} seconds.")
if __name__ == "__main__":
start()