-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsimplemrs.py
381 lines (320 loc) · 11.1 KB
/
simplemrs.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
# -*- coding: utf-8 -*-
"""
Serialization functions for the SimpleMRS format.
"""
from pathlib import Path
from delphin.util import Lexer
from delphin import predicate
from delphin.lnk import Lnk
from delphin.sembase import (role_priority, property_priority)
from delphin import variable
from delphin.mrs import (
EP,
HCons,
ICons,
MRS,
MRSSyntaxError,
CONSTANT_ROLE)
CODEC_INFO = {
'representation': 'mrs',
}
TOP_FEATURE = 'TOP'
##############################################################################
##############################################################################
# Pickle-API methods
def load(source):
"""
Deserialize SimpleMRSs from a file (handle or filename)
Args:
source (str, file): input filename or file object
Returns:
a list of MRS objects
"""
if hasattr(source, 'read'):
ms = list(_decode(source))
else:
source = Path(source).expanduser()
with source.open() as fh:
ms = list(_decode(fh))
return ms
def loads(s):
"""
Deserialize SimpleMRS string representations
Args:
s (str): a SimpleMRS string
Returns:
a list of MRS objects
"""
ms = list(_decode(s.splitlines()))
return ms
def dump(ms, destination, properties=True, lnk=True,
indent=False, encoding='utf-8'):
"""
Serialize MRS objects to SimpleMRS and write to a file
Args:
ms: an iterator of MRS objects to serialize
destination: filename or file object where data will be written
properties: if `False`, suppress morphosemantic properties
lnk: if `False`, suppress surface alignments and strings
indent (bool, int): if `True` or an integer value, add
newlines and indentation
encoding (str): if *destination* is a filename, write to the
file with the given encoding; otherwise it is ignored
"""
text = dumps(ms, properties=properties, lnk=lnk, indent=indent)
if hasattr(destination, 'write'):
print(text, file=destination)
else:
destination = Path(destination).expanduser()
with destination.open('w', encoding=encoding) as fh:
print(text, file=fh)
def dumps(ms, properties=True, lnk=True, indent=False):
"""
Serialize MRS objects to a SimpleMRS representation
Args:
ms: an iterator of MRS objects to serialize
properties: if `False`, suppress variable properties
lnk: if `False`, suppress surface alignments and strings
indent (bool, int): if `True` or an integer value, add
newlines and indentation
Returns:
a SimpleMRS string representation of a corpus of MRS objects
"""
return _encode(ms, properties, lnk, indent)
def decode(s):
"""
Deserialize an MRS object from a SimpleMRS string.
"""
lexer = SimpleMRSLexer.lex(s.splitlines())
return _decode_mrs(lexer)
def encode(m, properties=True, lnk=True, indent=False):
"""
Serialize a MRS object to a SimpleMRS string.
Args:
m: an MRS object
properties (bool): if `False`, suppress variable properties
lnk: if `False`, suppress surface alignments and strings
indent (bool, int): if `True` or an integer value, add
newlines and indentation
Returns:
a SimpleMRS-serialization of the MRS object
"""
return _encode([m], properties, lnk, indent)
##############################################################################
##############################################################################
# Deserialization
SimpleMRSLexer = Lexer(
tokens=[
(r'\[', 'LBRACK:['),
(r'\]', 'RBRACK:]'),
(r'<(?:-?\d+[:#]-?\d+'
r'|@\d+'
r'|\d+(?: +\d+)*)>', 'LNK:a lnk value'),
(r'"([^"\\]*(?:\\.[^"\\]*)*)"', 'DQSTRING:a string'),
(r"'([^ \n:<>\[\]])", 'SQSYMBOL:a quoted symbol'),
(r'<', 'LANGLE:<'),
(r'>', 'RANGLE:>'),
(r'([^\s:<>\[\]]+):', 'FEATURE:a feature'),
(r'(?:[^ \n\]<]+'
r'|<(?![-0-9:#@ ]*>\s))+', 'SYMBOL:a symbol'),
(r'[^\s]', 'UNEXPECTED'),
],
error_class=MRSSyntaxError)
LBRACK = SimpleMRSLexer.tokentypes.LBRACK
RBRACK = SimpleMRSLexer.tokentypes.RBRACK
LNK = SimpleMRSLexer.tokentypes.LNK
DQSTRING = SimpleMRSLexer.tokentypes.DQSTRING
SQSYMBOL = SimpleMRSLexer.tokentypes.SQSYMBOL
LANGLE = SimpleMRSLexer.tokentypes.LANGLE
RANGLE = SimpleMRSLexer.tokentypes.RANGLE
FEATURE = SimpleMRSLexer.tokentypes.FEATURE
SYMBOL = SimpleMRSLexer.tokentypes.SYMBOL
def _decode(lineiter):
lexer = SimpleMRSLexer.lex(lineiter)
try:
while lexer.peek():
yield _decode_mrs(lexer)
except StopIteration:
pass
def _decode_mrs(lexer):
top = index = lnk = surface = identifier = None
rels = []
hcons = []
icons = []
variables = {}
lexer.expect_type(LBRACK)
lnk = _decode_lnk(lexer)
surface = lexer.accept_type(DQSTRING)
feature = lexer.accept_type(FEATURE)
while feature is not None:
feature = feature.upper()
if feature in ('LTOP', 'TOP'):
top = lexer.expect_type(SYMBOL).lower()
elif feature == 'INDEX':
index = _decode_variable(lexer, variables)
elif feature == 'RELS':
lexer.expect_type(LANGLE)
while lexer.peek()[0] == LBRACK:
rels.append(_decode_rel(lexer, variables))
lexer.expect_type(RANGLE)
elif feature == 'HCONS':
lexer.expect_type(LANGLE)
while lexer.peek()[0] == SYMBOL:
hcons.append(_decode_cons(lexer, HCons, variables))
lexer.expect_type(RANGLE)
elif feature == 'ICONS':
lexer.expect_type(LANGLE)
while lexer.peek()[0] == SYMBOL:
icons.append(_decode_cons(lexer, ICons, variables))
lexer.expect_type(RANGLE)
else:
raise ValueError('invalid feature: ' + feature)
feature = lexer.accept_type(FEATURE)
lexer.expect_type(RBRACK)
return MRS(top, index, rels, hcons,
icons=icons, variables=variables,
lnk=lnk, surface=surface, identifier=identifier)
def _decode_lnk(lexer):
lnk = lexer.accept_type(LNK)
if lnk is not None:
lnk = Lnk(lnk)
return lnk
def _decode_variable(lexer, variables):
var = lexer.expect_type(SYMBOL).lower()
if var not in variables:
variables[var] = {}
props = variables[var]
if lexer.accept_type(LBRACK):
lexer.accept_type(SYMBOL) # variable type
feature = lexer.accept_type(FEATURE)
while feature is not None:
value = lexer.expect_type(SYMBOL)
props[feature.upper()] = value.lower()
feature = lexer.accept_type(FEATURE)
lexer.expect_type(RBRACK)
return var
def _decode_rel(lexer, variables):
args = {}
surface = None
lexer.expect_type(LBRACK)
pred = predicate.normalize(
lexer.choice_type(DQSTRING, SQSYMBOL, SYMBOL)[1])
lnk = _decode_lnk(lexer)
surface = lexer.accept_type(DQSTRING)
_, label = lexer.expect((FEATURE, 'LBL'), (SYMBOL, None))
# any remaining are arguments or a constant
role = lexer.accept_type(FEATURE)
while role is not None:
role = role.upper()
if role == 'CARG':
value = lexer.expect_type(DQSTRING)
else:
value = _decode_variable(lexer, variables)
args[role] = value
role = lexer.accept_type(FEATURE)
lexer.expect_type(RBRACK)
return EP(pred,
label.lower(),
args=args,
lnk=lnk,
surface=surface,
base=None)
def _decode_cons(lexer, cls, variables):
lhs = _decode_variable(lexer, variables)
relation = lexer.expect_type(SYMBOL).lower()
rhs = _decode_variable(lexer, variables)
return cls(lhs, relation, rhs)
##############################################################################
##############################################################################
# Encoding
def _encode(ms, properties, lnk, indent):
if indent is None or indent is False:
indent = False # normalize None to False
delim = ' '
else:
indent = True # normalize integers to True
delim = '\n'
return delim.join(_encode_mrs(m, properties, lnk, indent) for m in ms)
def _encode_mrs(m, properties, lnk, indent):
delim = '\n ' if indent else ' '
if properties:
varprops = dict(m.variables)
else:
varprops = {}
parts = [
_encode_surface_info(m, lnk),
_encode_hook(m, varprops, indent),
_encode_rels(m.rels, varprops, lnk, indent),
_encode_hcons(m.hcons),
_encode_icons(m.icons, varprops)
]
return '[ {} ]'.format(
delim.join(
' '.join(tokens) for tokens in parts if tokens))
def _encode_surface_info(m, lnk):
tokens = []
if lnk:
if m.lnk:
tokens.append(str(m.lnk))
if m.surface is not None:
tokens.append('"{}"'.format(m.surface))
return tokens
def _encode_hook(m, varprops, indent):
delim = '\n ' if indent else ' '
tokens = []
if m.top is not None:
tokens.append('{}: {}'.format(TOP_FEATURE, m.top))
if m.index is not None:
tokens.append('INDEX: {}'.format(_encode_variable(m.index, varprops)))
if tokens:
tokens = [delim.join(tokens)]
return tokens
def _encode_variable(var, varprops):
tokens = [var]
if varprops.get(var):
tokens.append('[')
tokens.append(variable.type(var))
for prop in sorted(varprops[var], key=property_priority):
val = varprops[var][prop]
tokens.append(prop + ':')
tokens.append(val)
tokens.append(']')
del varprops[var]
return ' '.join(tokens)
def _encode_rels(rels, varprops, lnk, indent):
delim = ('\n ' + ' ' * len('RELS: < ')) if indent else ' '
tokens = []
for rel in rels:
pred = rel.predicate
if lnk:
pred += str(rel.lnk)
reltoks = ['[', pred]
if lnk and rel.surface is not None:
reltoks.append('"{}"'.format(rel.surface))
reltoks.extend(('LBL:', rel.label))
for role in sorted(rel.args, key=role_priority):
arg = rel.args[role]
if role == CONSTANT_ROLE:
arg = '"{}"'.format(arg)
else:
arg = _encode_variable(arg, varprops)
reltoks.extend((role + ':', arg))
reltoks.append(']')
tokens.append(' '.join(reltoks))
if tokens:
tokens = ['RELS: <'] + [delim.join(tokens)] + ['>']
return tokens
def _encode_hcons(hcons):
tokens = ['{} {} {}'.format(hc.hi, hc.relation, hc.lo)
for hc in hcons]
if tokens:
tokens = ['HCONS: <'] + [' '.join(tokens)] + ['>']
return tokens
def _encode_icons(icons, varprops):
tokens = ['{} {} {}'.format(_encode_variable(ic.left, varprops),
ic.relation,
_encode_variable(ic.right, varprops))
for ic in icons]
if tokens:
tokens = ['ICONS: <'] + [' '.join(tokens)] + ['>']
return tokens