-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathenglish_conv.py
111 lines (87 loc) · 2.69 KB
/
english_conv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2022 Shewer Lu <[email protected]>
#
# Distributed under terms of the MIT license.
"""
pip3 install leveldb luadata
"""
import os
import sys
import csv
import leveldb
import luadata
import re
def load_csv(csvf):
l = []
with open(csvf, newline='') as csvfile:
rows= csv.DictReader(csvfile)
for row in rows:
if row['word'][0] == '#':
continue
if row['phonetic'] != "":
row['phonetic'] = '[' + row['phonetic'] + ']'
l.append(row)
return l
def load_text(textf):
l=[]
with open(textf,newline='') as textfile:
for line in textfile.readlines():
if line[0] == '#':
continue
row = {}
word,data = line.split('\t',1)
row['word']=word
ll= data.split(';',1)
if len(ll)>1:
row['phonetic'] = ll[0]
row['translation']= ll[1]
else:
row['translation']= ll[0]
l.append(row )
return l
def write_chunk(filename,rows):
with open(filename,'w') as fn:
fn.write('return {\n')
for row in rows:
value= luadata.serialize(row) #.replace('\\n','\n').replace('\\r','\r')
fn.write(value + ',\n')
fn.write('}\n')
def write_leveldb(filename,rows):
db = leveldb.LevelDB(filename,create_if_missing=True)
for row in rows:
key = row['word']
if not key:
continue
if re.findall('[A-Z]',key):
key = key.lower() + '\t' + key
value= luadata.serialize(row).replace('\\n','\n').replace('\\r','\r')
db.Put(key.encode('utf-8'),value.encode('utf-8'))
def main(fn,fmt):
"""TODO: Docstring for main.
:returns: TODO
python conv_file.py file.[txt|csv] [leveldb|chunk] -- default: luac compile to chunk_bin
"""
try:
f,ext = os.path.splitext(fn)
rows = ext == ".csv" and load_csv(fn) or load_text(fn)
if fmt== 'leveldb':
write_leveldb(f,rows)
elif fmt == 'chunk':
write_chunk(f + '.txtl', rows)
else:
write_chunk(f + '.txtll',rows)
print("compile to bin ")
os.system( 'luac -o '+ f + ".txtl " + f + ".txtll && rm " + f +".txtll" )
except ValueError as ve:
return str(ve)
if __name__ == '__main__' :
print(sys.argv,len(sys.argv))
if len(sys.argv) <2:
print('help : python english_conv.py filename [chukn|lezeldb] default: chunk_bin ')
else:
fn= sys.argv[1]
fmt= len(sys.argv)>2 and sys.argv[2]
sys.exit(main(fn,fmt))