-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_all_paths.py
222 lines (167 loc) · 4.91 KB
/
gen_all_paths.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
from nltk.parse.stanford import StanfordDependencyParser
import re
import graphd
import csv
import nltk
from nltk import word_tokenize
wfile = open("out.txt", "w")
# 0: Root
# 1: Type of Word
# 2: Modifier Relation
class GTree(object):
def __init__(self, modif=None, typ=0, ret=False):
self.children = {}
self.modifier = modif
self.type = typ
self.ret = ret
self.tag = ""
def printTree(self, index=0):
for i in range(index):
wfile.write("\t")
wfile.write(str(self.modifier) + "|" + str(self.ret) + "|" + str(self.tag) + "\n")
index += 1
for ch in self.children:
#print(ch)
self.children[ch].printTree(index)
root = GTree()
eparts = ['a', 'an', 'is', 'the', 'and']
def find_shortest_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not start in graph:
return None
shortest = None
for node in graph[start]:
if node not in path:
newpath = find_shortest_path(graph, node, end, path)
if newpath:
if not shortest or len(newpath) < len(shortest):
shortest = newpath
return shortest
def find_all_paths(graph, start, end, path=[]):
path = path + [start]
if start == end:
return [path]
if not start in graph:
return []
paths = []
for node in graph[start]:
if node not in path:
newpaths = find_all_paths(graph, node, end, path)
for newpath in newpaths:
paths.append(newpath)
return paths
dependency_parser = StanfordDependencyParser()
def getkey(item):
return len(item)
finit = 0
with open("canc.tsv") as tsv:
for line in csv.reader(tsv, dialect="excel-tab"):
if finit == 0:
finit += 1
continue
#print(line)
u_inp = line[0]
options = []
options.append(line[1])
options.append(line[2])
options.append(line[3])
options.append(line[4])
options.append(line[5])
options.append(line[6])
# is good
# is __
# banal one
#
#
fw = ""
for w in options[1].split(" "):
if w in eparts:
u_inp = u_inp.replace("__", eparts[eparts.index(w)] + " __")
else:
options[1] = w
u_inp_m = u_inp.replace("__", options[1])
u_inp = line[10]
#print(u_inp_m)
imp_parts = re.findall(r"\*[^\*]*\*", u_inp)
for i in range(len(imp_parts)):
imp_parts[i] = imp_parts[i][1:-1]
#print(imp_parts)
print(u_inp_m)
print(u_inp)
result = dependency_parser.raw_parse(u_inp_m)
dep = result.__next__()
i_graph = list(dep.triples())
#print(i_graph)
graph = {}
for node in i_graph:
#print(node[0][0] + " " + node[2][0])
if node[0][0] == options[1]:
start = node[0][0] + "_" + node[0][1]
if node[2][0] == options[1]:
start = node[2][0] + "_" + node[2][1]
if node[0][0] in imp_parts:
imp_parts[imp_parts.index(node[0][0])] = node[0][0] + "_" + node[0][1]
if node[2][0] in imp_parts:
imp_parts[imp_parts.index(node[2][0])] = node[2][0] + "_" + node[2][1]
el1 = node[0][0] + "_" + node[0][1]
el2 = node[2][0] + "_" + node[2][1]
if el1 not in graph:
graph[el1] = {}
if el2 not in graph:
graph[el2] = {}
graph[el1][el2] = node[1]
graph[el2][el1] = node[1]
print(options)
print(imp_parts)
print(start)
i_rules = []
i_seq = []
#print(graph)
#print(spg)
for j in range(len(imp_parts)):
#spg = find_shortest_path(graph, start, imp_parts[j])
for spg in find_all_paths(graph, start, imp_parts[j]):
t = []
i_seq.append(spg)
for i in range(len(spg) - 1):
t.append(graph[spg[i]][spg[i + 1]])
i_rules.append(t)
i_rules.sort(key=getkey)
i_seq.sort(key=getkey)
print(i_rules)
print(i_seq)
currnode = root
for i in range(len(i_rules)):
#proper pos bucket
currnode = root
wtype = i_seq[i][0].split("_")[1]
if wtype in currnode.children:
currnode = root.children[wtype]
else:
node = GTree(wtype ,1)
currnode.children[wtype] = node
currnode = currnode.children[wtype]
j = 1
print(len(i_rules[i]))
for rl in i_rules[i]:
#tf = True if rl is last rule
tf = i_rules[i].index(rl) == (len(i_rules[i]) - 1)
tf = (j == len(i_rules[i]))
print("index: " + str(i_rules[i].index(rl)) + " last: " + str((len(i_rules[i]) - 1)) + " j: " + str(j))
if rl in currnode.children:
currnode = currnode.children[rl]
else:
if tf == True:
node = GTree(rl, 2, True)
node.tag = i_seq[i][j].split("_")[1]
else:
print(rl + " |rule not last " + i_seq[i][j] + "| " + i_rules[i][len(i_rules[i]) - 1])
node = GTree(rl, 2)
currnode.children[rl] = node
currnode = currnode.children[rl]
j += 1
#print(root.children['JJ'].children['parataxis'].children['ccomp'].type)
root.printTree()
wfile.close()