Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor improvements and added test case #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/amr.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
from collections import defaultdict, namedtuple, Counter, Container

from parsimonious.grammar import Grammar
from parsimonious.exceptions import ParseError
from nltk.parse import DependencyGraph

def clean_grammar_file(s):
return re.sub('\n[ \t]+', ' ', re.sub(r'#.*','',s.replace('\t',' ').replace('`','_backtick')))

with open('amr.peg') as inF:
scriptdir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(scriptdir, 'amr.peg')) as inF:
grammar = Grammar(clean_grammar_file(inF.read()))


Expand Down Expand Up @@ -255,7 +257,10 @@ def __init__(self, anno, tokens=None):
self.nodes[TOP]['type'] = 'TOP'
if anno:
self._anno = anno
p = grammar.parse(anno)
try:
p = grammar.parse(anno)
except ParseError as e:
raise AMRSyntaxError('Bad parse: '+str(e))
if p is None:
raise AMRSyntaxError('Well-formedness error in annotation:\n'+anno.strip())
self._analyze(p)
Expand Down Expand Up @@ -553,7 +558,9 @@ def walk(n): # (v / concept...)
:calendar (c2 / country :wiki "Japan"
:name (n2 / name :op1 "Japan"))))''',
''' (d / date-entity :polite +
:time (a / amr-unknown))'''
:time (a / amr-unknown))''',
'''( o / have-01 :ARG0 o :value 2)''',
#'''( o / have-01 :ARG0 o :value -2)''' # causes errors if uncommented
]

sembad_tests = [ # not a syntax error, but malformed in terms of variables
Expand Down