-
Notifications
You must be signed in to change notification settings - Fork 0
/
hminghkm.py
250 lines (204 loc) · 7.01 KB
/
hminghkm.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
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
244
245
246
247
248
249
#!/usr/bin/env python
# hminghkm.py
# [email protected] (Jason Riesa)
#
# Minimal GHKM-rule extractor from David Chiang
# Modified to use NLPTrees, tree and alignment fragments,
# weakRef objects, and constrained extraction.
"""Command-line usage:
stdin:
COMINGFROM FRANCE AND RUSSIA DE ASTRO NAUTS \t (NP (NP (NNS astronauts)) (VP (VBG coming) (PP (IN from) (NP (NP (NNP france) (CC and) (NNP russia)))))) \t 0-1 0-2 1-3 2-4 3-5 5-0 6-0
stdout:
(NNS NAUTS ASTRO) <- (NNS astronauts)
(NP NNS:x0) <- (NP NNS:x0)
(NNP FRANCE) <- (NNP france)
(CC AND) <- (CC and)
(NNP RUSSIA) <- (NNP russia)
(NP CC:x1 NNP:x2 NNP:x0) <- (NP NNP:x0 CC:x1 NNP:x2)
(NP NP:x0) <- (NP NP:x0)
(VP NP:x0 COMINGFROM) <- (VP (VBG coming) (PP (IN from) NP:x0))
(NP DE NP:x1 VP:x0) <- (NP NP:x1 VP:x0)
"""
import itertools
import sys
import NLPTree
import NLPTreeHelper
import Tree
class Rule(object):
def __init__(self, f, e):
"""
f is a one-level tree representing the French side of the rule
e is a tree representing the English side of the rule
each node is labeled with either a str or a Variable
"""
self.f = f
self.e = e
def __str__(self):
return "%s -> %s" % (self.e, " ".join([str(item) for item in self.f.children]))
def __eq__(self, other):
return str(self) == str(other)
def __hash__(self):
return hash(str(self))
class Variable(object):
def __init__(self, data, index=None):
"""
label is the label (syntactic category) of the variable,
index is a number by which this variable will be paired
with another variable on the other side of the rule
"""
self.data = data
self.index = index
def __str__(self):
if self.index is not None:
return "%s:x%d" % (self.data, self.index)
else:
return self.data
def __eq__(self, other):
return (self.data, self.index) == (other.data, other.index)
def __hash__(self):
return hash((self.data, self.index))
def mark_phrases(fwords, etree, align, offset = 0, hierarchical = False):
fn = len(fwords)
en = etree.j - offset
# the first French word aligned to each English word
emin = [fn] * en
# the last French word aligned to each English word, plus one
emax = [-1] * en
# the number of English words aligned to each French word
fcount = [0] * fn
# similarly for the other direction
ecount = [0] * en
for (fi,ei) in align:
emin[ei] = min(emin[ei],fi)
emax[ei] = max(emax[ei],fi+1)
fcount[fi] += 1
ecount[ei] += 1
# fcumul[fi] is the number of alignment points in fwords[:fi]
fcumul = [0]
s = 0
for c in fcount:
s += c
fcumul.append(s)
for node in etree.bottomup():
if len(node.children) == 0:
node.ecount = ecount[node.i-offset]
node.emin = emin[node.i-offset]
node.emax = emax[node.i-offset]
else:
node.ecount = 0
node.emin = fn
node.emax = -1
for child in node.children:
node.emin = min(node.emin, child.emin)
node.emax = max(node.emax, child.emax)
node.ecount += child.ecount
# We know how many alignment points the English node has,
# and the number of alignment points that the corresponding
# French span has. If those numbers are equal, then the
# two are exclusively aligned to each other and we have a phrase.
node.phrase = node.ecount > 0 and node.ecount == fcumul[node.emax]-fcumul[node.emin]
def _detach_phrases(node, accum, etree, hierarchical):
copy = NLPTree.NLPTree(node.data, [_detach_phrases(child, accum, etree, hierarchical) for child in node.children])
copy.emin, copy.emax = node.emin, node.emax
if node.phrase and len(node.children) > 0:
# We only care about the current node
if node == etree or (not hierarchical):
accum.append(copy)
copy = NLPTree.NLPTree(Variable(node.data))
copy.emin, copy.emax = node.emin, node.emax
return copy
def detach_phrases(node, etree, hierarchical):
accum = []
_detach_phrases(node, accum, etree, hierarchical)
return accum
def make_rule(fwords, etree, align):
fspans = []
# build the erhs while keeping track of the French spans of the vars
for enode in etree.frontier():
if isinstance(enode.data, Variable):
fspans.append((enode.emin, enode.emax, enode))
fspans.sort()
# build the frhs with links to erhs, and build ants
r = Rule(NLPTree.NLPTree(etree.data), etree)
prev_fj = etree.emin
for (vi,(fi,fj,enode)) in enumerate(fspans):
assert fj > fi
for fk in xrange(prev_fj,fi):
fleaf = NLPTree.NLPTree(fwords[fk])
r.f.insert_child(-1, fleaf)
var = Variable(enode.data, vi)
fleaf = NLPTree.NLPTree(var)
enode.data = var
r.f.insert_child(-1, fleaf)
prev_fj = fj
for fk in xrange(prev_fj,etree.emax):
fleaf = NLPTree.NLPTree(fwords[fk])
r.f.insert_child(-1, fleaf)
yield r
def extract(fwords, etree, align, offset = 0, hierarchical = False):
"""
fwords is a list of French words
etree is an English tree
align is a list of pairs of (french, english) positions
returns: an iterator over extracted Rules
"""
# find the frontier set
mark_phrases(fwords, etree, align, offset)
# push outermost unaligned French words into top rule
etree.emin, etree.emax = 0, len(fwords)
for subtree in detach_phrases(etree, etree, hierarchical):
for r in make_rule(fwords, subtree, align):
yield r
def extractRuleDict(fwords, etree, align, offset = 0, hierarchical = False):
"""
fwords is a list of French words
etree is an English tree
align is a list of pairs of (french, english) positions
returns: an dictionary of extracted Rules
"""
rules = { }
# find the frontier set
mark_phrases(fwords, etree, align, offset)
# push outermost unaligned French words into top rule
etree.emin, etree.emax = 0, len(fwords)
for subtree in detach_phrases(etree, etree, hierarchical):
for r in make_rule(fwords, subtree, align):
rules[r] = True
return rules
if __name__ == "__main__":
class SkipSentence(Exception):
pass
progress = 0
skipped = 0
for line in sys.stdin:
try:
(fline, eline, aline) = line.split("\t")
fwords = fline.split()
etree = NLPTreeHelper.stringToTree_weakRef(eline)
if etree is None:
raise SkipSentence
if etree.data in ["", "TOP", "ROOT"]:
if len(etree.children) == 1:
etree = etree.children[0]
else:
sys.stderr.write("warning, line %d: top node has multiple children\n" % (progress+1))
raise SkipSentence
fn = len(fwords)
en = etree.j
align = []
for pair in aline.split():
i,j = pair.split("-",1)
i = int(i)
j = int(j)
if i >= fn or j >= en:
sys.stderr.write("warning, line %d: alignment point out of bounds\n" % (progress+1))
raise SkipSentence
align.append((i,j))
if len(align) == 0:
sys.stderr.write("warning, line %d: no alignments\n" % (progress+1))
raise SkipSentence
for r in extract(fwords, etree, align):
print r
except SkipSentence:
skipped += 1
progress += 1