Debian Codegraph @

tools/from-morph-test.py

raw
1 #!/usr/bin/python3
2
3 import argparse
4 import base64
5 import hashlib
6 import json
7 import os
8 import yaml
9
10 def hash_line(s):
11 return base64.b64encode(hashlib.sha256(s.encode('utf-8')).digest(), b'-_')[:12].decode('utf-8')
12
13 parser = argparse.ArgumentParser('convert morph-test yaml files to apertium-regtest directories')
14 parser.add_argument('lang', help='ISO code of language')
15 parser.add_argument('yaml', help='file to convert')
16 parser.add_argument('-d', '--dir', help='location of test directory (default: cwd)', default='.')
17 args = parser.parse_args()
18
19 new_tests = {}
20
21 def do_test(name, tst):
22 analysis = []
23 surf = []
24 for k_, v_ in tst.items():
25 k = '^' + k_ + '$'
26 v = v_ if isinstance(v_, str) else v_[0]
27 surf.append((v, hash_line(k)))
28 analysis.append((k, hash_line(v)))
29 if len(surf) == 0:
30 return
31 with open('%s-input.txt' % name, 'w') as fout:
32 surf.sort()
33 for s, h in surf:
34 fout.write(s + '\n')
35 with open('%s-gen-generator-gold.txt' % name, 'w') as fout:
36 surf.sort(key=lambda x: x[1])
37 for s, h in surf:
38 fout.write('[%s]\n%s [/option]\n[/%s]\n' % (h, s, h))
39 with open('%s-gen-input.txt' % name, 'w') as fout:
40 analysis.sort()
41 for a, h in analysis:
42 fout.write(a + '\n')
43 with open('%s-morph-gold.txt' % name, 'w') as fout:
44 analysis.sort(key=lambda x: x[1])
45 for a, h in analysis:
46 fout.write('[%s]\n%s [/option]\n[/%s]\n' % (h, a, h))
47 global new_tests, args
48 new_tests[name] = {
49 'input': '%s-input.txt' % name,
50 'mode': '%s-morph' % args.lang
51 }
52 new_tests[name + '-gen'] = {
53 'input': '%s-gen-input.txt' % name,
54 'mode': '%s-gener' % args.lang
55 }
56
57 with open(args.yaml) as fin:
58 pth = os.path.join(args.dir, 'test')
59 if not os.path.isdir(pth):
60 os.mkdir(pth)
61 os.chdir(pth)
62 blob = yaml.load(fin.read(), yaml.BaseLoader)
63 for name, tst in blob['Tests'].items():
64 do_test(name.replace(' ', '_').replace('/', '_'), tst)
65 if os.path.isfile('tests.json'):
66 with open('tests.json') as fin:
67 new_tests.update(json.loads(fin.read()))
68 with open('tests.json', 'w') as js:
69 js.write(json.dumps(new_tests))