Debian Codegraph @

tools/lou_maketable.d/make_suggestions.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 from utils import *
23
24 def update_counters(total, init=False):
25 if not init:
26 sys.stderr.write("\033[1A\033[K")
27 sys.stderr.write("\033[1A\033[K")
28 sys.stderr.write(("%d words processed\n" % total))
29 sys.stderr.flush()
30
31 def main():
32 parser = argparse.ArgumentParser(description="Suggest dictionary rows and liblouis rules")
33 parser.add_argument('-d', '--dictionary', required=True, dest="DICTIONARY",
34 help="dictionary file")
35 parser.add_argument('-t', '--table', required=True, dest="TABLE",
36 help="translation table including hyphenation patterns")
37 parser.add_argument('-n', type=int, default=10, dest="MAX_PROBLEMS",
38 help="maximal number of suggestions")
39 parser.add_argument('--non-interactive', type=bool, default=False, dest="NON_INTERACTIVE",
40 help="commit all suggestions without providing details")
41 parser.add_argument('--print-total-rate', type=bool, default=False, dest="TOTAL_RATE",
42 help="print the total number of correctly translated words (takes longer)")
43 args = parser.parse_args()
44 println("# -*- tab-width: 30; -*-")
45 conn, c = open_dictionary(args.DICTIONARY)
46 c.execute("SELECT text,braille FROM dictionary WHERE braille IS NOT NULL ORDER BY frequency DESC")
47 load_table(args.TABLE)
48 problems = 0
49 correct = 0
50 total = 0
51 update_counters(total, init=True)
52 for text, braille in c.fetchall():
53 problem = False
54 if braille:
55 actual_braille, applied_rules = translate(text)
56 if actual_braille != braille:
57 problem = True
58 if problems < args.MAX_PROBLEMS:
59 comments = []
60 comments.append("wrong braille\t\t" + actual_braille)
61 suggest_rows = []
62 suggest_rules = []
63 non_letters = False
64 if not is_letter(text):
65 non_letters = True
66 comments.append("word has non-letters")
67 comments.append("applied rules")
68 applied_rules = [get_rule(x) for x in applied_rules if x is not None]
69 for rule in applied_rules:
70 comments.append("> " + "\t".join(rule))
71 other_relevant_rules = set(find_relevant_rules(text)) - set(applied_rules)
72 if other_relevant_rules:
73 comments.append("other relevant rules")
74 for rule in other_relevant_rules:
75 comments.append("> " + "\t".join(rule))
76 suggest_rules.append({"opcode": "word", "text": text, "braille": braille})
77 if problem and problems < args.MAX_PROBLEMS:
78 if args.NON_INTERACTIVE and suggest_rules:
79 for rule in suggest_rules:
80 println("%(opcode)s\t%(text)s\t%(braille)s" % rule)
81 else:
82 println("# >>>\t%s\t%s" % (text, braille or ""))
83 for comment in comments:
84 println("# | " + comment)
85 println("# |__")
86 for row in suggest_rows:
87 println("#\t%(text)s\t%(braille)s" % row)
88 for rule in suggest_rules:
89 println("# %(opcode)s\t%(text)s\t%(braille)s" % rule)
90 println()
91 problems += 1
92 if not args.TOTAL_RATE and problems >= args.MAX_PROBLEMS:
93 break
94 else:
95 correct += 1
96 total += 1
97 update_counters(total)
98 if args.TOTAL_RATE:
99 println("### %d out of %d (%.1f %%) words translated correctly" % (correct, total, math.floor(1000 * correct / total) / 10))
100 elif correct > 0:
101 println("### %d words translated correctly" % correct)
102 conn.close()
103
104 if __name__ == "__main__": main()