Problems with using PyDelphin to generate from an MRS

I had this working earlier, but it isn’t working anymore and I can’t seem to pinpoint why.

I am trying to parse a fragment then generate directly from the MRS I got (since I know that will be well-formed) just to test the generation functionality.

with ace.ACEParser('../erg/erg-2020-osx-0.9.31.dat') as parser:
    phone_response = parser.interact('the phone')
    phone_mrs_resp = phone_response['results'][0]['mrs']
    print(phone_mrs_resp)

# decode it
phone_mrs = simplemrs.decode(phone_mrs_resp)


# generate from it
with ace.ACEGenerator('../erg/erg-2020-osx-0.9.31.dat', ['-r', 'root_frag']) as generator:
    response = generator.interact(phone_mrs)

The output from running this is here:

NOTE: parsed 1 / 1 sentences, avg 1244k, time 0.05594s
[ LTOP: h0 INDEX: e2 [ e SF: prop-or-ques ] RELS: < [ unknown<0:9> LBL: h1 ARG0: e2 ARG: x4 [ x PERS: 3 NUM: sg IND: + ] ]  [ _the_q<0:3> LBL: h5 ARG0: x4 RSTR: h6 BODY: h7 ]  [ _phone_n_1<4:9> LBL: h8 ARG0: x4 ] > HCONS: < h0 qeq h1 h6 qeq h8 > ICONS: < > ]
NOTE: transfer did 0 successful unifies and 0 failed ones
Traceback (most recent call last):
  File "/Users/elizabeth/Documents/sandbox/src/mrs_rewrite.py", line 503, in <module>
    response = generator.interact(phone_mrs)
  File "/Users/elizabeth/.venv/openstream/lib/python3.8/site-packages/delphin/ace.py", line 255, in interact
    validated = self._validate_input(datum)
  File "/Users/elizabeth/.venv/openstream/lib/python3.8/site-packages/delphin/ace.py", line 387, in _validate_input
    return _possible_mrs(datum)
  File "/Users/elizabeth/.venv/openstream/lib/python3.8/site-packages/delphin/ace.py", line 624, in _possible_mrs
    for i, c in enumerate(s):
  File "/Users/elizabeth/.venv/openstream/lib/python3.8/site-packages/delphin/sembase.py", line 164, in __getitem__
    return self._pidx[id]
KeyError: 0

It does successfully parse and give me an MRS (which I printed), but I can’t then feed the same thing back into the generator for some reason, and this error is a little unclear to me so I can’t figure out how to fix it.

That error sounds like you’re trying to run ace in the transfer rules mode, rather than generator mode. Does it have the right flags?

This is because ACEGenerator.interact() expects the string form of the MRS and not the decoded MRS object. The error message is not at all transparent about this, so I opened a new issue for PyDelphin: Attempting to generate from an MRS object needs a better error message · Issue #350 · delph-in/pydelphin · GitHub

Try using the string:

>>> from delphin import ace
>>> parser = ace.ACEParser('../erg-2018.dat')
>>> generator = ace.ACEGenerator('../erg-2018.dat', cmdargs=['-r', 'root_frag'])
>>> response = parser.interact('the phone')
>>> first_result = response.result(0)
>>> # Result.mrs() gets the decoded MRS object
>>> generator.interact(first_result.mrs())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/goodmami/delphin/pydelphin/delphin/ace.py", line 255, in interact
    validated = self._validate_input(datum)
  File "/home/goodmami/delphin/pydelphin/delphin/ace.py", line 387, in _validate_input
    return _possible_mrs(datum)
  File "/home/goodmami/delphin/pydelphin/delphin/ace.py", line 624, in _possible_mrs
    for i, c in enumerate(s):
  File "/home/goodmami/delphin/pydelphin/delphin/sembase.py", line 164, in __getitem__
    return self._pidx[id]
