Skip to content

Commit 868cda2

Browse files
Add debug flag and grammar rule
1 parent f7acb15 commit 868cda2

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

readai.py

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from nltk import Tree
33
import os
44
import sys
5+
import getopt
56

67
# sys.path.append('~/Downloads/en') # put where you downloaded the nodebox/linguistics
78

@@ -13,8 +14,6 @@
1314
os.environ['STANFORD_PARSER'] = '.' #'~/Downloads/stanford-parser-full-2015-04-20/'
1415
os.environ['STANFORD_MODELS'] = '.' #'~/Downloads/stanford-parser-full-2015-04-20/'
1516

16-
parser = stanford.StanfordParser()
17-
1817
smap = {}
1918

2019

@@ -121,6 +120,8 @@ def get_word(tree):
121120

122121

123122
def get_root_word(word):
123+
if word in ['is', 'was']:
124+
return 'is'
124125
return en.verb.present(word)
125126

126127

@@ -283,6 +284,12 @@ def describe(tree):
283284
prop, prop_node = describe(pp_node)
284285
action_node.set(prop, prop_node)
285286

287+
if matches('( VP ( VB/VBZ/VBP/VPZ/VBD/VBG/VBN ) ( NP ) ( SBAR ) )', tree):
288+
# SBAR at end
289+
sbar, sbar_node = describe(tree[2])
290+
action_node.set(sbar, sbar_node)
291+
292+
286293
return action, action_node
287294

288295
if matches('( VP ( VB/VBZ/VBP/VPZ/VBD/VBG ) ( S ) )', tree):
@@ -327,7 +334,7 @@ def describe(tree):
327334

328335
return prop, prop_node
329336

330-
print "ERROR reading " + str(tree)
337+
raise ValueError("ERROR reading " + str(tree))
331338

332339

333340
def answer(tree):
@@ -363,21 +370,48 @@ def answer(tree):
363370

364371
print "ERROR answering"
365372

373+
def usage():
374+
print "Usage: " + sys.argv[0] + " [-d]"
366375

367-
line = raw_input("Enter line: ")
376+
def main(argv):
377+
378+
debug = False
379+
380+
try:
381+
opts, args = getopt.getopt(argv, "hd",["help","debug"])
382+
except getopt.GetoptError as e:
383+
usage()
384+
sys.exit(2)
385+
for opt, arg in opts:
386+
if opt in ["-h", "help"]:
387+
usage()
388+
sys.exit(2)
389+
if opt in ["-d", "debug"]:
390+
debug = True
391+
392+
parser = stanford.StanfordParser()
368393

369-
while line != 'stop':
370-
sent = list(parser.raw_parse(line))[0]
371-
# Print parse tree
372-
# print sent
373-
if sent[0].label() == "SBARQ":
374-
print answer(sent)
375-
else:
376-
describe(sent)
377-
# Print semantic map
378-
# print smap
379394
line = raw_input("Enter line: ")
380395

396+
while line != 'stop':
397+
sent = list(parser.raw_parse(line))[0]
398+
if debug:
399+
print sent # print parse tree
400+
if sent[0].label() == "SBARQ":
401+
print answer(sent)
402+
else:
403+
try:
404+
describe(sent)
405+
except ValueError as e:
406+
print "Error describing sentence. " + e
407+
if debug:
408+
print smap # print semantic map
409+
line = raw_input("Enter line: ")
410+
411+
412+
if __name__ == "__main__":
413+
main(sys.argv[1:])
414+
381415
# Example:
382416
"""
383417
Mary went sledding

0 commit comments

Comments
 (0)