|
2 | 2 | from nltk import Tree |
3 | 3 | import os |
4 | 4 | import sys |
| 5 | +import getopt |
5 | 6 |
|
6 | 7 | # sys.path.append('~/Downloads/en') # put where you downloaded the nodebox/linguistics |
7 | 8 |
|
|
13 | 14 | os.environ['STANFORD_PARSER'] = '.' #'~/Downloads/stanford-parser-full-2015-04-20/' |
14 | 15 | os.environ['STANFORD_MODELS'] = '.' #'~/Downloads/stanford-parser-full-2015-04-20/' |
15 | 16 |
|
16 | | -parser = stanford.StanfordParser() |
17 | | - |
18 | 17 | smap = {} |
19 | 18 |
|
20 | 19 |
|
@@ -121,6 +120,8 @@ def get_word(tree): |
121 | 120 |
|
122 | 121 |
|
123 | 122 | def get_root_word(word): |
| 123 | + if word in ['is', 'was']: |
| 124 | + return 'is' |
124 | 125 | return en.verb.present(word) |
125 | 126 |
|
126 | 127 |
|
@@ -283,6 +284,12 @@ def describe(tree): |
283 | 284 | prop, prop_node = describe(pp_node) |
284 | 285 | action_node.set(prop, prop_node) |
285 | 286 |
|
| 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 | + |
286 | 293 | return action, action_node |
287 | 294 |
|
288 | 295 | if matches('( VP ( VB/VBZ/VBP/VPZ/VBD/VBG ) ( S ) )', tree): |
@@ -327,7 +334,7 @@ def describe(tree): |
327 | 334 |
|
328 | 335 | return prop, prop_node |
329 | 336 |
|
330 | | - print "ERROR reading " + str(tree) |
| 337 | + raise ValueError("ERROR reading " + str(tree)) |
331 | 338 |
|
332 | 339 |
|
333 | 340 | def answer(tree): |
@@ -363,21 +370,48 @@ def answer(tree): |
363 | 370 |
|
364 | 371 | print "ERROR answering" |
365 | 372 |
|
| 373 | +def usage(): |
| 374 | + print "Usage: " + sys.argv[0] + " [-d]" |
366 | 375 |
|
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() |
368 | 393 |
|
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 |
379 | 394 | line = raw_input("Enter line: ") |
380 | 395 |
|
| 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 | + |
381 | 415 | # Example: |
382 | 416 | """ |
383 | 417 | Mary went sledding |
|
0 commit comments