| 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 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() |