summaryrefslogtreecommitdiff
path: root/gi/evaluation/tree.py
blob: 702d80b6f9fa072e22146de2296d7e3ddd5c344e (plain)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
import re, sys

class Symbol:
    def __init__(self, nonterm, term=None, var=None):
        assert not (term != None and var != None)
        self.tag = nonterm
        self.token = term
        self.variable = var

    def is_variable(self):
        return self.variable != None

    def __eq__(self, other):
        return self.tag == other.tag and self.token == other.token and self.variable == other.variable

    def __ne__(self, other):
        return not (self == other)

    def __hash__(self):
        return hash((self.tag, self.token, self.variable))

    def __repr__(self):
        return str(self)

    def __cmp__(self, other):
        return cmp((self.tag, self.token, self.variable),
                   (other.tag, other.token, other.variable))

    def __str__(self):
        parts = []
	if False: # DEPENDENCY
	    if self.token:
		parts.append(str(self.token))
	    elif self.variable != None:
		parts.append('#%d' % self.variable)
	    if self.tag:
		parts.append(str(self.tag))
	    return '/'.join(parts)
	else:
	    if self.tag:
		parts.append(str(self.tag))
	    if self.token:
		parts.append(str(self.token))
	    elif self.variable != None:
		parts.append('#%d' % self.variable)
	    return ' '.join(parts)

class TreeNode:
    def __init__(self, data, children=None, order=-1):
        self.data = data
        self.children = []
        self.order = order
        self.parent = None
        if children: self.children = children

    def insert(self, child):
        self.children.append(child)
        child.parent = self

    def leaves(self):
        ls = []
        for node in self.xtraversal():
            if not node.children:
                ls.append(node.data)
        return ls

    def leaf_nodes(self):
        ls = []
        for node in self.xtraversal():
            if not node.children:
                ls.append(node)
        return ls

    def max_depth(self):
        d = 1
        for child in self.children:
            d = max(d, 1 + child.max_depth())
        if not self.children and self.data.token:
            d = 2
        return d

    def max_width(self):
        w = 0
        for child in self.children:
           w += child.max_width()
        return max(1, w)

    def num_internal_nodes(self):
        if self.children:
            n = 1
            for child in self.children:
                n += child.num_internal_nodes()
            return n
        elif self.data.token:
            return 1
        else:
            return 0

    def postorder_traversal(self, visit):
        """
	Postorder traversal; no guarantee that terminals will be read in the
	correct order for dep. trees.
        """
        for child in self.children:
            child.traversal(visit)
	visit(self)

    def traversal(self, visit):
        """
        Preorder for phrase structure trees, and inorder for dependency trees.
        In both cases the terminals will be read off in the correct order.
        """
        visited_self = False
        if self.order <= 0:
            visited_self = True
            visit(self)

        for i, child in enumerate(self.children):
            child.traversal(visit)
            if i + 1 == self.order:
                visited_self = True
                visit(self)

        assert visited_self

    def xpostorder_traversal(self):
        for child in self.children:
            for node in child.xpostorder_traversal():
                yield node
        yield self

    def xtraversal(self):
        visited_self = False
        if self.order <= 0:
            visited_self = True
            yield self

        for i, child in enumerate(self.children):
            for d in child.xtraversal():
                yield d

            if i + 1 == self.order:
                visited_self = True
                yield self

        assert visited_self

    def xpostorder_traversal(self):
        for i, child in enumerate(self.children):
            for d in child.xpostorder_traversal():
                yield d
        yield self

    def edges(self):
        es = []
        self.traverse_edges(lambda h,c: es.append((h,c)))
        return es

    def traverse_edges(self, visit):
        for child in self.children:
            visit(self.data, child.data)
            child.traverse_edges(visit)

    def subtrees(self, include_self=False):
        st = []
        if include_self:
            stack = [self]
        else:
            stack = self.children[:]

        while stack:
            node = stack.pop()
            st.append(node)
            stack.extend(node.children)
        return st

    def find_parent(self, node):
        try:
            index = self.children.index(node)
            return self, index
        except ValueError:
            for child in self.children:
                if isinstance(child, TreeNode):
                    r = child.find_parent(node)
                    if r: return r
        return None

    def is_ancestor_of(self, node):
        if self == node:
            return True
        for child in self.children:
            if child.is_ancestor_of(child):
                return True
        return False

    def find(self, node):
        if self == node:
            return self
        for child in self.children:
            if isinstance(child, TreeNode):
                r = child.find(node)
                if r: return r
            else:
                if child == node:
                   return r
        return None

    def equals_ignorecase(self, other):
        if not isinstance(other, TreeNode):
            return False
        if self.data != other.data:
            return False
        if len(self.children) != len(other.children):
            return False
        for mc, oc in zip(self.children, other.children):
            if isinstance(mc, TreeNode):
                if not mc.equals_ignorecase(oc):
                    return False
            else:
                if mc.lower() != oc.lower():
                    return False
        return True

    def node_number(self, numbering, next=0):
        if self.order <= 0:
            numbering[id(self)] = next
            next += 1

        for i, child in enumerate(self.children):
            next = child.node_number(numbering, next)
            if i + 1 == self.order:
                numbering[id(self)] = next
                next += 1

        return next

    def display_conll(self, out):
        numbering = {}
        self.node_number(numbering)
        next = 0
        self.children[0].traversal(lambda x: \
            out.write('%d\t%s\t%s\t%s\t%s\t_\t%d\tLAB\n' \
             % (numbering[id(x)], x.data.token, x.data.token, 
                x.data.tag, x.data.tag, numbering[id(x.parent)])))
        out.write('\n')

    def size(self):
        sz = 1 
        for child in self.children:
            sz += child.size()
        return sz

    def __eq__(self, other):
        if isinstance(other, TreeNode) and self.data == other.data \
                and self.children == other.children:
            return True
        return False

    def __cmp__(self, other):
        if not isinstance(other, TreeNode): return 1
        n = cmp(self.data, other.data)
        if n != 0: return n
        n = len(self.children) - len(other.children)
        if n != 0: return n
        for sc, oc in zip(self.children, other.children):
            n = cmp(sc, oc)
            if n != 0: return n
        return 0

    def __ne__(self, other):
        return not self.__eq__(other)

    def __hash__(self):
        return hash((self.data, tuple(self.children)))

    def __repr__(self):
        return str(self)

    def __str__(self):
        s = '('
        space = False
        if self.order <= 0:
            s += str(self.data)
            space = True
        for i, child in enumerate(self.children):
            if space: s += ' '
            s += str(child)
            space = True
            if i+1 == self.order:
                s += ' ' + str(self.data)
        return s + ')'

