-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdicos_to_sqlite.py
executable file
·146 lines (118 loc) · 4.35 KB
/
dicos_to_sqlite.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
## This script will parse all DAF dicos as well as reload them
## in MySQL. It will also rebuild the TLFI from the entry pickles.
## It will then process whatever dico(s) you give as an argument.
## If you don't give arguments, it will process every dico.
import sqlite3
import cPickle
import json
import sys
import os
import re
import littre
import feraud
import nicot
import acad1694
import acad1762
import acad1798
import acad1835
import acad1932
def dico_handler(dico):
if dico == 'littre':
dico_dict = littre.parser(dico_path)
elif dico == 'feraud':
dico_dict = feraud.parser(dico_path)
elif dico == 'nicot':
dico_dict = nicot.parser(dico_path)
elif dico == 'acad1694':
dico_dict = acad1694.parser(dico_path)
elif dico == 'acad1762':
dico_dict = acad1762.parser(dico_path)
elif dico == 'acad1798':
dico_dict = acad1798.parser(dico_path)
elif dico == 'acad1835':
dico_dict = acad1835.parser(dico_path)
elif dico == 'acad1932':
dico_dict = acad1932.parser(dico_path)
print "Parsing done"
daf_loader(dico, dico_dict)
def tlfi_loader():
tlfi_dict = {}
tlfi_dir = dico_path + 'tlfi/entry_pickles'
cursor.executescript ('drop table if exists tlfi')
cursor.execute ('create table tlfi (headword text primary key, entry blob)')
## To avoid unwanted newlines in the HTML
remove_divs = re.compile('<div[^<]*>')
remove_divs2 = re.compile('</div>')
## Remove numbers for headwords for merge
remove_nums = re.compile('\d+')
for pickled_file in sorted(os.listdir(tlfi_dir)):
word = pickled_file.decode('utf-8')
input_file = open(tlfi_dir + '/' + word)
entry = cPickle.load(input_file)
entry['content'] = remove_divs.sub('', entry['content'])
entry['content'] = remove_divs2.sub('', entry['content'])
## Remove numbers in headwords
word = remove_nums.sub('', word)
## Just keep the first entry for each headword
if word not in tlfi_dict:
tlfi_dict[word] = {}
tlfi_dict[word] = {'content': [], 'prons': ''}
tlfi_dict[word]['content'].append(entry['content'])
try:
if len(entry['prons']) > 0:
tlfi_dict[word]['prons'] = entry['prons']
except KeyError:
pass
print "Dictionary rebuilding done"
count = 0
for word in tlfi_dict:
count += 1
entry = cPickle.dumps({'content': tlfi_dict[word]['content'], 'prons': tlfi_dict[word]['prons']}, -1)
cursor.execute ("insert into tlfi values (?, ?)", (word.encode('utf-8'), buffer(entry)))
if count == 100:
count = 0
conn.commit()
def daf_loader(table, dico_dict):
cursor.executescript ('drop table if exists ' + table)
cursor.execute ('create table ' + table + ' (headword text primary key, entry blob)')
for headword in dico_dict:
if table != 'littre':
entries = cPickle.dumps({'content': dico_dict[headword]}, -1)
else:
entry = dico_dict[headword]['content']
try:
pron = dico_dict[headword]['pron']
entries = cPickle.dumps({'content': entry, 'prons': pron}, -1)
except KeyError:
entries = cPickle.dumps({'content': entry}, -1)
headword = headword.encode('utf-8')
cursor.execute('insert into ' + table + ' values (?,?)', (headword,buffer(entries)))
del dico_dict
## Specify dico path
dico_path = "../dicos/"
## Dico selection :load all dicos if no arguments
dicos = []
try:
if sys.argv[1]:
for dico in sys.argv[1:]:
dicos.append(dico)
except IndexError:
dicos = ['feraud', 'nicot', 'acad1694', 'acad1762',
'acad1798', 'acad1835', 'acad1932', 'littre', 'tlfi']
## SQLite instantiation
dvlf_path = '/home/clovis/DVLF/'
conn = sqlite3.connect(dvlf_path + 'dvlf.sqlite')
conn.text_factory = str
cursor = conn.cursor()
## Dico parsing and loading
for dico in dicos:
print "\n## Processing %s ##" % dico
if dico != 'tlfi':
dico_handler(dico)
else:
tlfi_loader()
conn.commit()
print 'SQLite load done'
cursor.close()
print "\nAll dicos loaded\n"