-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinferer.py
More file actions
243 lines (218 loc) · 9.64 KB
/
inferer.py
File metadata and controls
243 lines (218 loc) · 9.64 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
242
243
# Copyright 2017 Johns Hopkins University. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import networkx as nx
import spacy
import sys
import time
from collections import defaultdict
from relation import Relation
from structures import *
THRESHOLD = 4 # higher numebrs speed up solver
POINTS = 1.0
BONUS_POINTS = 1.0
class Inferer:
"""
Inferrer is technically the correct spelling. There is one inferrer for each
passage of multilingual parallel text.
size : int Number of relations (= num rows = len(relations))
numEditions : int Number of editions (<= num cols = len(sentences))
relations : relation list List of relations
sentences : Doc list List of sentences
backMap : (int, int) dict If i = relations[r][ed] then backMap(ed, i) =
score : int Final score of the inferer (-1 if unscored)
"""
def __init__(self, perm):
self.size = 0
self.numEditions = 0
self.relations = []
self.sentences = []
self.testSentences = []
self.backMap = {}
self.finalScore = -1
self.perm = list(perm)
self.invperm = [x[0] for x in
sorted([f for f in enumerate(self.perm, 0)],
key=lambda x:x[1])]
def infer(self, parseTree, suggested):
"""
Given a parsed tree and alignments, augments the object with either
new relations or maps words from parsedTree to old relations.
parseTree : Doc
suggested : 2d alignment array with all other parses
"""
t0 = time.time()
newRelations = self.updateAlignmentsAtOnce(parseTree, suggested)
t1 = time.time()
for relation in newRelations:
self.addRelation(relation)
for relation in self.relations:
relation.flushBuffer() # buffer might not be needed anymore
self.numEditions += 1
self.sentences.append(parseTree)
t2 = time.time()
if t2 - t0 > 1:
print ("UAAO: {}, other: {}".format(t1-t0, t2-t1))
# SCORE is not passed on, this return value doesn't matter
# TODO: remove
return 1 # score, which can be derived from a similarity score?
def updateAlignmentsAtOnce(self, parseTree, suggested):
"""
Use maximum weight bipartite matching - (|parseTree| + |relations|)^3
parseTree: Doc
suggested: 2d alignment array
"""
t0 = time.time()
G = nx.Graph()
structures = [Structure(word, self.numEditions) for word in parseTree]
for s in structures:
G.add_node(s)
for relation in self.relations:
G.add_node(relation)
t1 = time.time()
# create a parent matrix
parentMatrix = self.createParentMatrix(parseTree, suggested)
t2 = time.time()
# note structures is already sorted into a DAG
# might not be relevant
prof_scores = []
for s in structures:
for relationIdx in xrange(len(self.relations)):
relation = self.relations[relationIdx]
score = relation.scoreWith(s, suggested, parentMatrix[relationIdx])
if (score >= THRESHOLD):
G.add_edge(s, relation, {'weight': score})
prof_scores.append(score)
t3 = time.time()
bestMatching = nx.max_weight_matching(G)
t4 = time.time()
for relationIdx in xrange(len(self.relations)):
relation = self.relations[relationIdx]
if relation in bestMatching:
newStructure = bestMatching[relation]
relation.structureBuffer.append((newStructure, G[relation][newStructure]['weight']))
self.backMap[(self.numEditions, newStructure.name.i)] = relationIdx
unmatched = [Relation(s) for s in structures
if (s not in bestMatching and not s.shouldIgnore())]
t5 = time.time()
if t5 - t0 > 1:
print ("UAAO: create: {}, matrix: {}, score: {}, match: {}, combine: {}, weights: []".format(t1-t0, t2-t1, t3-t2, t4-t3, t5-t4, prof_scores))
return unmatched
def createParentMatrix(self, parseTree, suggested):
"""
For each word, create a matrix that scores how close it is to all possible parents. This
is done by looking at its parent in other sentences. This is later used to determine
whether two parents share similar children.
parseTree: Doc
suggested: 2d alignment array
Returns:
matrix = size by |parseTree| array
matrix[r][i] = score that r is the parent relation of i.
"""
matrix = [[0 for _ in parseTree] for _ in xrange(self.size)]
for sIdx in xrange(len(parseTree)):
for ed in xrange(self.numEditions):
tIdxs = suggested[ed][sIdx]
for tIdx in tIdxs:
try:
t = self.sentences[ed][tIdx]
pIdx = t.head.i
try:
r = self.backMap[(ed, pIdx)]
matrix[r][sIdx] += POINTS / len(tIdxs)
if (t.dep_ == parseTree[sIdx].dep_):
matrix[r][sIdx] += BONUS_POINTS / len(tIdxs)
if (t.head.pos_ == parseTree[sIdx].pos_):
matrix[r][sIdx] += BONUS_POINTS / len(tIdxs)
except KeyError:
# Oh well, not found.
pass
except IndexError:
print (u"Could not index {} in {}: {}".format(tIdx, self.sentences[ed], self.sentences))
return matrix
def addRelation(self, relation):
"""
Insert a relation into list, update backmap and size
"""
self.relations.append(relation)
self.backMap[(self.numEditions, relation.name.i)] = self.size
self.size += 1
return
def extractRules(self):
"""
returns a list of equivalent sets
call pick consensus in here
"""
return [list(r.extractRules()) for r in self.relations]
def predict(self, testLine, suggested):
# testline is an unparsed line from testfile
# suggested is an alignment list, suggested[i] corresponds to alignments with filei
# max weight between words and structures
self.testSentences.append(" ".join(testLine))
G = nx.Graph()
for relation in xrange(self.size):
G.add_node(relation)
for word in testLine:
G.add_node(word)
for ed in xrange(self.numEditions):
for i in xrange(len(testLine)):
for j in suggested[ed][i]:
# (test[i], train[j]) are aligned
try:
relation = self.backMap[(self.perm[ed], j)]
if (testLine[i] in G[relation]):
G[relation][testLine[i]]['weight'] += 1
else:
G.add_edge(relation, testLine[i], weight=1)
except Exception, e:
pass
bestMatching = nx.max_weight_matching(G)
for relationIdx in xrange(self.size):
relation = self.relations[relationIdx]
if relationIdx in bestMatching:
relation.predicts.append(bestMatching[relationIdx])
else:
relation.predicts.append("")
# self.backMap update?
# Ignore unmatched things, don't make new relations
return None
def score(self):
if (self.size == 0):
return 0
self.finalScore = sum([r.score for r in self.relations])/(float(self.size))
def getScore(self):
return self.finalScore
def toString(self):
# stackoverflow print function
headerRow = [str(self.perm[i]) for i in range(self.numEditions)]
headerRow.insert(0, "ED:")
headerRow.append("Predict?")
data = [row.toList(self.numEditions) for row in self.relations]
for i in xrange(self.size):
data[i].insert(0, "{}".format(self.relations[i].finalString()))
data.insert(0, headerRow)
lens = [max(map(len, col)) for col in zip(*data)]
fmt = u'\t'.join('{{:{}}}'.format(x) for x in lens)
table = u"\n".join([fmt.format(*row) for row in data])
sentences = u"\n".join([sent.string for sent in self.sentences])
testSentences = u"\n".join(self.testSentences)
relations = u"\n".join(["{}".format(r.toString(self.backMap, self.relations))
for r in self.relations
if len(r.getEdges(self.backMap, self.relations)[1]) > 0])
return (u"{}\n\n{}\n\n{}\n\n{}".format(table, sentences, testSentences, relations),
relations)