-
Notifications
You must be signed in to change notification settings - Fork 0
/
safolu-to-sql.py
60 lines (53 loc) · 1.97 KB
/
safolu-to-sql.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
# -*- coding: utf8 -*-
import sys
import json
import sqlite3
from amis import fuzzme
conn = sqlite3.connect('dict-safolu.sq3')
def load_amis():
cur = conn.cursor()
cur.execute('DELETE FROM amis')
conn.commit()
dictionary = json.load(open("dict-safolu.json"))
for word in dictionary:
title = word['title']
cmn = []
for h in word['heteronyms']:
for d in h['definitions']:
cmn.append(d['def'] \
.replace(u'\ufff9\ufffa\ufffb', '') \
.replace('`', '') \
.replace('~', ''))
content = json.dumps(word, ensure_ascii=False) \
.replace(u'\ufff9\ufffa\ufffb', '') \
.replace('"heteronyms"', '"h"') \
.replace('"definitions":', '"d":') \
.replace('"title":', '"t":') \
.replace('"example":', '"e":') \
.replace('"synonyms":', '"s":') \
.replace('"def":', '"f":') \
.replace('`', '') \
.replace('~', '')
cur.execute('INSERT INTO amis VALUES (?,?,?,?)', (title, word.get('stem', title), ' '.join(cmn), content))
conn.commit()
def fuzzy_amis():
cur = conn.cursor()
cur.execute('DELETE FROM fuzzy')
conn.commit()
cur.execute('SELECT DISTINCT title FROM amis')
for row in cur.fetchall():
print row[0]
cur.execute('INSERT INTO fuzzy VALUES (?,?)', (fuzzme(row[0]), row[0]))
conn.commit()
if __name__ == '__main__':
cur = conn.cursor()
cur.execute('DROP TABLE amis');
cur.execute('DROP TABLE fuzzy');
cur.execute('CREATE TABLE amis (title text, stem text, cmn text, json text)')
cur.execute('CREATE INDEX amis_title ON amis (title)')
cur.execute('CREATE INDEX amis_stem ON amis (stem)')
cur.execute('CREATE TABLE fuzzy (fuzz text, amis text)')
cur.execute('CREATE INDEX fuzzy_fuzz ON fuzzy (fuzz)')
conn.commit()
load_amis()
fuzzy_amis()