-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtatoeba2db.py
243 lines (235 loc) · 7.94 KB
/
tatoeba2db.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Convert Tatoeba dumps (https://tatoeba.org/eng/downloads) into a SQLite db.
Copyright (c) 2016 gumblex
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
'''
import os
import io
import sys
import time
import tarfile
import sqlite3
import calendar
cstr = lambda s: None if s == '\\N' else s
cint = lambda s: None if s == '\\N' else int(s)
cdate = lambda s: None if s in ('\\N', '0000-00-00 00:00:00') else calendar.timegm(time.strptime(s, '%Y-%m-%d %H:%M:%S'))
def stream_tarfile(filename):
with tarfile.open(filename, 'r') as tf:
f = tf.extractfile(tf.next())
yield from io.TextIOWrapper(f, 'utf-8', newline='\n')
print('Creating schema')
db = sqlite3.connect('tatoeba.db')
cur = db.cursor()
#cur.execute('PRAGMA journal_mode=WAL')
cur.execute(
'CREATE TABLE IF NOT EXISTS sentences ('
'id INTEGER PRIMARY KEY,'
'lang TEXT,'
'text TEXT,'
'username TEXT,'
'date_added INTEGER,'
'date_modified INTEGER'
')')
cur.execute(
'CREATE INDEX IF NOT EXISTS idx_sentences_lang'
' ON sentences (lang)'
)
cur.execute(
'CREATE TABLE IF NOT EXISTS links ('
'sentence INTEGER,'
'translation INTEGER,'
'PRIMARY KEY (sentence, translation),'
'FOREIGN KEY(sentence) REFERENCES sentences(id),'
'FOREIGN KEY(translation) REFERENCES sentences(id)'
')')
cur.execute(
'CREATE INDEX IF NOT EXISTS idx_links'
' ON links (sentence)'
)
cur.execute(
'CREATE TABLE IF NOT EXISTS tags ('
'sentence INTEGER,'
'tag TEXT,'
'UNIQUE (sentence, tag),'
'FOREIGN KEY(sentence) REFERENCES sentences(id)'
')')
cur.execute(
'CREATE INDEX IF NOT EXISTS idx_tags'
' ON tags (sentence)'
)
cur.execute(
'CREATE TABLE IF NOT EXISTS user_lists ('
'id INTEGER PRIMARY KEY,'
'username TEXT,'
'date_created INTEGER,'
'date_modified INTEGER,'
'list_name TEXT,'
'editable TEXT'
')')
cur.execute(
'CREATE TABLE IF NOT EXISTS sentences_in_lists ('
'list INTEGER,'
'sentence INTEGER,'
'PRIMARY KEY (list, sentence),'
'FOREIGN KEY(list) REFERENCES user_lists(id),'
'FOREIGN KEY(sentence) REFERENCES sentences(id)'
')')
cur.execute(
'CREATE INDEX IF NOT EXISTS idx_sentences_in_lists'
' ON sentences_in_lists (list)'
)
cur.execute(
'CREATE TABLE IF NOT EXISTS jpn_indices ('
'sentence INTEGER,'
'meaning INTEGER,'
'text TEXT,'
'PRIMARY KEY (sentence, meaning),'
'FOREIGN KEY(sentence) REFERENCES sentences(id),'
'FOREIGN KEY(meaning) REFERENCES sentences(id)'
')')
cur.execute(
'CREATE INDEX IF NOT EXISTS idx_jpn_indices'
' ON jpn_indices (sentence)'
)
cur.execute(
'CREATE TABLE IF NOT EXISTS sentences_with_audio ('
'sentence INTEGER,'
'username TEXT,'
'license TEXT,'
'attribution TEXT,'
'UNIQUE (sentence, username),'
'FOREIGN KEY(sentence) REFERENCES sentences(id)'
')')
cur.execute(
'CREATE INDEX IF NOT EXISTS idx_sentences_with_audio'
' ON sentences_with_audio (sentence)'
)
cur.execute(
'CREATE TABLE IF NOT EXISTS user_languages ('
'lang TEXT,'
'skill INTEGER,'
'username TEXT,'
'details TEXT,'
'UNIQUE (lang, username)'
')')
cur.execute(
'CREATE INDEX IF NOT EXISTS idx_user_languages'
' ON user_languages (username)'
)
cur.execute(
'CREATE TABLE IF NOT EXISTS users_sentences ('
'username TEXT,'
# 'lang TEXT,'
'sentence INTEGER,'
'rating INTEGER,'
'date_added INTEGER,'
'date_modified INTEGER,'
'UNIQUE (username, sentence),'
'FOREIGN KEY(sentence) REFERENCES sentences(id)'
')')
cur.execute(
'CREATE INDEX IF NOT EXISTS idx_users_sentences'
' ON users_sentences (sentence)'
)
db.commit()
if os.path.isfile('sentences_detailed.tar.bz2'):
print('Importing sentences_detailed.tar.bz2')
for ln in stream_tarfile('sentences_detailed.tar.bz2'):
l = ln.rstrip('\n').split('\t')
cur.execute(
'REPLACE INTO sentences'
' (id, lang, text, username, date_added, date_modified)'
' VALUES (?, ?, ?, ?, ?, ?)',
(int(l[0]), l[1], l[2], cstr(l[3]), cdate(l[4]), cdate(l[5]))
)
db.commit()
elif os.path.isfile('sentences.tar.bz2'):
print('sentences_detailed.tar.bz2 not found.')
print('Importing sentences.tar.bz2')
for ln in stream_tarfile('sentences.tar.bz2'):
l = ln.rstrip('\n').split('\t')
cur.execute('INSERT OR IGNORE INTO sentences '
'(id, lang, text) VALUES (?, ?, ?)', (int(l[0]), l[1], l[2]))
db.commit()
else:
print('sentences_detailed.tar.bz2 or sentences.tar.bz2 not found.')
sys.exit(1)
if os.path.isfile('links.tar.bz2'):
print('Importing links.tar.bz2')
for ln in stream_tarfile('links.tar.bz2'):
l = ln.rstrip('\n').split('\t')
cur.execute('REPLACE INTO links VALUES (?, ?)', (int(l[0]), int(l[1])))
db.commit()
if os.path.isfile('tags.tar.bz2'):
print('Importing tags.tar.bz2')
for ln in stream_tarfile('tags.tar.bz2'):
l = ln.rstrip('\n').split('\t')
cur.execute('REPLACE INTO tags VALUES (?, ?)', (int(l[0]), l[1]))
db.commit()
if os.path.isfile('user_lists.tar.bz2'):
print('Importing user_lists.tar.bz2')
for ln in stream_tarfile('user_lists.tar.bz2'):
l = ln.rstrip('\n').split('\t')
cur.execute('REPLACE INTO user_lists VALUES (?, ?, ?, ?, ?, ?)',
(int(l[0]), l[1], cdate(l[2]), cdate(l[3]), l[4], l[5]))
db.commit()
if os.path.isfile('sentences_in_lists.tar.bz2'):
print('Importing sentences_in_lists.tar.bz2')
for ln in stream_tarfile('sentences_in_lists.tar.bz2'):
l = ln.rstrip('\n').split('\t')
cur.execute('REPLACE INTO sentences_in_lists VALUES (?, ?)',
(int(l[0]), int(l[1])))
db.commit()
if os.path.isfile('jpn_indices.tar.bz2'):
print('Importing jpn_indices.tar.bz2')
for ln in stream_tarfile('jpn_indices.tar.bz2'):
l = ln.rstrip('\n').split('\t')
cur.execute('REPLACE INTO jpn_indices VALUES (?, ?, ?)',
(int(l[0]), int(l[1]), l[2]))
db.commit()
if os.path.isfile('sentences_with_audio.tar.bz2'):
print('Importing sentences_with_audio.tar.bz2')
for ln in stream_tarfile('sentences_with_audio.tar.bz2'):
l = ln.rstrip('\n').split('\t')
cur.execute('REPLACE INTO sentences_with_audio VALUES (?, ?, ?, ?)',
(int(l[0]), cstr(l[1]), cstr(l[2]), cstr(l[3])))
db.commit()
if os.path.isfile('user_languages.tar.bz2'):
print('Importing user_languages.tar.bz2')
last_line = last_detail = None
for ln in stream_tarfile('user_languages.tar.bz2'):
ln = ln.rstrip('\n')
if ln.endswith('\\'):
if last_detail:
last_detail += ln[:-1] + '\n'
else:
l = ln.split('\t')
last_line = (l[0], cint(l[1]), cstr(l[2]))
last_detail = l[-1][:-1] + '\n'
elif last_detail:
cur.execute('REPLACE INTO user_languages VALUES (?, ?, ?, ?)', last_line + (last_detail + ln,))
last_line = last_detail = None
else:
l = ln.split('\t')
cur.execute('REPLACE INTO user_languages VALUES (?, ?, ?, ?)',
(l[0], cint(l[1]), cstr(l[2]), l[3]))
if last_detail:
cur.execute('REPLACE INTO user_languages VALUES (?, ?, ?, ?)',
last_line + (last_detail,))
db.commit()
if os.path.isfile('users_sentences.csv'):
print('Importing users_sentences.csv')
with open('users_sentences.csv', 'r', encoding='utf-8') as f:
for ln in f:
l = ln.rstrip('\n').split('\t')
cur.execute('REPLACE INTO users_sentences VALUES (?, ?, ?, ?, ?)',
(l[0], int(l[1]), int(l[2]), cdate(l[3]), cdate(l[4])))
db.commit()
cur.execute('PRAGMA optimize')
cur.execute('VACUUM')
db.commit()
print('Done.')