def read_PSTs(fname):
    infile = open(fname)
    trees = []
    for line in infile:
        trees.append(parse_PST(line.strip()))
    infile.close()
    return trees

def parse_PST_multiline(infile, hash_is_var=True):
    buf = ''
    num_open = 0
    while True:
        line = infile.readline()
        if not line:
            return None
        buf += ' ' + line.rstrip()
        num_open += line.count('(') - line.count(')')
        if num_open == 0:
            break

    return parse_PST(buf, hash_is_var)

def parse_PST(line, hash_is_var=True):
    line = line.rstrip()
    if not line or line.lower() == 'null':
        return None

    # allow either (a/DT) or (DT a)
    #parts_re = re.compile(r'(\(*)([^/)]*)(?:/([^)]*))?(\)*)$')

    # only allow (DT a)
    parts_re = re.compile(r'(\(*)([^)]*)(\)*)$')

    root = TreeNode(Symbol('TOP'))
    stack = [root]
    for part in line.rstrip().split():
        m = parts_re.match(part)
        #opening, tok_or_tag, tag, closing = m.groups()
        opening, tok_or_tag, closing = m.groups()
	tag = None
        #print 'token', part, 'bits', m.groups()
        for i in opening:
            node = TreeNode(Symbol(None))
            stack[-1].insert(node)
            stack.append(node)

        if tag:
            stack[-1].data.tag = tag
            if hash_is_var and tok_or_tag.startswith('#'):
                stack[-1].data.variable = int(tok_or_tag[1:])
            else:
                stack[-1].data.token = tok_or_tag
        else:
            if stack[-1].data.tag == None:
                stack[-1].data.tag = tok_or_tag
            else:
                if hash_is_var and tok_or_tag.startswith('#'):
                    try:
                        stack[-1].data.variable = int(tok_or_tag[1:])
                    except ValueError: # it's really a token!
                        #print >>sys.stderr, 'Warning: # used for token:', tok_or_tag
                        stack[-1].data.token = tok_or_tag
                else:
                    stack[-1].data.token = tok_or_tag
        
        for i in closing:
            stack.pop()

    #assert str(root.children[0]) == line
    return root.children[0]