KeyError: 0
>>> # Result['mrs'] gets the original MRS string from ACE
>>> generator.interact(first_result['mrs'])
NOTE: 18 passive, 161 active edges in final generation chart; built 18 passives total. [3 results]
Response({'NOTES': [], 'WARNINGS': [], 'ERRORS': [], 'run': {'run-id': 0, 'application': 'ACE 0.9.34 via PyDelphin v1.6.0', 'environment': '-r root_frag --tsdb-notes --tsdb-stdout --report-labels', 'user': 'goodmami', 'host': 'fw', 'os': 'Linux-5.15.60-1-MANJARO-x86_64-with-glibc2.36', 'start': datetime.datetime(2022, 9, 7, 9, 39, 58, 953318), 'platform': 'gcc 4.2', 'grammar': 'ERG (2018)', 'avms': 10439, 'lexicon': 40320, 'lrules': 112, 'rules': 250}, 'input': '[ LTOP: h0 INDEX: e2 [ e SF: prop-or-ques ] RELS: < [ unknown<0:9> LBL: h1 ARG0: e2 ARG: x4 [ x PERS: 3 NUM: sg IND: + ] ]  [ _the_q<0:3> LBL: h5 ARG0: x4 RSTR: h6 BODY: h7 ]  [ _phone_n_1<4:9> LBL: h8 ARG0: x4 ] > HCONS: < h0 qeq h1 h6 qeq h8 > ICONS: < > ]', 'surface': None, 'results': [{'result-id': 0, 'surface': 'The phone?', 'derivation': '(187 np_frg_c 1.914552 0 2 (186 sp-hd_n_c 1.750455 0 2 (19 the_1 0.000000 0 1 ("the" -1 "token [ +FORM string +FROM string +TO string +ID *diff-list* [ LIST *list* LAST *list* ] +TNT tnt [ +TAGS *list* +PRBS *list* +MAIN tnt_main [ +TAG string +PRB string ] ] +CLASS token_class +TRAIT token_trait [ +UW - +IT italics +LB bracket_list [ LIST *list* LAST *list* ] +RB bracket_list [ LIST *list* LAST *list* ] +LD bracket_list [ LIST *list* LAST *list* ] +RD bracket_list [ LIST *list* LAST *list* ] +HD token_head [ +TI string +LL ctype [ -CTYPE- string ] +TG string ] ] +PRED predsort +CARG string +TICK bool +ONSET c-or-v-onset ]")) (185 w_qmark_plr 1.438052 1 2 (184 n_sg_ilr 0.741561 1 2 (20 phone_n1 0.000000 1 2 ("phone?" -1 "token [ +FORM string +FROM string +TO string +ID *diff-list* [ LIST *list* LAST *list* ] +TNT tnt [ +TAGS *list* +PRBS *list* +MAIN tnt_main [ +TAG string +PRB string ] ] +CLASS token_class +TRAIT token_trait [ +UW - +IT italics +LB bracket_list [ LIST *list* LAST *list* ] +RB bracket_list [ LIST *list* LAST *list* ] +LD bracket_list [ LIST *list* LAST *list* ] +RD bracket_list [ LIST *list* LAST *list* ] +HD token_head [ +TI string +LL ctype [ -CTYPE- string ] +TG string ] ] +PRED predsort +CARG string +TICK bool +ONSET c-or-v-onset ]"))))))', 'mrs': '[ LTOP: h0 INDEX: e2 [ e SF: ques ] RELS: < [ unknown<-1:-1> LBL: h1 ARG0: e2 ARG: x4 [ x PERS: 3 NUM: sg IND: + ] ]  [ _the_q<-1:-1> LBL: h5 ARG0: x4 RSTR: h6 BODY: h7 ]  [ _phone_n_1<-1:-1> LBL: h8 ARG0: x4 ] > HCONS: < h0 qeq h1 h6 qeq h8 > ICONS: < > ]', 'tree': '("XP" ("NP" ("DET" ("the")) ("N" ("N" ("N" ("phone?"))))))', 'score': 1.914552}, {'result-id': 1, 'surface': 'The phone.', 'derivation': '(191 np_frg_c 1.504844 0 2 (190 sp-hd_n_c 1.340747 0 2 (19 the_1 0.000000 0 1 ("the" -1 "token [ +FORM string +FROM string +TO string +ID *diff-list* [ LIST *list* LAST *list* ] +TNT tnt [ +TAGS *list* +PRBS *list* +MAIN tnt_main [ +TAG string +PRB string ] ] +CLASS token_class +TRAIT token_trait [ +UW - +IT italics +LB bracket_list [ LIST *list* LAST *list* ] +RB bracket_list [ LIST *list* LAST *list* ] +LD bracket_list [ LIST *list* LAST *list* ] +RD bracket_list [ LIST *list* LAST *list* ] +HD token_head [ +TI string +LL ctype [ -CTYPE- string ] +TG string ] ] +PRED predsort +CARG string +TICK bool +ONSET c-or-v-onset ]")) (189 w_period_plr 1.276191 1 2 (188 n_sg_ilr 0.069387 1 2 (20 phone_n1 0.000000 1 2 ("phone." -1 "token [ +FORM string +FROM string +TO string +ID *diff-list* [ LIST *list* LAST *list* ] +TNT tnt [ +TAGS *list* +PRBS *list* +MAIN tnt_main [ +TAG string +PRB string ] ] +CLASS token_class +TRAIT token_trait [ +UW - +IT italics +LB bracket_list [ LIST *list* LAST *list* ] +RB bracket_list [ LIST *list* LAST *list* ] +LD bracket_list [ LIST *list* LAST *list* ] +RD bracket_list [ LIST *list* LAST *list* ] +HD token_head [ +TI string +LL ctype [ -CTYPE- string ] +TG string ] ] +PRED predsort +CARG string +TICK bool +ONSET c-or-v-onset ]"))))))', 'mrs': '[ LTOP: h0 INDEX: e2 [ e SF: prop ] RELS: < [ unknown<-1:-1> LBL: h1 ARG0: e2 ARG: x4 [ x PERS: 3 NUM: sg IND: + ] ]  [ _the_q<-1:-1> LBL: h5 ARG0: x4 RSTR: h6 BODY: h7 ]  [ _phone_n_1<-1:-1> LBL: h8 ARG0: x4 ] > HCONS: < h0 qeq h1 h6 qeq h8 > ICONS: < > ]', 'tree': '("XP" ("NP" ("DET" ("the")) ("N" ("N" ("N" ("phone."))))))', 'score': 1.504844}, {'result-id': 2, 'surface': 'The phone', 'derivation': '(183 np_frg_c 0.480413 0 2 (182 sp-hd_n_c 0.316316 0 2 (19 the_1 0.000000 0 1 ("the" -1 "token [ +FORM string +FROM string +TO string +ID *diff-list* [ LIST *list* LAST *list* ] +TNT tnt [ +TAGS *list* +PRBS *list* +MAIN tnt_main [ +TAG string +PRB string ] ] +CLASS token_class +TRAIT token_trait [ +UW - +IT italics +LB bracket_list [ LIST *list* LAST *list* ] +RB bracket_list [ LIST *list* LAST *list* ] +LD bracket_list [ LIST *list* LAST *list* ] +RD bracket_list [ LIST *list* LAST *list* ] +HD token_head [ +TI string +LL ctype [ -CTYPE- string ] +TG string ] ] +PRED predsort +CARG string +TICK bool +ONSET c-or-v-onset ]")) (181 n_sg_ilr 0.412216 1 2 (20 phone_n1 0.000000 1 2 ("phone" -1 "token [ +FORM string +FROM string +TO string +ID *diff-list* [ LIST *list* LAST *list* ] +TNT tnt [ +TAGS *list* +PRBS *list* +MAIN tnt_main [ +TAG string +PRB string ] ] +CLASS token_class +TRAIT token_trait [ +UW - +IT italics +LB bracket_list [ LIST *list* LAST *list* ] +RB bracket_list [ LIST *list* LAST *list* ] +LD bracket_list [ LIST *list* LAST *list* ] +RD bracket_list [ LIST *list* LAST *list* ] +HD token_head [ +TI string +LL ctype [ -CTYPE- string ] +TG string ] ] +PRED predsort +CARG string +TICK bool +ONSET c-or-v-onset ]")))))', 'mrs': '[ LTOP: h0 INDEX: e2 [ e SF: prop-or-ques ] RELS: < [ unknown<-1:-1> LBL: h1 ARG0: e2 ARG: x4 [ x PERS: 3 NUM: sg IND: + ] ]  [ _the_q<-1:-1> LBL: h5 ARG0: x4 RSTR: h6 BODY: h7 ]  [ _phone_n_1<-1:-1> LBL: h8 ARG0: x4 ] > HCONS: < h0 qeq h1 h6 qeq h8 > ICONS: < > ]', 'tree': '("XP" ("NP" ("DET" ("the")) ("N" ("N" ("phone")))))', 'score': 0.480413}], 'readings': 3, 'pedges': 18, 'aedges': 14, 'tgc': 0, 'copies': 103, 'unifications': 40526, 'total': 6, 'treal': 6, 'tcpu': 6, 'others': 661020})

2 Likes