-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanswer_scraper.py
57 lines (46 loc) · 1.7 KB
/
answer_scraper.py
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
import json
import re
from base_scraper import BaseScraper
class KeyScraper(BaseScraper):
def __init__(self, scraper : str):
super().__init__(scraper=scraper)
self.data = {
"questions" : {
"mcq" : [],
"numerical" : []
}
}
def parse_as(self, file, kind):
data = file.read()
regs = self.get_regexes(kind=kind)
mcqs = regs['mcq'].finditer(data)
if self.metadata["method"] == "qid_oid":
for match in mcqs:
self.data["questions"]["mcq"].append({
"question_id" : int(match.group('qid')),
"answer" : self.sane_int(match.group('ans'))
})
elif self.metadata["method"] == "qid_opt":
for match in mcqs:
self.data["questions"]["mcq"].append({
"question_id" : int(match.group('qid')),
"answer" : self.sane_str(match.group('ans'))
})
else:
pass # WTF
nums = regs['numerical'].finditer(data)
for match in nums:
self.data["questions"]["numerical"].append({
"question_id" : int(match.group('qid')),
"answer" : self.sane_float(match.group('ans'))
})
class ProvisionalKeyScraper(KeyScraper):
def __init__(self, scraper : str):
super().__init__(scraper=scraper)
def parse(self, file):
return super().parse_as(file, 'provisional_answer')
class FinalKeyScraper(KeyScraper):
def __init__(self, scraper : str):
super().__init__(scraper=scraper)
def parse(self, file):
return super().parse_as(file, 'final_answer')