forked from mdtux89/amr-eager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.py
78 lines (68 loc) · 2.26 KB
/
node.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
#!/usr/bin/env python
#coding=utf-8
'''
Definition of Node class. It represents an AMR node in the stack of the transition system.
The variable name and the concept label must have been determined from the token that
generated it (aligned to it).
@author: Marco Damonte ([email protected])
@since: 03-10-16
'''
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class Node:
def __init__(self, token, var = None, concept = None, isConst = None):
assert (type(token) == bool and token == True) or (var is not None and isConst is not None)
if type(token) == bool and token == True: # special case for top node, use token as boolean flag
self.isRoot = True
self.token = None
self.isConst = None
self.constant = None
self.concept = None
self.var = None
else:
self.isRoot = False
self.token = token
if isConst:
self.isConst = True
self.constant = var
self.var = None
else:
self.isConst = False
self.var = var
self.constant = None
if concept == None:
self.concept = None
else:
self.concept = concept.encode('utf-8').strip()
def __eq__(self, other):
return str(self) == str(other)
def __ne__(self, other):
return not(self == other)
def __hash__(self):
return hash((self.__repr__()))
def __repr__(self):
if self.isRoot:
return '<%s %s>' % (
self.__class__.__name__, "TOP")
else:
if self.isConst:
return '<%s %s %s %s>' % (
self.__class__.__name__, "const", self.constant, self.concept)
else:
return '<%s %s %s>' % (
self.__class__.__name__, self.var, self.concept)
def variable(self):
if self.isRoot:
return "TOP"
elif self.isConst:
return self.constant
else:
return self.var
def amrconcept(self):
if self.isRoot:
return ""
elif self.isConst:
return ""
else:
return self.concept