-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnc.py
342 lines (305 loc) · 13.3 KB
/
snc.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
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
"""
Author: Yazen Ghannam
CAP 5636
Fall 2012
Semantic Network Creater
"""
import sys, re
from pyparsing import *
def findList(inList, item):
if item in inList:
return inList
else:
for x in inList:
if isinstance(x, list):
result = findList(x, item)
if result != -1:
return result
return -1
def findListLevels( levels, inList, item):
result = item
for x in range(levels):
result = findList(inList, result)
return result
def toString(list):
string = ""
for x in list:
if x[0] != 'DT':
string += x[1] + "_"
return string.rstrip("01234567890_ ")
def parsePrep(list):
#print list
if list[0] == 'PREP':
if len(list[2]) > 3:
return list[1] + "_" + toString(list[2][1]) + "_" + parsePrep(list[2][3])
else:
return list[1] + "_" + toString(list[2][1])
else:
return " "
def answer(first, second, third):
if first == '?' and second == '?' and third == '?':
for role in data:
if role == 'INANIMATE-CAUSE':
for verb in data[role]:
for theme in data[role][verb]:
print topic, verb, theme
else:
for verb in data[role]:
for theme in data[role][verb]:
print theme, verb, topic
if first == topic and second == '?' and third == '?':
for role in ['INANIMATE-CAUSE']:
for verb in data[role]:
for theme in data[role][verb]:
print first, verb, theme
if first == topic and second != '?' and third == '?':
for role in ['INANIMATE-CAUSE']:
for verb in data[role]:
if second == verb:
for theme in data[role][verb]:
print first, second, theme
if first == topic and second == '?' and third != '?':
for role in ['INANIMATE-CAUSE']:
for verb in data[role]:
for theme in data[role][verb]:
if third == theme:
print first, verb, theme
if first == '?' and second == '?' and third == topic:
for role in ['THEME', 'EXPERIENCE']:
for verb in data[role]:
for theme in data[role][verb]:
print theme, verb, topic
if first == '?' and second != '?' and third == topic:
for role in ['THEME', 'EXPERIENCE']:
for verb in data[role]:
if second == verb:
for theme in data[role][verb]:
print theme, verb, topic
if first != '?' and second == '?' and third == topic:
for role in ['THEME', 'EXPERIENCE']:
for verb in data[role]:
for theme in data[role][verb]:
if first == theme:
print theme, verb, topic
if first != '?' and second != '?' and third != '?':
exists = False
if first == topic:
for verb in data['INANIMATE-CAUSE']:
if second == verb:
for theme in data['INANIMATE-CAUSE'][verb]:
if third == theme:
exists = True
else:
for role in ['THEME', 'EXPERIENCE']:
for verb in data[role]:
if second == verb:
for theme in data[role][verb]:
if first == theme:
exists = True
if(exists):
print "True"
else:
print "False"
def prompt():
input = raw_input("Please enter command: ")
if input == 'quit':
return 0
parameters = input.split(' ')
answer(parameters[0].upper(), parameters[1].upper(), parameters[2].upper())
print
return 1
if __name__ == '__main__':
print 'Hello\n'
topic = sys.argv[1]
filename = sys.argv[2]
topic = topic.upper()
print topic
file = open(filename, 'r')
sentences = []
i = -1
parsing = False
for line in file:
if re.search("NEXT-SENTENCE", line):
i += 1
sentences.append("")
parsing = False
elif re.search("INTERPRETATION STARTS", line):
parsing = True
elif parsing:
sentences[i] += line.strip("\t\n ")
"""
fileOut = open("famine.out", 'w')
for x in sentences:
fileOut.write(x)
fileOut.write("\n\n")
fileOut.close()
#print sentences[0]
"""
# define grammar
# define punctuation literals
LPAR, RPAR, LBRK, RBRK, LBRC, RBRC, VBAR = map(Suppress, "()[]{}|")
sexp = Forward()
sexpList = Group(LPAR + ZeroOrMore(sexp) + RPAR)
sexp << ( Word(alphanums+"-_*/") | sexpList )
sexpGroups = ZeroOrMore(sexp)
data = {"INANIMATE-CAUSE":{}, "THEME":{}, "EXPERIENCE":{}}
count = 0
ierr = 0
terr = 0
perr = 0
for sentence in sentences:
try:
#print sexpGroups.parseString(sentence).asList()
parse = sexpGroups.parseString(sentence).asList()
semantic_role_list = findListLevels(3, parse, topic+'1')
if semantic_role_list != -1 and semantic_role_list[len(semantic_role_list)-1][0] in ['THEME', 'INANIMATE-CAUSE', 'EXPERIENCE']:
semantic_role = semantic_role_list[len(semantic_role_list)-1][0]
else:
semantic_role = None
if semantic_role == 'INANIMATE-CAUSE':
level = 4
topicList = findListLevels(level, parse, topic+'1')
verb = findListLevels(1, topicList, 'MAIN-VERB')
while verb == -1:
level += 1
topicList = findListLevels(level, parse, topic+'1')
verb = findListLevels(1, topicList, 'MAIN-VERB')
verb = verb[1]
if verb not in data[semantic_role]:
data[semantic_role] .update({verb:[]})
themeList = findListLevels(2, parse, 'THEME')
level = 4
topicList = findListLevels(level, parse, topic+'1')
themeList = findListLevels(2, topicList, 'THEME')
while themeList == -1 and topicList != -1:
level += 1
topicList = findListLevels(level, parse, topic+'1')
if topicList != -1:
themeList = findListLevels(2, topicList, 'THEME')
if themeList != -1:
themeIndex = themeList.index(['THEME'])
if len(themeList) < 4:
pass
else:
other = toString(themeList[themeIndex-2])
if other not in data[semantic_role][verb]:
data[semantic_role][verb].append(other)
sense = themeList[themeIndex-1][0][0].rstrip("01234567890- ")
#print parse[0][0], sense
if sense not in data[semantic_role][verb]:
data[semantic_role][verb].append(sense)
#print parse[0][0], verb, other, sense
if semantic_role == "EXPERIENCE":
level = 4
topicList = findListLevels(level, parse, topic+'1')
verb = findListLevels(1, topicList, 'MAIN-VERB')
while verb == -1:
level += 1
topicList = findListLevels(level, parse, topic+'1')
verb = findListLevels(1, topicList, 'MAIN-VERB')
verb = verb[1]
if verb not in data[semantic_role]:
data[semantic_role] .update({verb:[]})
exList = findListLevels(2, parse, 'EXPERIENCER')
level = 4
topicList = findListLevels(level, parse, topic+'1')
exList = findListLevels(2, topicList, 'EXPERIENCER')
while themeList == -1:
level += 1
topicList = findListLevels(level, parse, topic+'1')
exList = findListLevels(2, topicList, 'EXPERIENCER')
if exList != -1:
exIndex = exList.index(['EXPERIENCER'])
if len(exList) < 4:
pass
else:
other = toString(exList[exIndex-2])
if other not in data[semantic_role][verb]:
data[semantic_role][verb].append(other)
sense = exList[exIndex-1][0][0].rstrip("01234567890- ")
#print parse[0][0], sense
if sense not in data[semantic_role][verb]:
data[semantic_role][verb].append(sense)
#print parse[0][0], verb, other, sense
if semantic_role == "THEME":
level = 4
topicList = findListLevels(level, parse, topic+'1')
verb = findListLevels(1, topicList, 'MAIN-VERB')
while verb == -1:
level += 1
topicList = findListLevels(level, parse, topic+'1')
verb = findListLevels(1, topicList, 'MAIN-VERB')
verb = verb[1]
#verb = findListLevels(1, parse, 'MAIN-VERB')[1]
if verb not in data[semantic_role]:
data[semantic_role] .update({verb:[]})
level = 4
topicList = findListLevels(level, parse, topic+'1')
icList = findListLevels(2, topicList, 'INANIMATE-CAUSE')
agList = findListLevels(2, topicList, 'AGENT')
while icList == -1 and agList == -1:
level += 1
topicList = findListLevels(level, parse, topic+'1')
if topicList == -1:
break
icList = findListLevels(2, topicList, 'INANIMATE-CAUSE')
agList = findListLevels(2, topicList, 'AGENT')
if icList != -1:
icIndex = icList.index(['INANIMATE-CAUSE'])
if len(icList) == icIndex+2:# and icList[0] != 'PREP':
#print "otherList",otherList
other = toString(icList[icIndex-2]) + "_" + parsePrep(icList[icIndex+1])
else:
other = toString(icList[icIndex-2])
if other not in data[semantic_role][verb]:
data[semantic_role][verb].append(other)
sense = icList[icIndex-1][0][0].rstrip("01234567890- ")
if sense not in data[semantic_role][verb]:
data[semantic_role][verb].append(sense)
#print parse[0][0], verb, other, sense
if agList != -1:
agIndex = agList.index(['AGENT'])
if len(agList) == agIndex+2:# and agList[0] == 'PREP':
other = toString(agList[agIndex-2]) + "_" + parsePrep(agList[agIndex+1])
else:
other = toString(agList[agIndex-2])
if other not in data[semantic_role][verb]:
data[semantic_role][verb].append(other)
sense = agList[agIndex-1][0][0].rstrip("01234567890- ")
if sense not in data[semantic_role][verb]:
data[semantic_role][verb].append(sense)
#print parse[0][0], verb, other, sense
count += 1
except IndexError:
ierr += 1
#print e
#parse = sexpGroups.parseString(sentence).asList()
#print parse
#semantic_role = findListLevels(3, parse, topic+'1')
#if semantic_role[len(semantic_role)-1][0] in ['THEME', 'INANIMATE-CAUSE', 'EXPERIENCE']:
#semantic_role = semantic_role[len(semantic_role)-1][0]
#print parse[0][0], semantic_role, len(semantic_role)
#print
except TypeError:
terr += 1
#print verbList
#print verb
#parse = sexpGroups.parseString(sentence).asList()
#print parse
#semantic_role = findListLevels(3, parse, topic+'1')
#print parse[0][0], semantic_role
#print
except ParseException:
perr += 1
#print "Could not parse: "
#print sentence
#print
#print sentence
#print data
print ierr, "index errors"
print terr, "type errors"
print perr, "parse errors"
print count, "out of", len(sentences), "sentences parsed\n"
p = 1
while p == 1:
p = prompt()