-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparse_analysis.py
164 lines (156 loc) · 4.32 KB
/
parse_analysis.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
#coding=utf8
import sys
import os
import re
class Node:
tag = None
parent = None
index = -1
word = ""
child = []
right = None
left = None
def __init__(self,parent=None,word="",tag="",index=-1):
self.parent = parent
self.word = word
self.tag = tag
self.child = []
self.index = index
self.right = None
self.left = None
def has_child(self,n):
if n in self.child:
return True
return False
def add_child(self,child):
self.child.append(child)
def get_leaf(self):
nl = []
for c in self.child:
if c.index >= 0:
nl.append(c)
else:
nl += c.get_leaf()
return nl
def get_pub_node(self,node):
if not node:
return None
father = node.parent
while True:
if not father:break
sf = self.parent
while True:
if not sf:break
if sf == father:
return father
sf = sf.parent
father = father.parent
return None
class Stack:
items = []
def __init__(self,items=[]):
self.items = items
def push(self,item):
self.items.append(item)
def size(self):
return len(self.items)
def pop(self):
if len(self.items) > 0:
last = self.items[-1]
self.items = self.items[:-1]
return last
else:
return None
def last(self):
if len(self.items) > 0:
return self.items[-1]
else:
return None
def combine(self):
return self.items
def print_node_list(nl):
for n in nl:
if n.parent:
print "******"
print n.index,n.word,n.tag,n.parent.tag
print "child:"
for q in n.child:
print q.tag,
print
else:
print "******"
print n.index,n.word,n.tag,"None"
print "child:"
for q in n.child:
print q.tag,
print
def buildTree(parse):
stack = Stack([])
item = ""
parent = None
left = None
right = None
nl = []
wl = []
word_index = 0
for letter in parse.decode("utf8"):
if letter == "(":
if len(item.strip()) > 0:
item = item.strip().encode("utf8")
item = item.split(" ")
word = ""
tag = ""
index = -1
if len(item) == 2:
#word
tag = item[0].strip()
word = item[1].strip()
index = word_index
word_index += 1
else:
tag = item[0].strip()
node = Node(parent,word,tag,index)
if len(item) == 2:
node.left = left
wl.append(node)
if node.left:
node.left.right = node
left = node
if node.parent:
node.parent.add_child(node)
stack.push(parent)
parent = node
nl.append(node)
item = ""
elif letter == ")":
if len(item.strip()) > 0:
item = item.strip().encode("utf8")
item = item.split(" ")
word = ""
tag = ""
index = -1
if len(item) == 2:
#word
tag = item[0].strip()
word = item[1].strip()
index = word_index
word_index += 1
else:
tag = item[0].strip()
node = Node(parent,word,tag,index)
if len(item) == 2:
node.left = left
wl.append(node)
if node.left:
node.left.right = node
left = node
if node.parent:
node.parent.add_child(node)
nl.append(node)
item = ""
else:
last = stack.pop()
parent = last
else:
item += letter
return nl,wl