| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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() |