Debian Codegraph @

tools/lou_maketable.d/submit_rules.py

raw symbols SCIP
1 # liblouis Braille Translation and Back-Translation Library
2 #
3 # Copyright (C) 2017 Bert Frees
4 #
5 # This file is part of liblouis.
6 #
7 # liblouis is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU Lesser General Public License as published
9 # by the Free Software Foundation, either version 2.1 of the License, or
10 # (at your option) any later version.
11 #
12 # liblouis is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with liblouis. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 import argparse
22 import fileinput
23 import re
24 from utils import *
25
26 def main():
27 parser = argparse.ArgumentParser(description="Add contraction rules to contraction table")
28 parser.add_argument('FILE', default=['-'],
29 help="file with unprocessed contraction rules")
30 parser.add_argument('CONTRACTION_TABLE', help="contraction table")
31 parser.add_argument('-t', '--table', required=True,
32 help="translation table for converting to dot patterns", dest="TABLE")
33 args = parser.parse_args()
34 load_table(args.TABLE)
35 p = re.compile(r"^#.*|[ \t]*([+-])?(nocross[ \t]+)?(always|word|begword|endword|midword|begmidword|midendword|prfword|sufword)[ \t]+([^ \t]+)[ \t]+([^ \t\n]+)([ \t]+(.*))?\n?$")
36 rules = []
37 for line in fileinput.FileInput(args.CONTRACTION_TABLE, openhook=fileinput.hook_encoded("utf-8")):
38 m = p.match(line)
39 exit_if_not(m and not m.group(1))
40 _, nocross, opcode, text, braille, _, comment = m.groups()
41 nocross = nocross or ""
42 if opcode:
43 rule = {"opcode": opcode, "nocross": nocross, "text": text, "braille": braille, "comment": comment}
44 rules.append(rule)
45 for line in fileinput.FileInput(args.FILE, openhook=fileinput.hook_encoded("utf-8")):
46 m = p.match(line)
47 if not m:
48 printerrln("%s: rule is not valid" % (line,))
49 exit(1)
50 exit_if_not(m)
51 add_or_delete, nocross, opcode, text, braille, _, _ = m.groups()
52 nocross = nocross or ""
53 if opcode:
54 comment = braille
55 if comment.endswith('\\'):
56 comment = comment + " "
57 braille = to_dot_pattern(braille)
58 rules, rule = partition(lambda rule: rule["opcode"] == opcode and
59 rule["text"] == text and
60 rule["braille"] == braille,
61 rules)
62 rule = list(rule)
63 if add_or_delete == '-':
64 if not rule:
65 printerrln("%s%s %s %s: rule can not be deleted because not in %s" % (nocross,opcode,text,braille,args.CONTRACTION_TABLE))
66 exit(1)
67 else:
68 if rule:
69 printerrln("%s%s %s %s: rule can not be added because already in %s" % (nocross,opcode,text,braille,args.CONTRACTION_TABLE))
70 exit(1)
71 rule = {"opcode": opcode, "nocross": nocross, "text": text, "braille": braille, "comment": comment}
72 rules.append(rule)
73 opcode_order = {"word": 1, "always": 2, "begword": 3, "endword": 4, "midword": 5, "begmidword": 6, "midendword": 7, "prfword": 8, "sufword": 9}
74 for rule in sorted(rules, key=lambda rule: (rule["text"], rule["nocross"], opcode_order[rule["opcode"]])):
75 println(u"{nocross:<9}{opcode:<10} {text:<10} {braille:<30} {comment}".format(**rule))
76
77 if __name__ == "__main__": main()