def read_DTs(fname):
    infile = open(fname)
    trees = []
    while True:
        t = parse_DT(infile)
        if t: trees.append(t)
        else: break
    infile.close()
    return trees

def read_bracketed_DTs(fname):
    infile = open(fname)
    trees = []
    for line in infile:
        trees.append(parse_bracketed_DT(line))
    infile.close()
    return trees

def parse_DT(infile):
    tokens = [Symbol('ROOT')]
    children = {}

    for line in infile:
        parts = line.rstrip().split()
        #print parts
        if not parts: break
        index = len(tokens)
        token = parts[1]
        tag = parts[3]
        parent = int(parts[6])
        if token.startswith('#'):
            tokens.append(Symbol(tag, var=int(token[1:])))
        else:
            tokens.append(Symbol(tag, token))
        children.setdefault(parent, set()).add(index)

    if len(tokens) == 1: return None

    root = TreeNode(Symbol('ROOT'), [], 0)
    schedule = []
    for child in sorted(children[0]):
        schedule.append((root, child))

    while schedule:
        parent, index = schedule[0]
        del schedule[0]
    
        node = TreeNode(tokens[index])
        node.order = 0
        parent.insert(node)

        for child in sorted(children.get(index, [])):
            schedule.append((node, child))
            if child < index:
                node.order += 1

    return root

_bracket_split_re = re.compile(r'([(]*)([^)/]*)(?:/([^)]*))?([)]*)')

def parse_bracketed_DT(line, insert_root=True):
    line = line.rstrip()
    if not line or line == 'NULL': return None
    #print line

    root = TreeNode(Symbol('ROOT'))
    stack = [root]
    for part in line.rstrip().split():
        m = _bracket_split_re.match(part)

        for c in m.group(1):
            node = TreeNode(Symbol(None))
            stack[-1].insert(node)
            stack.append(node)

        if m.group(3) != None:
            if m.group(2).startswith('#'):
                stack[-1].data.variable = int(m.group(2)[1:])
            else:
                stack[-1].data.token = m.group(2)
            stack[-1].data.tag = m.group(3)
        else:
            stack[-1].data.tag = m.group(2)
        stack[-1].order = len(stack[-1].children)
        # FIXME: also check for vars

        for c in m.group(4):
            stack.pop()

    assert len(stack) == 1
    if not insert_root or root.children[0].data.tag == 'ROOT':
        return root.children[0]
    else:
        return root

_bracket_split_notag_re = re.compile(r'([(]*)([^)/]*)([)]*)')

def parse_bracketed_untagged_DT(line):
    line = line.rstrip()
    if not line or line == 'NULL': return None

    root = TreeNode(Symbol('TOP'))
    stack = [root]
    for part in line.rstrip().split():
        m = _bracket_split_notag_re.match(part)

        for c in m.group(1):
            node = TreeNode(Symbol(None))
            stack[-1].insert(node)
            stack.append(node)

        if stack[-1].data.token == None:
            stack[-1].data.token = m.group(2)
            stack[-1].order = len(stack[-1].children)
        else:
            child = TreeNode(Symbol(nonterm=None, term=m.group(2)))
            stack[-1].insert(child)

        for c in m.group(3):
            stack.pop()

    return root.children[0]