-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractor.py
199 lines (162 loc) · 6.82 KB
/
extractor.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
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
from abc import ABC,abstractmethod
from cassis import Cas
from enum import Enum, auto
from cassis.typesystem import Type
from typing import List
from features import uima
from collections import OrderedDict
class AlignmentLabel(Enum):
LC_TOKEN = auto()
TOKEN = auto()
SEMTYPE = auto()
LEMMA = auto()
SPELLING = auto()
SYNONYM = auto()
SIMILARITY = auto()
REFERENCE = auto()
class FeatureExtractor(ABC):
# some constants common to feature extractors
STUDENT_ANSWER_VIEW = "studentAnswer"
TARGET_ANSWER_VIEW = "_InitialView"
QUESTION_VIEW = "question"
ANSWER_TYPE = "de.sfs.isaac.server.nlp.types.Answer"
MAPPABLE_TYPE = "de.sfs.isaac.server.nlp.types.MappableAnnotation"
TOKEN_TYPE = "de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token"
KEYWORD_TYPE = "de.sfs.isaac.server.nlp.types.Keyword"
DEPENDENCY_TYPE = "de.tudarmstadt.ukp.dkpro.core.api.syntax.type.dependency.Dependency"
CHUNK_TYPE = "de.tudarmstadt.ukp.dkpro.core.api.syntax.type.chunk.Chunk"
@abstractmethod
def extract(self, cas: Cas) -> (str,float):
return ("", 0.0)
def get_mappable_ann(self, cas: Cas, t: Type):
return next(cas.select_covered(FeatureExtractor.MAPPABLE_TYPE, t))
def get_perc_of_mapping_type(self, cas: Cas, alignment: AlignmentLabel) -> float:
overallMatchesCount = 0
itemsOfGivenTypeCount = 0
for t in cas.select(FeatureExtractor.TOKEN_TYPE):
item = self.get_mappable_ann(cas, t)
# check for matches/alignment
if item.match is not None and item.match.target is not None:
overallMatchesCount += 1
# check for types
if item.match.label == alignment.name:
itemsOfGivenTypeCount += 1
# if nothing has been matched at all, the result is 0
if overallMatchesCount == 0:
return 0.0;
else:
return itemsOfGivenTypeCount / overallMatchesCount
class Outcome(FeatureExtractor):
LABEL2INT = {"correct" : 1, "incorrect" : 0, "true" : 1, "false" : 0}
def extract(self, cas: Cas) -> (str,float):
studentView = cas.get_view(FeatureExtractor.STUDENT_ANSWER_VIEW)
answer = next(studentView.select(FeatureExtractor.ANSWER_TYPE))
score = answer.contentScore
if score in Outcome.LABEL2INT:
return ("Outcome", Outcome.LABEL2INT[score])
else:
try:
outcome = float(score)
except ValueError:
outcome = float('nan')
return ("Outcome", outcome)
class Diagnosis(FeatureExtractor):
def extract(self, cas:Cas)->(str,float):
studentView = cas.get_view(FeatureExtractor.STUDENT_ANSWER_VIEW)
answer = next(studentView.select(FeatureExtractor.ANSWER_TYPE))
return ("Diagnosis", int(answer.contentDiagnosis))
class PercentOfMappingType(FeatureExtractor):
def __init__(self, alignment: AlignmentLabel):
self.alignment = alignment
def extract(self, cas:Cas)->(str,float):
studentView = cas.get_view(FeatureExtractor.STUDENT_ANSWER_VIEW)
return (self.alignment.name + "-Match", self.get_perc_of_mapping_type(studentView, self.alignment))
class MatchedAnnotation(FeatureExtractor):
def __init__(self, view_name: str, ann_type: str):
self.view_name = view_name
self.ann_type = ann_type
def extract(self, cas:Cas)->(str,float):
view = cas.get_view(self.view_name)
matched_ann = 0
all_ann = 0
for a in view.select(self.ann_type):
all_ann += 1
mappable = self.get_mappable_ann(view, a)
if mappable.match:
matched_ann += 1
ret = -1.0
if not all_ann:
ret = 0.0
else:
ret = matched_ann / all_ann
return (self.view_name + "-" + uima.simple_type_name(self.ann_type) + "-Overlap", ret)
class TripleOverlap(FeatureExtractor):
DEP_MAPPING_TYPE = "de.sfs.isaac.server.nlp.types.DepMapping"
def __init__(self, view_name: str):
self.view_name = view_name
self.english_arg_rels = set(
["dep",
"arg",
"subj",
"nsubj",
"nsubjpass",
"csubj",
"comp",
"obj",
"dobj",
"iobj",
"pobj",
"attr",
"ccomp",
"xcomp",
"compl",
"mark",
"rel",
"acomp",
"agent"])
def extract(self, cas:Cas)->(str,float):
view = cas.get_view(self.view_name)
dep_matches = len(list(view.select(TripleOverlap.DEP_MAPPING_TYPE)))
dep_rels = 0
for d in view.select(FeatureExtractor.DEPENDENCY_TYPE):
if d.DependencyType in self.english_arg_rels:
dep_rels += 1
ret = -1.0
if not dep_rels:
ret = 0.0
else:
ret = dep_matches / dep_rels
return (self.view_name + "-Triple-Overlap", ret)
class Variety(FeatureExtractor):
def extract(self, cas:Cas)->(str,float):
studentView = cas.get_view(FeatureExtractor.STUDENT_ANSWER_VIEW)
variety = 0.0
for al in AlignmentLabel:
variety += self.get_perc_of_mapping_type(studentView, al)
return ("Variety", variety);
class FeatureExtraction():
def __init__(self, extractors: List[FeatureExtractor] = None):
if extractors:
self.extractors = extractors
else:
self.extractors = [
MatchedAnnotation(FeatureExtractor.TARGET_ANSWER_VIEW, FeatureExtractor.KEYWORD_TYPE),
MatchedAnnotation(FeatureExtractor.TARGET_ANSWER_VIEW, FeatureExtractor.TOKEN_TYPE),
MatchedAnnotation(FeatureExtractor.STUDENT_ANSWER_VIEW, FeatureExtractor.TOKEN_TYPE),
MatchedAnnotation(FeatureExtractor.TARGET_ANSWER_VIEW, FeatureExtractor.CHUNK_TYPE),
MatchedAnnotation(FeatureExtractor.STUDENT_ANSWER_VIEW, FeatureExtractor.CHUNK_TYPE),
TripleOverlap(FeatureExtractor.TARGET_ANSWER_VIEW),
TripleOverlap(FeatureExtractor.STUDENT_ANSWER_VIEW),
PercentOfMappingType(AlignmentLabel.LC_TOKEN),
PercentOfMappingType(AlignmentLabel.LEMMA),
PercentOfMappingType(AlignmentLabel.SYNONYM),
Variety(),
Outcome()
]
def from_cases(self, cases: List[Cas]) -> OrderedDict:
ret = OrderedDict()
for cas in cases:
for x in self.extractors:
f = x.extract(cas)
ret.setdefault(f[0], []).append(f[1])
return ret