/
eisner_algo.py
183 lines (139 loc) · 6.76 KB
/
eisner_algo.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
import pydecode
import numpy as np
import pprint
from collections import defaultdict
from constants import Constants
class EisnerAlgo:
def __init__(self):
self.num_shapes = 3
self.tri=0; self.trap = 1;self.tri_stop=2;
self.adj = 1; self.non_adj = 0
self.num_dir = 2; self.num_adj = 2
self.right = 0; self.left = 1
self.graph = None
self.out = []
self.weights = None
self.sentence = None
self.constants = Constants()
def eisner_first_order(self, sentence):
self.sentence = sentence
n = len(sentence)
coder = np.arange((self.num_shapes * self.num_dir * n * n),
dtype=np.int64).reshape([self.num_shapes, self.num_dir, n, n])
self.out = np.arange(n * n * self.num_shapes * self.num_dir * 2,
dtype=np.int64).reshape([self.num_shapes, self.num_dir, 2, n, n])
chart = pydecode.ChartBuilder(coder, self.out)
chart.init(np.diag(coder[self.tri, self.right]).copy())
chart.init(np.diag(coder[self.tri, self.left, 1:, 1:]).copy())
for direction in range(self.num_dir):
for tri, triStop, index in zip(np.diag(coder[self.tri, direction]),
np.diag(coder[self.tri_stop, direction]), range(n)):
if(index == 0):
continue
chart.set(triStop, [[tri]], labels=[np.int64(self.out[self.tri_stop,\
direction, self.adj, index, index])])
self.all_label_indices = np.array([],dtype=np.int64)
for k in range(1, n):
for s in range(n):
t = k + s
if t >= n:
break
# First create incomplete items.
out_ind = np.zeros([t-s], dtype=np.int64)
if s!=0:
label_indices = self.compute_label_indices(self.trap,
self.left, t , s)
chart.set_t(coder[self.trap, self.left, s, t],
coder[self.tri_stop, self.right, s, s:t],
coder[self.tri, self.left, s+1:t+1, t],
labels=label_indices)
label_indices = self.compute_label_indices(self.trap, self.right,
s, t)
chart.set_t(coder[self.trap, self.right, s, t],
coder[self.tri, self.right, s, s:t],
coder[self.tri_stop, self.left, s+1:t+1, t],
labels= label_indices)
if s!=0:
label_indices = self.compute_label_indices(self.tri, self.left,
t, s)
chart.set_t(coder[self.tri, self.left, s, t],
coder[self.tri_stop, self.left, s, s:t],
coder[self.trap, self.left, s:t, t])
label_indices = self.compute_label_indices(self.tri_stop,
self.left, s, t)
chart.set(coder[self.tri_stop, self.left, s, t],
[[coder[self.tri, self.left, s, t]]],
labels= label_indices)
label_indices = self.compute_label_indices(self.tri,
self.right, s, t)
chart.set_t(coder[self.tri, self.right, s, t],
coder[self.trap, self.right, s, s+1:t+1],
coder[self.tri_stop, self.right, s+1:t+1, t])
if s!=0 or (s==0 and t==n-1):
label_indices = self.compute_label_indices(self.tri_stop,
self.right, s, t)
chart.set(coder[self.tri_stop, self.right, s, t],
[[coder[self.tri, self.right, s, t]]],
labels=label_indices)
self.graph = chart.finish()
return self.graph
def compute_weights(self, label_scores):
if self.weights == None:
self.weights = pydecode.transform(self.graph, label_scores)
return self.weights
def reset_values(self):
self.weights = None
def compute_marginals(self, label_scores):
self.compute_weights(label_scores)
edge_marginals = pydecode.marginals(self.graph, self.weights)
return edge_marginals
def best_path(self, label_scores):
self.compute_weights(label_scores)
return pydecode.best_path(self.graph, self.weights)
def best_edges(self, label_scores):
path = self.best_path(label_scores)
best_edges = path.edges
heads = np.array([], dtype=np.int64)
for edge in best_edges:
heads = np.append(heads, edge.head.label)
shapes, direction, head, modifier = self.get_indices_of_heads(heads,
len(self.sentence))
print "shapes"
pprint.pprint(shapes)
print "direct"
pprint.pprint(direction)
print "head"
pprint.pprint(head)
print "mod"
pprint.pprint(modifier)
right_indices = np.where(direction == 0)
left_indices = np.where(direction == 1)
depen = np.full(len(self.sentence), -1, dtype=np.int64)
depen[modifier[right_indices].tolist()] = head[right_indices].tolist()
depen[head[left_indices].tolist()] = modifier[left_indices].tolist()
return depen
def get_indices_of_heads(self, heads, n):
shapes, direction, row, column = self.constants.get_indices(heads, n)
indices_needed = np.where(shapes == 1)
return shapes[indices_needed], direction[indices_needed], row[indices_needed], column[indices_needed]
def compute_label_indices(self, shape, direction, head, mod):
indices = np.tile([shape, direction, 0, head, mod], abs(head-mod)).\
reshape(abs(head-mod), 5)
indices[:,2] = self.compute_adj(head, mod)
if(indices.shape[0] == 1):
return np.array([self.out[indices[0,0], indices[0,1], indices[0,2],
indices[0, 3], indices[0, 4]]])
return self.out[indices[:,0].tolist(),indices[:,1].tolist(),
indices[:,2].tolist(), indices[:,3].tolist(), indices[:,4].tolist()]
def compute_adj(self, head, mod):
if(head > mod):
split = np.arange(mod, head)
else:
split = np.arange(head, mod)
vfunc = np.vectorize(self.is_adj)
return vfunc(head, split)
def is_adj(self, head, split):
if abs(head - split) <=1:
return self.adj
else:
return self.non_adj