Debian Codegraph @

tools/lou_maketable.d/submit_rows.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 csv
23 import fileinput
24 from utils import *
25
26 class Reader:
27 def __init__(self, files, encoding):
28 self.reader = csv.reader(fileinput.FileInput(files, openhook=fileinput.hook_encoded(encoding)),
29 delimiter='\t', quoting=csv.QUOTE_NONE)
30 def __iter__(self):
31 return self
32 def __next__(self):
33 row = next(self.reader)
34 if not row or row[0].startswith("#"):
35 return next(self)
36 if not len(row) == 3:
37 printerrln('expected 3 columns, got %s: %s' % (len(row),row))
38 exit(1)
39 if not row[0] == "":
40 printerrln("expected first column to be empty, got '%s'" % (row[0],))
41 exit(1)
42 maybe_chunked_text, braille = row[1:3]
43 maybe_chunked_text = to_lowercase(maybe_chunked_text)
44 text, chunked_text = read_text(maybe_chunked_text)
45 braille = braille if braille != "" else None
46 if braille != None:
47 if '0' in to_dot_pattern(braille).split('-'):
48 printerrln('invalid braille: %s' % (braille,))
49 exit(1)
50 exit_if_not(not chunked_text or validate_chunks(chunked_text))
51 return {'text': text,
52 'braille': braille,
53 'chunked_text': chunked_text}
54
55 def main():
56 parser = argparse.ArgumentParser(description="Submit entries to dictionary.")
57 parser.add_argument('FILE', nargs='*', default=['-'], help="TSV file with entries")
58 parser.add_argument('-d', '--dictionary', required=True, dest="DICTIONARY",
59 help="dictionary file")
60 parser.add_argument('-t', '--table', required=True, dest="TABLE",
61 help="translation table to be used for converting words to lowercase")
62 args = parser.parse_args()
63 load_table(args.TABLE)
64 conn, c = open_dictionary(args.DICTIONARY)
65 reader = Reader(args.FILE, "utf-8")
66 c.execute("CREATE TABLE IF NOT EXISTS dictionary (text TEXT PRIMARY KEY, braille TEXT, chunked_text TEXT, frequency INTEGER)")
67 for row in reader:
68 c.execute("INSERT OR IGNORE INTO dictionary VALUES (:text,:braille,:chunked_text,0)", row)
69 if c.lastrowid == 0:
70 c.execute("UPDATE dictionary SET braille=:braille,chunked_text=:chunked_text WHERE text=:text", row)
71 conn.commit()
72 conn.close()
73
74 if __name__ == "__main__": main()