-
Notifications
You must be signed in to change notification settings - Fork 0
/
tatoeba_data.py
executable file
·266 lines (234 loc) · 8 KB
/
tatoeba_data.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
# Alphabet Soup gives language learners easily digestible chunks for practice.
# Copyright 2019-2020 Yorwba
# Alphabet Soup is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# Alphabet Soup is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with Alphabet Soup. If not, see <https://www.gnu.org/licenses/>.
import argparse
import sqlite3
def read_escaped_lines(filename):
with open(filename, newline='\n') as file:
continued_line = ''
for line in file:
if line.endswith('\\\n'):
backslash_count = len(line) - len(line.rstrip('\n').rstrip('\\')) - 1
if backslash_count % 2:
continued_line += line[:-2]+'\n'
continue
continued_line += line[:-1]
yield continued_line
continued_line = ''
if continued_line:
yield continued_line
def split_tsv_line(line):
splits = []
current_split = ''
escaped = False
for char in line:
if escaped:
if char not in {'\\', '\t'}:
current_split += '\\'
current_split += char
escaped = False
elif char == '\\':
escaped = True
elif char == '\t':
if current_split == '\\N':
current_split = None
splits.append(current_split)
current_split = ''
else:
current_split += char
if current_split == '\\N':
current_split = None
splits.append(current_split)
return splits
def read_tatoeba_tsv(filename):
for line in read_escaped_lines(filename):
yield split_tsv_line(line)
def read_user_languages():
c = conn.cursor()
c.execute(
'''
CREATE TABLE IF NOT EXISTS user_languages (
lang text,
level integer,
user text,
details text,
PRIMARY KEY (lang, user))
''')
c.execute(
'''
CREATE INDEX idx_language_users ON user_languages(lang)
''')
c.executemany('INSERT OR IGNORE INTO user_languages VALUES (?,?,?,?)',
((lang, level, user, details)
for user_list in (
'data/tatoeba/user_languages.csv',
'CKs_native_speaker_list.csv')
for lang, level, user, details
in read_tatoeba_tsv(user_list)
if lang and user))
conn.commit()
def read_sentences():
c = conn.cursor()
c.execute(
'''
CREATE TABLE IF NOT EXISTS sentences_detailed (
id integer PRIMARY KEY,
lang text,
text text,
user text,
added date,
modified date)
''')
c.execute(
'''
CREATE INDEX idx_user_sentences ON sentences_detailed(user)
''')
c.executemany('INSERT INTO sentences_detailed VALUES (?,?,?,?,?,?)',
read_tatoeba_tsv('data/tatoeba/sentences_detailed.csv'))
conn.commit()
def read_links():
c = conn.cursor()
c.execute(
'''
CREATE TABLE IF NOT EXISTS links (
sentence_id integer REFERENCES sentences_detailed(id),
translation_id integer REFERENCES sentences_detailed(id))
''')
c.execute(
'''
CREATE INDEX idx_links_sentence ON links(sentence_id)
''')
c.executemany('INSERT INTO links VALUES (?,?)',
read_tatoeba_tsv('data/tatoeba/links.csv'))
conn.commit()
def read_tags():
c = conn.cursor()
c.execute(
'''
CREATE TABLE IF NOT EXISTS tags (
id integer REFERENCES sentences_detailed(id),
name text)
''')
c.executemany('INSERT INTO tags VALUES (?,?)',
read_tatoeba_tsv('data/tatoeba/tags.csv'))
conn.commit()
def read_sentences_with_audio():
c = conn.cursor()
c.execute(
'''
CREATE TABLE IF NOT EXISTS sentences_with_audio (
sentence_id integer REFERENCES sentences_detailed(id),
audio_id integer PRIMARY KEY,
user text REFERENCES sentences_detailed(user),
license text,
attribution text)
''')
c.execute(
'''
CREATE INDEX idx_sentences_with_audio_sentence ON sentences_with_audio(sentence_id)
''')
c.executemany('INSERT OR REPLACE INTO sentences_with_audio VALUES (?,?,?,?,?)',
read_tatoeba_tsv('data/tatoeba/sentences_with_audio.csv'))
conn.commit()
def read_transcriptions():
c = conn.cursor()
c.execute(
'''
CREATE TABLE IF NOT EXISTS transcriptions (
id integer REFERENCES sentences_detailed(id),
lang text REFERENCES sentences_detailed(lang),
script text,
user text REFERENCES sentences_detailed(user),
transcription text,
PRIMARY KEY (id, script))
''')
c.executemany('INSERT OR REPLACE INTO transcriptions VALUES (?,?,?,?,?)',
read_tatoeba_tsv('data/tatoeba/transcriptions.csv'))
conn.commit()
def build_database(args):
from os import remove
try:
remove(args.database)
except FileNotFoundError:
pass
global conn
conn = sqlite3.connect(args.database)
read_user_languages()
read_sentences()
read_links()
read_tags()
read_sentences_with_audio()
read_transcriptions()
def filter_language(args):
lang_tags = args.language.split('-')
lang = lang_tags[0]
if len(lang_tags) == 2:
script = lang_tags[1]
else:
script = ''
global conn
conn = sqlite3.connect(args.database)
c = conn.cursor()
for row in c.execute(
'''
SELECT
s.id,
s.lang,
IFNULL(
(
SELECT t.transcription
FROM transcriptions AS t
WHERE t.id = s.id
AND script = :script
AND user != ''
),
s.text
),
s.user,
s.added,
s.modified
FROM
sentences_detailed AS s
WHERE
s.lang = :lang
AND (
(s.id IN (SELECT id FROM tags WHERE name = 'OK'))
OR (
s.user IN (
SELECT user
FROM user_languages
WHERE lang = :lang
AND level >= :level)))
''',
dict(
lang=lang,
script=script,
level=args.minimum_level)):
id, lang, text, user, added, modified = row
url = 'https://tatoeba.org/eng/sentences/show/'+str(id)
license = 'https://creativecommons.org/licenses/by/2.0/'
print('\t'.join(('tatoeba', url, str(id), license, user or 'unknown user', text)))
def main(argv):
parser = argparse.ArgumentParser(
description='Tatoeba data file parser')
parser.add_argument('command', nargs=1, choices={
'build-database',
'filter-language'})
parser.add_argument('--database', type=str, default='data/tatoeba.sqlite')
parser.add_argument('--minimum-level', type=int, default=5)
parser.add_argument('--language', type=str)
args = parser.parse_args(argv[1:])
globals()[args.command[0].replace('-', '_')](args)
if __name__ == '__main__':
import sys
main(sys.argv)