forked from allenai/aristo-mini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
52 lines (42 loc) · 1.48 KB
/
models.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
"""
typed models for our data. these are the exact analogues of the case classes used by the scala
code (which is why the fields have unfortunate, non-pythonic names)
"""
from typing import NamedTuple, List, Any, Dict, NamedTuple
import simplejson as json
# pylint: disable=invalid-name
class Choice(NamedTuple):
label: str
text: str
class ChoiceConfidence(NamedTuple):
choice: Choice
confidence: float
class MultipleChoiceAnswer(NamedTuple):
choiceConfidences: List[ChoiceConfidence]
class SolverAnswer(NamedTuple):
solverInfo: str
multipleChoiceAnswer: MultipleChoiceAnswer
class MultipleChoiceQuestion(NamedTuple):
stem: str
choices: List[Choice]
id_: str = None
answerKey: str = None
@staticmethod
def from_jsonl(line: str) -> 'MultipleChoiceQuestion':
blob = json.loads(line)
question = blob['question']
return MultipleChoiceQuestion(
id_=blob['id'],
stem=question['stem'],
choices=[Choice(c["label"], c["text"]) for c in question['choices']],
answerKey=blob['answerKey'],
)
class Exam(NamedTuple):
name: str
questions: List[MultipleChoiceQuestion]
def parse_question(blob: Dict[str, Any]) -> MultipleChoiceQuestion:
"""parses a question from a json blob. is possibly too lenient to malformed json"""
return MultipleChoiceQuestion(
stem=blob.get("stem", ""),
choices=[Choice(c["label"], c["text"]) for c in blob.get("choices", [])]
)