forked from goodmami/demophin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minidelphin.py
1058 lines (896 loc) · 33 KB
/
minidelphin.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# minidelphin : single-module pyDelphin implementation
#
# Copyright (2015) Michael Wayne Goodman <[email protected]>
#
# This file is a subset of pyDelphin. Please see the pyDelphin project
# page for license information:
# http://github.com/goodmami/pydelphin
from __future__ import print_function
import os
import re
import logging
from collections import (OrderedDict, deque, defaultdict, namedtuple)
from itertools import chain
from subprocess import (check_call, CalledProcessError, Popen, PIPE, STDOUT)
IVARG_ROLE = 'ARG0'
CONSTARG_ROLE = 'CARG'
QUANTIFIER_POS = 'q'
var_re = re.compile(r'^([-\w]*\D)(\d+)$')
class PyDelphinException(Exception):
pass
class XmrsError(PyDelphinException):
pass
class XmrsDeserializationError(XmrsError):
pass
class Xmrs(object):
def __init__(self, top=None, index=None, xarg=None,
eps=None, hcons=None, icons=None, vars=None,
lnk=None, surface=None, identifier=None):
self.top = top
self.index = index
self.xarg = xarg
self._nodeids = []
self._eps = {}
self._hcons = {}
self._icons = {}
self._vars = defaultdict(
lambda: {'props': [], 'refs': defaultdict(list)}
)
# just calling __getitem__ will instantiate them on _vars
if top is not None: self._vars[top]
if index is not None: self._vars[index]
if xarg is not None: self._vars[xarg]
if vars is not None:
_vars = self._vars
for var, props in vars.items():
_vars[var]['props'] = props
if eps is not None:
self.add_eps(eps)
if hcons is not None:
self.add_hcons(hcons)
if icons is not None:
self.add_icons(icons)
self.lnk = lnk # Lnk object (MRS-level lnk spans the whole input)
self.surface = surface # The surface string
self.identifier = identifier # Associates an utterance with the RMRS
def add_eps(self, eps):
# (nodeid, pred, label, args, lnk, surface, base)
_nodeids, _eps, _vars = self._nodeids, self._eps, self._vars
for ep in eps:
eplen = len(ep)
if eplen < 3:
raise XmrsError(
'EPs must have length >= 3: (nodeid, pred, label, ...)'
)
nodeid, pred, lbl = ep[0], ep[1], ep[2]
if nodeid in _eps:
raise XmrsError(
'EP already exists in Xmrs: {} ({})'
.format(nodeid, ep[1])
)
_nodeids.append(nodeid)
_eps[nodeid] = ep
if lbl is not None:
_vars[lbl]['refs']['LBL'].append(nodeid)
args = None
if eplen >= 4: args = ep[3]
if args is None: args = {}
for role, val in args.items():
# if the val is not in _vars, it might still be a
# variable; check with var_re
if val in _vars or var_re.match(val):
vardict = _vars[val]
vardict['refs'][role].append(nodeid)
def add_hcons(self, hcons):
# (hi, relation, lo)
_vars = self._vars
_hcons = self._hcons
for hc in hcons:
if len(hc) < 3:
raise XmrsError(
'Handle constraints must have length >= 3: '
'(hi, relation, lo)'
)
hi = hc[0]
lo = hc[2]
if hi in _hcons:
raise XmrsError(
'Handle constraint already exists for hole %s.' % hi
)
_hcons[hi] = hc
# the following should also ensure lo and hi are in _vars
if 'hcrefs' not in _vars[lo]:
_vars[lo]['hcrefs'] = []
for role, refs in _vars[hi]['refs'].items():
for nodeid in refs:
_vars[lo]['hcrefs'].append((nodeid, role, hi))
def add_icons(self, icons):
_vars, _icons = self._vars, self._icons
for ic in icons:
if len(ic) < 3:
raise XmrsError(
'Individual constraints must have length >= 3: '
'(left, relation, right)'
)
left = ic[0]
right = ic[2]
if left not in _icons:
_icons[left] = []
_icons[left].append(ic)
# the following should also ensure left and right are in _vars
if 'icrefs' not in _vars[right]:
_vars[right]['icrefs'] = []
_vars[right]['icrefs'].append(ic)
_vars[left] # just to instantiate if not done yet
def __repr__(self):
if self.surface is not None:
stringform = '"{}"'.format(self.surface)
else:
stringform = ' '.join(ep[1].lemma for ep in self.eps())
return '<Xmrs object ({}) at {}>'.format(stringform, id(self))
def __contains__(self, obj):
return obj in self._eps or obj in self._vars
def __eq__(self, other):
# actual equality is more than isomorphism, all variables and
# things must have the same form, not just the same shape
if not isinstance(other, Xmrs):
return NotImplemented
if ((self.top, self.index, self.xarg) !=
(other.top, other.index, other.xarg)):
return False
a, b = sorted(self.eps()), sorted(other.eps())
if len(a) != len(b) or any(ep1 != ep2 for ep1, ep2 in zip(a, b)):
return False
a, b = sorted(self.hcons()), sorted(other.hcons())
if len(a) != len(b) or any(hc1 != hc2 for hc1, hc2 in zip(a, b)):
return False
a, b = sorted(self.icons()), sorted(other.icons())
if len(a) != len(b) or any(ic1 != ic2 for ic1, ic2 in zip(a, b)):
return False
return True
@property
def ltop(self):
return self.top
@property
def cfrom(self):
cfrom = -1
if self.lnk is not None and self.lnk[0] == 0:
cfrom = self.lnk[1][0]
return cfrom
@property
def cto(self):
cto = -1
if self.lnk is not None and self.lnk[0] == 0:
cfrom = self.lnk[1][1]
return cto
# basic access to internal structures
def ep(self, nodeid): return self._eps[nodeid]
def eps(self, nodeids=None):
if nodeids is None: nodeids = self._nodeids
_eps = self._eps
return [_eps[nodeid] for nodeid in nodeids]
def hcon(self, hi): return self._hcons[hi]
def hcons(self): return list(self._hcons.values())
def icons(self, left=None):
if left is not None:
return self._icons[left]
else:
return list(chain.from_iterable(self._icons.values()))
def variables(self): return list(self._vars)
# access to internal sub-structures
def properties(self, var_or_nodeid):
if var_or_nodeid in self._vars:
return dict(self._vars[var_or_nodeid]['props'])
elif var_or_nodeid in self._eps:
var = self._eps[var_or_nodeid][3].get(IVARG_ROLE)
return dict(self._vars.get(var, {}).get('props', []))
else:
raise KeyError(var_or_nodeid)
def pred(self, nodeid): return self._eps[nodeid][1]
def preds(self, nodeids=None):
if nodeids is None: nodeids = self._nodeids
_eps = self._eps
return [_eps[nid][1] for nid in nodeids]
def label(self, nodeid): return self._eps[nodeid][2]
def labels(self, nodeids=None):
if nodeids is None: nodeids = self._nodeids
_eps = self._eps
return [_eps[nid][2] for nid in nodeids]
def args(self, nodeid): return self._eps[nodeid][3]
# calculated sub-structures
def outgoing_args(self, nodeid):
_vars = self._vars
args = self.args(nodeid)
for arg, val in args.items():
pass
def incoming_args(self, nodeid):
_vars = self._vars
_eps = self._eps
ep = _eps[nodeid]
lbl = ep[2]
iv = ep[3].get(IVARG_ROLE)
in_args = []
if 'hcrefs' in vd:
pass
def labelset(self, label):
return self._vars[label]['refs']['LBL']
def labelset_heads(self, label):
_eps = self._eps
_vars = self._vars
nodeids = {nodeid: _eps[nodeid][3].get(IVARG_ROLE, None)
for nodeid in _vars[label]['refs']['LBL']}
if len(nodeids) <= 1:
return list(nodeids)
ivs = {iv: nodeid for nodeid, iv in nodeids.items() if iv is not None}
out = {n: len(list(filter(ivs.__contains__, _eps[n][3].values())))
for n in nodeids}
# out_deg is 1 for ARG0, but <= 1 because sometimes ARG0 is missing
candidates = [n for n, out_deg in out.items() if out_deg <= 1]
in_ = {}
q = {}
for n in candidates:
iv = nodeids[n]
if iv in _vars:
in_[n] = sum(1 for slist in _vars[iv]['refs'].values()
for s in slist if s in nodeids)
else:
in_[n] = 0
q[n] = 1 if _eps[n][1].is_quantifier() else 0
return sorted(
candidates,
key=lambda n: (
# prefer fewer outgoing args to eps in the labelset
out[n],
# prefer more incoming args from eps in the labelset
-in_[n],
# prefer quantifiers (if it has a labelset > 1, it's a
# compound quantifier, like "nearly all")
-q[n],
# finally sort by the nodeid itself
n
)
)
def subgraph(self, nodeids):
_eps, _vars = self._eps, self._vars
_hcons, _icons = self._hcons, self._icons
top = index = xarg = None
eps = [_eps[nid] for nid in nodeids]
lbls = set(ep[2] for ep in eps)
hcons = []
icons = []
subvars = {}
if self.top:
top = self.top
tophc = _hcons.get(top, None)
if top in lbls:
subvars[top] = {}
elif tophc is not None and tophc[2] in lbls:
subvars[top] = {}
hcons.append(tophc)
else:
top = None # nevermind, set it back to None
# do index after we know if it is an EPs intrinsic variable.
# what about xarg? I'm not really sure.. just put it in
if self.xarg:
xarg = self.xarg
subvars[self.xarg] = _vars[self.xarg]['props']
subvars.update((lbl, {}) for lbl in lbls)
subvars.update(
(var, _vars[var]['props'])
for ep in eps for var in ep[3].values()
if var in _vars
)
if self.index in subvars:
index = self.index
# hcons and icons; only if the targets exist in the new subgraph
for var in subvars:
hc = _hcons.get(var, None)
if hc is not None and hc[2] in lbls:
hcons.append(hc)
for ic in _icons.get(var, []):
if ic[0] in subvars and ic[2] in subvars:
icons.append(ic)
return Xmrs(
top=top, index=index, xarg=xarg,
eps=eps, hcons=hcons, icons=icons, vars=subvars,
lnk=self.lnk, surface=self.surface, identifier=self.identifier
)
def is_connected(self):
nids = set(self._nodeids) # the nids left to find
nidlen = len(nids)
if nidlen == 0:
raise XmrsError('Cannot compute connectedness of an empty Xmrs.')
_eps, _hcons, _vars = self._eps, self._hcons, self._vars
explored = set()
seen = set()
agenda = [next(iter(nids))]
while agenda:
curnid = agenda.pop()
ep = _eps[curnid]
lbl = ep[2]
conns = set()
# labels can be shared, targets of HCONS, or targets of args
if lbl in _vars: # every EP should have a LBL
for refrole, ref in _vars[lbl]['refs'].items():
if refrole == 'hcons':
for hc in ref:
if hc[0] in _vars:
refnids = _vars[hc[0]]['refs'].values()
conns.update(chain.from_iterable(refnids))
elif refrole != 'icons':
conns.update(ref)
for role, var in ep[3].items():
if var not in _vars:
continue
vd = _vars[var]
if IVARG_ROLE in vd['refs']:
conns.update(chain.from_iterable(vd['refs'].values()))
# if 'iv' in vd:
# conns.add(vd['iv'])
# if 'bv' in vd:
# conns.add(vd['bv'])
if var in _hcons:
lo = _hcons[var][2]
if lo in _vars:
conns.update(_vars[lo]['refs']['LBL'])
if 'LBL' in vd['refs']:
conns.update(vd['refs']['LBL'])
explored.add(curnid)
for conn in conns:
if conn not in explored:
agenda.append(conn)
seen.update(conns)
# len(seen) is a quicker check
if len(seen) == nidlen and len(nids.difference(seen)) == 0:
break
return len(nids.difference(seen)) == 0
def is_well_formed(self):
_eps = self._eps
_vars = self._vars
nodeids = self._nodeids
hcons = [_vars[argval]['hcons']
for nid in nodeids
for argval in _eps[nid][3].values()
if 'hcons' in _vars.get(argval, {})]
return (
self.is_connected() and
all(_eps[nid][2] in _vars for nid in nodeids) and
all(lo in _vars and len(_vars[lo]['refs'].get('LBL', [])) > 0
for _, _, lo in hcons)
)
class Pred(namedtuple('Pred', ('type', 'lemma', 'pos', 'sense', 'string'))):
pred_re = re.compile(
r'_?(?P<lemma>.*?)_' # match until last 1 or 2 parts
r'((?P<pos>[a-z])_)?' # pos is always only 1 char
r'((?P<sense>([^_\\]|(?:\\.))+)_)?' # no unescaped _s
r'(?P<end>rel(ation)?)$', # NB only _rel is valid
re.IGNORECASE
)
# Pred types (used mainly in input/output, not internally in pyDelphin)
GRAMMARPRED = 0 # only a string allowed (quoted or not)
REALPRED = 1 # may explicitly define lemma, pos, sense
STRINGPRED = 2 # quoted string form of realpred
def __eq__(self, other):
if other is None:
return False
if isinstance(other, Pred):
other = other.string
return self.string.strip('"\'') == other.strip('"\'')
def __str__ (self):
return self.string
def __repr__(self):
return '<Pred object {} at {}>'.format(self.string, id(self))
def __hash__(self):
return hash(self.string)
@classmethod
def stringpred(cls, predstr):
lemma, pos, sense, end = split_pred_string(predstr.strip('"\''))
return cls(Pred.STRINGPRED, lemma, pos, sense, predstr)
@classmethod
def grammarpred(cls, predstr):
lemma, pos, sense, end = split_pred_string(predstr.strip('"\''))
return cls(Pred.GRAMMARPRED, lemma, pos, sense, predstr)
@staticmethod
def string_or_grammar_pred(predstr):
if predstr.strip('"').lstrip("'").startswith('_'):
return Pred.stringpred(predstr)
else:
return Pred.grammarpred(predstr)
@classmethod
def realpred(cls, lemma, pos, sense=None):
string_tokens = [lemma, pos]
if sense is not None:
sense = str(sense)
string_tokens.append(sense)
predstr = '_'.join([''] + string_tokens + ['rel'])
return cls(Pred.REALPRED, lemma, pos, sense, predstr)
def short_form(self):
return self.string.strip('"').lstrip("'").rsplit('_', 1)[0]
def is_quantifier(self):
return self.pos == QUANTIFIER_POS
def split_pred_string(predstr):
match = Pred.pred_re.search(predstr)
if match is None:
return (predstr, None, None, None)
# _lemma_pos(_sense)?_end
return (match.group('lemma'), match.group('pos'),
match.group('sense'), match.group('end'))
def links(xmrs):
links = []
prelinks = []
_eps = xmrs._eps
_hcons = xmrs._hcons
_vars = xmrs._vars
_pred = xmrs.pred
lsh = xmrs.labelset_heads
lblheads = {v: lsh(v) for v, vd in _vars.items() if 'LBL' in vd['refs']}
top = xmrs.top
if top is not None:
prelinks.append((0, top, None, top, _vars[top]))
for nid, ep in _eps.items():
for role, val in ep[3].items():
if role == IVARG_ROLE or val not in _vars:
continue
prelinks.append((nid, ep[2], role, val, _vars[val]))
for src, srclbl, role, val, vd in prelinks:
if IVARG_ROLE in vd['refs']:
tgtnids = [n for n in vd['refs'][IVARG_ROLE]
if not _pred(n).is_quantifier()]
if len(tgtnids) == 0:
continue # maybe some bad MRS with a lonely quantifier
tgt = tgtnids[0] # what do we do if len > 1?
tgtlbl = _eps[tgt][2]
post = 'EQ' if srclbl == tgtlbl else 'NEQ'
elif val in _hcons:
lbl = _hcons[val][2]
if lbl not in lblheads or len(lblheads[lbl]) == 0:
continue # broken MRS; log this?
tgt = lblheads[lbl][0] # sorted list; first item is most "heady"
post = 'H'
elif 'LBL' in vd['refs']:
if val not in lblheads or len(lblheads[val]) == 0:
continue # broken MRS; log this?
tgt = lblheads[val][0] # again, should be sorted already
post = 'HEQ'
else:
continue # CARGs, maybe?
links.append((src, tgt, role, post))
# now EQ links unattested by arg links
for lbl, heads in lblheads.items():
# I'm pretty sure this does what we want
if len(heads) > 1:
first = heads[0]
for other in heads[1:]:
links.append((first, other, None, 'EQ'))
return sorted(links) #, key=lambda link: (link.start, link.end))
def nodes(xmrs):
"""The list of Nodes."""
nodes = []
_vars = xmrs._vars
_props = xmrs.properties
for p in xmrs.eps():
eplen = len(p)
nid = p[0]
pred = p[1]
args = p[3]
sortinfo = lnk = surface = base = None
iv = args.get(IVARG_ROLE, None)
if iv is not None:
sort, _ = var_re.match(iv).groups()
sortinfo = _props(iv)
sortinfo['cvarsort'] = sort
if eplen >= 5:
lnk = p[4]
if eplen >= 6:
surface = p[5]
if eplen >= 7:
base = p[6]
carg = args.get(CONSTARG_ROLE, None)
nodes.append((nid, pred, sortinfo, lnk, surface, base, carg))
return nodes
def rargname_sortkey(rargname):
# canonical order: LBL ARG* RSTR BODY *-INDEX *-HNDL CARG ...
rargname = rargname.upper()
return (
rargname != 'LBL',
rargname in ('BODY', 'CARG'),
rargname.endswith('HNDL'),
rargname
)
# SimpleMRS codec
# versions are:
# * 1.0 long running standard
# * 1.1 added support for MRS-level lnk, surface and EP-level surface
_default_version = 1.1
_latest_version = 1.1
_top = r'TOP'
_ltop = r'LTOP'
# pretty-print options
_default_mrs_delim = '\n'
def load(fh, single=False, strict=False):
if isinstance(fh, str):
return loads(open(fh, 'r').read(), single=single, strict=strict)
return loads(fh.read(), single=single, strict=strict)
def loads(s, single=False, **kwargs):
ms = deserialize(s)
if single:
return next(ms)
else:
return ms
def dump(fh, ms, single=False, version=_default_version,
pretty_print=False, **kwargs):
print(dumps(ms,
single=single,
version=version,
pretty_print=pretty_print,
**kwargs),
file=fh)
def dumps(ms, single=False, version=_default_version,
pretty_print=False, **kwargs):
if single:
ms = [ms]
return serialize(ms, version=version,
pretty_print=pretty_print, **kwargs)
# for convenience
load_one = lambda fh, **kwargs: load(fh, single=True, **kwargs)
loads_one = lambda s, **kwargs: loads(s, single=True, **kwargs)
dump_one = lambda fh, m, **kwargs: dump(fh, m, single=True, **kwargs)
dumps_one = lambda m, **kwargs: dumps(m, single=True, **kwargs)
# Deserialization
tokenizer = re.compile(r'("[^"\\]*(?:\\.[^"\\]*)*"'
r'|[^\s:#@\[\]<>"]+'
r'|[:#@\[\]<>])')
def tokenize(string):
return deque(tokenizer.findall(string))
def deserialize(string):
# FIXME: consider buffering this so we don't read the whole string at once
tokens = tokenize(string)
while tokens:
yield _read_mrs(tokens)
def _read_mrs(tokens, version=_default_version):
#return read_mrs(tokens)
try:
if tokens[0] != '[':
return None
top = idx = surface = lnk = None
vars_ = {}
tokens.popleft() # [
if tokens[0] == '<':
lnk = _read_lnk(tokens)
if tokens[0].startswith('"'): # and tokens[0].endswith('"'):
surface = tokens.popleft()[1:-1] # get rid of first quotes
if tokens[0] in (_ltop, _top):
tokens.popleft() # LTOP / TOP
tokens.popleft() # :
top = tokens.popleft()
vars_[top] = []
if tokens[0] == 'INDEX':
tokens.popleft() # INDEX
tokens.popleft() # :
idx = tokens.popleft()
vars_[idx] = _read_props(tokens)
rels = _read_rels(tokens, vars_)
hcons = _read_cons(tokens, 'HCONS', vars_)
icons = _read_cons(tokens, 'ICONS', vars_)
tokens.popleft() # ]
# at this point, we could uniquify proplists in vars_, but most
# likely it isn't necessary, and might night harm things if we
# leave potential dupes in there. let's see how it plays out.
m = Xmrs(top=top, index=idx, eps=rels,
hcons=hcons, icons=icons, vars=vars_,
lnk=lnk, surface=surface)
except IndexError:
raise XDE('Invalid MRS: Unexpected termination.')
return m
def _read_props(tokens):
props = []
if tokens[0] == '[':
tokens.popleft() # [
vartype = tokens.popleft() # this gets discarded though
while tokens[0] != ']':
key = tokens.popleft()
tokens.popleft() # :
val = tokens.popleft()
props.append((key, val))
tokens.popleft() # ]
return props
def _read_rels(tokens, vars_):
rels = None
nid = 10000
if tokens[0] == 'RELS':
rels = []
tokens.popleft() # RELS
tokens.popleft() # :
tokens.popleft() # <
while tokens[0] != '>':
rels.append(_read_ep(tokens, nid, vars_))
nid += 1
tokens.popleft() # >
return rels
def _read_ep(tokens, nid, vars_):
# reassign these locally to avoid global lookup
CARG = CONSTARG_ROLE
_var_re = var_re
# begin parsing
tokens.popleft() # [
pred = Pred.string_or_grammar_pred(tokens.popleft())
lnk = _read_lnk(tokens)
surface = label = None
if tokens[0].startswith('"'):
surface = tokens.popleft()[1:-1] # get rid of first quotes
if tokens[0] == 'LBL':
tokens.popleft() # LBL
tokens.popleft() # :
label = tokens.popleft()
vars_[label] = []
args = {}
while tokens[0] != ']':
role = tokens.popleft()
tokens.popleft() # :
val = tokens.popleft()
if _var_re.match(val) is not None and role.upper() != CARG:
props = _read_props(tokens)
if val not in vars_:
vars_[val] = []
vars_[val].extend(props)
args[role] = val
tokens.popleft() # ]
return (nid, pred, label, args, lnk, surface)
def _read_cons(tokens, constype, vars_):
cons = None
if tokens[0] == constype:
cons = []
tokens.popleft() # (H|I)CONS
tokens.popleft() # :
tokens.popleft() # <
while tokens[0] != '>':
left = tokens.popleft()
reln = tokens.popleft()
rght = tokens.popleft()
cons.append((left, reln, rght))
# now just make sure they are in the vars_ dict
vars_.setdefault(left, [])
vars_.setdefault(rght, [])
tokens.popleft() # >
return cons
def _read_lnk(tokens):
# < FROM : TO >
lnk = None
if tokens[0] == '<':
tokens.popleft() # we just checked this is a left angle
if tokens[0] == '>':
pass # empty <> brackets the same as no lnk specified
# character span lnk: [FROM, ':', TO, ...]
elif tokens[1] == ':':
# first 0 is the CHARSPAN type (can be ignored)
lnk = (0, (int(tokens.popleft()), int(tokens[1])))
tokens.popleft() # this should be the colon
tokens.popleft() # and this is the cto
tokens.popleft() # should be '>'
return lnk
# Encoding
def serialize(ms, version=_default_version, pretty_print=False, **kwargs):
delim = '\n' if pretty_print else _default_mrs_delim
output = delim.join(
serialize_mrs(m, version=version, pretty_print=pretty_print)
for m in ms
)
return output
def serialize_mrs(m, version=_default_version, pretty_print=False):
# note that varprops is modified as a side-effect of the lower
# functions
varprops = {v: vd['props'] for v, vd in m._vars.items() if vd['props']}
toks = []
if version >= 1.1:
header_toks = []
if m.lnk is not None:
header_toks.append(serialize_lnk(m.lnk))
if m.surface is not None:
header_toks.append('"{}"'.format(m.surface))
if header_toks:
toks.append(' '.join(header_toks))
if m.top is not None:
toks.append(serialize_argument(
_top if version >= 1.1 else _ltop, m.top, varprops
))
if m.index is not None:
toks.append(serialize_argument(
'INDEX', m.index, varprops
))
delim = ' ' if not pretty_print else '\n '
toks.append('RELS: < {eps} >'.format(
eps=delim.join(serialize_ep(ep, varprops, version=version)
for ep in m.eps())
))
toks += [serialize_hcons(m.hcons())]
icons_ = m.icons()
if version >= 1.1 and icons_: # remove `and icons_` for "ICONS: < >"
toks += [serialize_icons(icons_)]
delim = ' ' if not pretty_print else '\n '
return '{} {} {}'.format('[', delim.join(toks), ']')
def serialize_argument(rargname, value, varprops):
_argument = '{rargname}: {value}{props}'
props = ''
if value in varprops:
props = ' [ {} ]'.format(
' '.join(
[var_re.match(value).group(1)] +
list(map('{0[0]}: {0[1]}'.format, varprops[value]))
)
)
del varprops[value] # only print props once
return _argument.format(
rargname=rargname,
value=str(value),
props=props
)
def serialize_ep(ep, varprops, version=_default_version):
# ('nodeid', 'pred', 'label', 'args', 'lnk', 'surface', 'base')
args = ep[3]
arglist = ' '.join([serialize_argument(rarg, args[rarg], varprops)
for rarg in sorted(args, key=rargname_sortkey)])
if version < 1.1 or len(ep) < 6 or ep[5] is None:
surface = ''
else:
surface = ' "%s"' % ep[5]
lnk = None if len(ep) < 5 else ep[4]
pred = ep[1]
predstr = pred.string
return '[ {pred}{lnk}{surface} LBL: {label}{s}{args} ]'.format(
pred=predstr,
lnk=serialize_lnk(lnk),
surface=surface,
label=str(ep[2]),
s=' ' if arglist else '',
args=arglist
)
def serialize_lnk(lnk):
s = ""
if lnk is not None and lnk[0] == 0:
cfrom, cto = lnk[1]
s = ''.join(['<', str(cfrom), ':', str(cto), '>'])
return s
def serialize_hcons(hcons):
toks = ['HCONS' + ':', '<']
for hc in hcons:
toks.extend(hc)
# reln = hcon[1]
# toks += [hcon[0], rel, str(hcon.lo)]
toks += ['>']
return ' '.join(toks)
def serialize_icons(icons):
toks = ['ICONS' + ':', '<']
for ic in icons:
toks.extend(ic)
# toks += [str(icon.left),
# icon.relation,
# str(icon.right)]
toks += ['>']
return ' '.join(toks)
# ACE interface
class AceProcess(object):
_cmdargs = []
def __init__(self, grm, cmdargs=None, executable=None, env=None, **kwargs):
if not os.path.isfile(grm):
raise ValueError("Grammar file %s does not exist." % grm)
self.grm = grm
self.cmdargs = cmdargs or []
self.executable = executable or 'ace'
self.env = env or os.environ
self._open()
def _open(self):
self._p = Popen(
[self.executable, '-g', self.grm] + self._cmdargs + self.cmdargs,
stdin=PIPE,
stdout=PIPE,
stderr=STDOUT,
env=self.env,
universal_newlines=True
)
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
return False # don't try to handle any exceptions
def send(self, datum):
self._p.stdin.write(datum.rstrip() + '\n')
self._p.stdin.flush()
def receive(self):
return self._p.stdout
def interact(self, datum):
self.send(datum)
result = self.receive()
return result
def read_result(self, result):
return result
def close(self):
self._p.stdin.close()
for line in self._p.stdout:
logging.debug('ACE cleanup: {}'.format(line.rstrip()))
retval = self._p.wait()
return retval
class AceParser(AceProcess):
def receive(self):
response = {
'NOTES': [],
'WARNINGS': [],
'ERRORS': [],
'SENT': None,
'RESULTS': []
}
blank = 0
stdout = self._p.stdout
line = stdout.readline().rstrip()
while True:
if line.strip() == '':
blank += 1
if blank >= 2:
break
elif line.startswith('SENT: ') or line.startswith('SKIP: '):
response['SENT'] = line.split(': ', 1)[1]
elif (line.startswith('NOTE:') or
line.startswith('WARNING') or
line.startswith('ERROR')):
level, message = line.split(': ', 1)
response['{}S'.format(level)].append(message)
else:
mrs, deriv = line.split(' ; ')
response['RESULTS'].append({
'MRS': mrs.strip(),
'DERIV': deriv.strip()
})
line = stdout.readline().rstrip()
return response
class AceGenerator(AceProcess):
_cmdargs = ['-e']
def receive(self):
response = {
'NOTE': None,