features.py
5.73 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import numpy
import random
from conf import RANDOM_WORD_VECTORS, W2V_MODEL, W2V_SIZE
from corneferencer.resolvers import constants
# mention features
def head_vec(mention):
head_base = mention.head_orth
if mention.head is not None:
head_base = mention.head['base']
return list(get_wv(W2V_MODEL, head_base))
def first_word_vec(mention):
return list(get_wv(W2V_MODEL, mention.words[0]['base']))
def last_word_vec(mention):
return list(get_wv(W2V_MODEL, mention.words[-1]['base']))
def first_after_vec(mention):
if len(mention.follow_context) > 0:
vec = list(get_wv(W2V_MODEL, mention.follow_context[0]['base']))
else:
vec = [0.0] * W2V_SIZE
return vec
def second_after_vec(mention):
if len(mention.follow_context) > 1:
vec = list(get_wv(W2V_MODEL, mention.follow_context[1]['base']))
else:
vec = [0.0] * W2V_SIZE
return vec
def first_before_vec(mention):
if len(mention.prec_context) > 0:
vec = list(get_wv(W2V_MODEL, mention.prec_context[-1]['base']))
else:
vec = [0.0] * W2V_SIZE
return vec
def second_before_vec(mention):
if len(mention.prec_context) > 1:
vec = list(get_wv(W2V_MODEL, mention.prec_context[-2]['base']))
else:
vec = [0.0] * W2V_SIZE
return vec
def preceding_context_vec(mention):
return list(get_context_vec(mention.prec_context, W2V_MODEL))
def following_context_vec(mention):
return list(get_context_vec(mention.follow_context, W2V_MODEL))
def mention_vec(mention):
return list(get_context_vec(mention.words, W2V_MODEL))
def sentence_vec(mention):
return list(get_context_vec(mention.sentence, W2V_MODEL))
def mention_type(mention):
type_vec = [0] * 4
if mention.head is None:
type_vec[3] = 1
elif mention.head['ctag'] in constants.NOUN_TAGS:
type_vec[0] = 1
elif mention.head['ctag'] in constants.PPRON_TAGS:
type_vec[1] = 1
elif mention.head['ctag'] in constants.ZERO_TAGS:
type_vec[2] = 1
else:
type_vec[3] = 1
return type_vec
# pair features
def distances_vec(ante, ana):
vec = []
mnts_intersect = pair_intersect(ante, ana)
words_dist = [0] * 11
words_bucket = 0
if mnts_intersect != 1:
words_bucket = get_distance_bucket(ana.start_in_words - ante.end_in_words - 1)
words_dist[words_bucket] = 1
vec.extend(words_dist)
mentions_dist = [0] * 11
mentions_bucket = 0
if mnts_intersect != 1:
mentions_bucket = get_distance_bucket(ana.position_in_mentions - ante.position_in_mentions - 1)
if words_bucket == 10:
mentions_bucket = 10
mentions_dist[mentions_bucket] = 1
vec.extend(mentions_dist)
vec.append(mnts_intersect)
return vec
def pair_intersect(ante, ana):
for ante_word in ante.words:
for ana_word in ana.words:
if ana_word['id'] == ante_word['id']:
return 1
return 0
def head_match(ante, ana):
if ante.head_orth.lower() == ana.head_orth.lower():
return 1
return 0
def exact_match(ante, ana):
if ante.text.lower() == ana.text.lower():
return 1
return 0
def base_match(ante, ana):
if ante.lemmatized_text.lower() == ana.lemmatized_text.lower():
return 1
return 0
def ante_contains_rarest_from_ana(ante, ana):
ana_rarest = ana.rarest
for word in ante.words:
if word['base'] == ana_rarest['base']:
return 1
return 0
def agreement(ante, ana, tag_name):
agr_vec = [0] * 3
if (ante.head[tag_name] == 'unk' or ana.head[tag_name] == 'unk'
or ante.head is None or ana.head is None):
agr_vec[2] = 1
elif ante.head[tag_name] == ana.head[tag_name]:
agr_vec[0] = 1
else:
agr_vec[1] = 1
return agr_vec
def is_acronym(ante, ana):
if ana.text.upper() == ana.text:
return check_one_way_acronym(ana.text, ante.text)
if ante.text.upper() == ante.text:
return check_one_way_acronym(ante.text, ana.text)
return 0
def same_sentence(ante, ana):
if ante.sentence_id == ana.sentence_id:
return 1
return 0
def same_paragraph(ante, ana):
if ante.paragraph_id == ana.paragraph_id:
return 1
return 0
# supporting functions
def get_wv(model, lemma, use_random_vec=True):
vec = None
if use_random_vec:
vec = random_vec()
try:
vec = model.wv[lemma]
except KeyError:
pass
except TypeError:
pass
return vec
def random_vec():
return numpy.asarray([random.uniform(-0.25, 0.25) for i in range(0, W2V_SIZE)], dtype=numpy.float32)
def get_context_vec(words, model):
vec = numpy.zeros(W2V_SIZE, dtype=numpy.float32)
unknown_count = 0
if len(words) != 0:
for word in words:
word_vec = get_wv(model, word['base'], RANDOM_WORD_VECTORS)
if word_vec is None:
unknown_count += 1
else:
vec += word_vec
significant_words = len(words) - unknown_count
if significant_words != 0:
vec = vec / float(significant_words)
else:
vec = random_vec()
return vec
def get_distance_bucket(distance):
if 0 <= distance <= 4:
return distance
elif 5 <= distance <= 7:
return 5
elif 8 <= distance <= 15:
return 6
elif 16 <= distance <= 31:
return 7
elif 32 <= distance <= 63:
return 8
elif distance >= 64:
return 9
return 10
def check_one_way_acronym(acronym, expression):
initials = u''
for expr1 in expression.split('-'):
for expr2 in expr1.split():
expr2 = expr2.strip()
if expr2:
initials += expr2[0].upper()
if acronym == initials:
return 1
return 0