forked from MaigoAkisame/MCPDict
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmake.py
executable file
·53 lines (46 loc) · 1.56 KB
/
make.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
#!/usr/bin/env python3
import sqlite3, os, sys
from collections import defaultdict
from time import time
from tables import *
import argparse
parser = argparse.ArgumentParser(description='Create mcpdict database')
parser.add_argument('-c', action='store_true', help='check 同音字頻', required=False)
parser.add_argument('-省', help='province to include', required=False)
args, argv = parser.parse_known_args()
start = time()
dicts = defaultdict(dict)
langs = getLangs(dicts, argv, args)
keys = [f"{lang.簡稱}" for lang in langs]
fields = [f"`{i}`" for i in keys]
CREATE = 'CREATE VIRTUAL TABLE mcpdict USING fts3 (%s)' % (",".join(fields))
INSERT = 'INSERT INTO mcpdict VALUES (%s)'% (','.join('?' * len(keys)))
#db
NAME = '../app/src/main/assets/databases/mcpdict.db'
DIR = os.path.dirname(NAME)
if os.path.exists(NAME): os.remove(NAME)
if not os.path.exists(DIR): os.mkdir(DIR)
conn = sqlite3.connect(NAME)
c = conn.cursor()
for i in keys:
if keys.count(i) > 1:
print(f"{i}重名")
exit()
c.execute(CREATE)
for i in sorted(dicts.keys(), key=cjkorder):
v = list(map(dicts[i].get, keys))
c.execute(INSERT, v)
#info
keys = list(langs[辭典數 if len(keys) > 辭典數 else 1].info.keys())
keys.remove("文件格式")
keys.remove("跳過行數")
CREATE = 'CREATE VIRTUAL TABLE info USING fts3 (%s)' % (",".join(keys))
INSERT = 'INSERT INTO info VALUES (%s)'% (','.join('?' * len(keys)))
c.execute(CREATE)
for lang in langs:
v = list(map(lang.info.get, keys))
c.execute(INSERT, v)
conn.commit()
conn.close()
passed = time() - start
print(f"({len(dicts):5d}) {passed:6.3f} 保存")