Repository navigation
Expand file tree
/
Copy pathsurface_graph_parser.py
More file actions
231 lines (215 loc) · 10.1 KB
/
Copy pathsurface_graph_parser.py
File metadata and controls
231 lines (215 loc) · 10.1 KB
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
from nltk import Tree
import chunking as c
def get_lists(chunks):
subject_list = []
object_list = []
relation_list = []
exception_list = []
# print(chunks.pprint())
idx = 0
for chunk in chunks:
# prev_chunk = chunks[idx - 1][0]
# if type(prev_chunk) is str:
# exception_list.append(prev_chunk)
# idx += 1
# continue
if chunk._label == 'NP':
if idx + 1 == len(chunks):
return subject_list, relation_list, object_list, exception_list
post_chunk = chunks[idx + 1]
if str(post_chunk._label).startswith('J'):
lemma = post_chunk[0][0]
pos = post_chunk[0][1]
if pos == 'JKS': # or pos == 'JX'
subject_list.append((chunk, post_chunk, idx))
elif pos == 'JX' and (lemma == '는' or lemma == '은'):
subject_list.append((chunk, post_chunk, idx))
else:
object_list.append((chunk, post_chunk, idx))
elif str(post_chunk._label).startswith('E'):
object_list.append((chunk, post_chunk, idx))
else:
object_list.append((chunk, Tree.fromstring("(N NIL/X/X/X)"), idx))
elif chunk._label == 'VP':
post_chunk = chunks[idx + 1]
if str(post_chunk._label).startswith('E'):
relation_list.append((chunk, post_chunk, idx))
idx += 1
return subject_list, relation_list, object_list, exception_list
def make_triplet(subject_list, relation_list, object_list):
graph = []
triple_set = []
main_exist = False
marked_object_position = []
print(subject_list)
print(object_list)
if len(subject_list) == 1 and len(relation_list) == 1 and len(object_list) == 0:
subject = subject_list[0]
relation = relation_list[0]
s_body = subject[0]
s_affix = subject[1]
r_body = relation[0]
r_affix = relation[1]
triple_set.append((s_body, s_affix, r_body))
triple_set.append((r_body, Tree.fromstring("(T Tense/X/X/X)"), r_affix))
# triple_set.append((s_body, r_body, False))
graph.append(triple_set)
elif len(subject_list) == 1 and len(relation_list) == 1 and len(object_list) == 1:
subject = subject_list[0]
relation = relation_list[0]
object = object_list[0]
s_body = subject[0]
s_affix = subject[1]
r_body = relation[0]
r_affix = relation[1]
o_body = object[0]
o_affix = object[1]
# if r_body[0][1] == 'VCP':
# triple_set.append((s_body, r_body, o_body))
# else:
triple_set.append((s_body, s_affix, r_body))
triple_set.append((r_body, o_affix, o_body))
triple_set.append((r_body, Tree.fromstring("(T Tense/X/X/X)"), r_affix))
graph.append(triple_set)
elif len(subject_list) > 1 and len(object_list) > 1 and len(relation_list) == 1:
relation = relation_list[0]
main_exist = False
r_position = relation[2]
r_body = relation[0]
r_affix = relation[1]
for subject in reversed(subject_list):
s_body = subject[0]
s_affix = subject[1]
s_position = subject[2]
for a in r_affix:
# print(a)
if a[1] == 'EC' or a[1] == 'EF': # 연결어미 / 종결어미 - 트리플 생성
for object in reversed(object_list):
if len(marked_object_position) > 0:
flag_position = min(marked_object_position)
else:
flag_position = 500
o_position = object[2]
o_affix = object[1]
o_body = object[0]
if s_position < o_position < flag_position:
if main_exist is False:
# if o_affix is not False and o_affix[0][1] == 'JKO' or r_body[0][1] == 'VCP':
triple_set.append((s_body, s_affix, r_body))
triple_set.append((r_body, o_affix, o_body))
triple_set.append((r_body, Tree.fromstring("(T Tense/X/X/X)"), r_affix))
# else:
# triple_set.append((s_body, r_body, False))
# triple_set.append((r_body, o_affix, o_body))
marked_object_position.append(object[2])
# print((subject, relation, object))
# main_exist = True
# main_rel = r_body
# else:
# triple_set.append((main_rel, o_affix, o_body))
# marked_object_position.append(o_position)
# print((main_rel, object[1], object))
graph.append(triple_set)
elif len(relation_list) == 0 and len(subject_list) > 0 and len(object_list) > 0:
for subject in subject_list:
s_body = subject[0]
s_affix = subject[1]
s_position = subject[2]
end_object = object_list[len(object_list) - 1]
main_exist = False
if len(marked_object_position) > 0:
flag_position = max(marked_object_position)
else:
flag_position = 0
r_position = end_object[2] + 1 #o_poistion
r_body = ('이', 'VCP')
r_affix = False
end_o_affix = end_object[1]
if end_o_affix is False:
print("TODO: handle this case - (1)")
continue
for a in end_o_affix:
# print(a)
if a[1] == 'EF': # 연결어미 / 종결어미 - 트리플 생성
for object in reversed(object_list):
o_position = object[2]
o_affix = object[1]
o_body = object[0]
if o_position < r_position and o_position > flag_position:
if main_exist is False:
triple_set.append((s_body, s_affix, r_body))
triple_set.append((r_body, o_affix, o_body))
triple_set.append((r_body, Tree.fromstring("(T Tense/X/X/X)"), r_affix))
marked_object_position.append(object[2])
# print((subject, relation, object))
main_exist = True
main_rel = r_body
else:
triple_set.append((main_rel, o_affix, o_body))
marked_object_position.append(o_position)
# print((main_rel, object[1], object))
graph.append(triple_set)
elif len(subject_list) == 1 and len(relation_list) > 0 and len(object_list) > 0:
subject = subject_list[0]
s_body = subject[0]
s_affix = subject[1]
s_position = subject[2]
for relation in relation_list:
main_exist = False
if len(marked_object_position) > 0:
flag_position = max(marked_object_position)
else:
flag_position = 0
r_position = relation[2]
r_body = relation[0]
r_affix = relation[1]
for a in r_affix:
# print(a)
if a[1] == 'EC' or a[1] == 'EF': # 연결어미 / 종결어미 - 트리플 생성
for object in reversed(object_list):
o_position = object[2]
o_affix = object[1]
o_body = object[0]
if o_position < r_position and o_position > flag_position:
if main_exist is False:
# if o_affix is not False and o_affix[0][1] == 'JKO' or r_body[0][1] == 'VCP':
triple_set.append((s_body, s_affix, r_body))
triple_set.append((r_body, o_affix, o_body))
triple_set.append((r_body, Tree.fromstring("(T Tense/X/X/X)"), r_affix))
# else:
# triple_set.append((s_body, s_affix, r_body))
# triple_set.append((r_body, o_affix, o_body))
marked_object_position.append(object[2])
# print((subject, relation, object))
main_exist = True
main_rel = r_body
else:
triple_set.append((main_rel, o_affix, o_body))
marked_object_position.append(o_position)
# print((main_rel, object[1], object))
graph.append(triple_set)
elif len(subject_list) > len(object_list):
print("TODO: handle this case - len(subject_list) > len(object_list)")
elif len(relation_list) > 1 and len(relation_list) > len(object_list) and len(subject_list) == 1:
print(
"TODO: handle this case - len(relation_list) > 1 and len(relation_list) > len(object_list) and len(subject_list) == 1")
else:
print("TODO: we cannot cover this sentence type yet...")
return graph
def print_graph(graph):
for triplet in graph:
for triple in triplet:
print(triple[0], triple[1], triple[2])
print("============================== ")
print("")
sentence = u''
chunks = c.get_chunk_tree(sentence)
chunks.pprint()
subject_list, relation_list, object_list, exception_list = get_lists(chunks)
print("======== chunk to list (distributing roles) ======= ")
print("sbj_list:", subject_list)
print("rel_list:", relation_list)
print("obj_list:", object_list)
print("exp_list:", exception_list)
graph = make_triplet(subject_list, relation_list, object_list)
print_graph(graph)