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