forked from delph-in/pydelphin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrs.py
executable file
·71 lines (63 loc) · 2.12 KB
/
mrs.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
#!/usr/bin/env python3
import sys
import argparse
from delphin.mrs import simplemrs, mrx, dmrx, eds, simpledmrs
from delphin.extra.latex import dmrs_tikz_dependency
mrsformats = {
'simplemrs': simplemrs,
'mrx': mrx,
'dmrx': dmrx,
'eds': eds,
'simpledmrs': simpledmrs
}
extraformats = {
'dmrs-tikz': dmrs_tikz_dependency
}
parser = argparse.ArgumentParser(description="Utility for manipulating MRSs")
subparsers = parser.add_subparsers(dest='command')
convert_parser = subparsers.add_parser('convert', aliases=['c'])
convert_parser.add_argument(
'--from', '-f',
dest='srcfmt',
choices=list(mrsformats.keys())
)
convert_parser.add_argument(
'--to', '-t',
dest='tgtfmt',
choices=list(mrsformats.keys()) + list(extraformats.keys())
)
convert_parser.add_argument('--pretty-print', '-p', action='store_true')
convert_parser.add_argument('--color', '-c', action='store_true')
convert_parser.add_argument('infile', metavar='PATH', nargs='?')
path_parser = subparsers.add_parser('paths', aliases=['p'])
path_parser.add_argument('--format', '-f', choices=list(mrsformats.keys()))
path_parser.add_argument('--depth', '-d', default=-1)
path_parser.add_argument('infile', metavar='PATH', nargs='?')
args = parser.parse_args()
if args.command in ('convert', 'c'):
srcfmt = mrsformats[args.srcfmt]
if args.infile is not None:
ms = srcfmt.load(open(args.infile, 'r'))
else:
ms = srcfmt.loads(sys.stdin.read())
output = ''
if args.tgtfmt in mrsformats:
output = mrsformats[args.tgtfmt].dumps(
ms,
pretty_print=args.pretty_print,
color=args.color
)
elif args.tgtfmt in extraformats:
output = extraformats[args.tgtfmt](ms)
print(output)
elif args.command in ('paths', 'p'):
from delphin.mrs import path as mrspath
if args.infile is not None:
instream = open(args.infile, 'r')
else:
instream = sys.stdin
outstream = sys.stdout
ms = mrsformats[args.format].load(instream)
for m in ms:
paths = list(mrspath.get_paths(m, max_depth=int(args.depth)))
print('\t'.join(paths))