-
Notifications
You must be signed in to change notification settings - Fork 0
/
rehydrate.py
executable file
·46 lines (40 loc) · 1.22 KB
/
rehydrate.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
#!/usr/bin/env python3
import os
import sqlite3
DATABASE = 'data/output/rehydrated.sqlite'
try:
os.remove(DATABASE)
except:
pass
conn = sqlite3.connect(DATABASE)
cur = conn.cursor()
cur.execute(
'''
CREATE TABLE sentences (id INTEGER PRIMARY KEY)
''')
cur.execute(
'''
CREATE TABLE links (first INTEGER, second INTEGER, PRIMARY KEY (first, second))
''')
with open('data/tatoeba/sentences.csv', 'rb') as f:
full_line = b''
for line in f:
full_line += line
if full_line.endswith(b'\\\n'):
continue
sentence_id, sentence_lang, text = full_line.split(sep=b'\t', maxsplit=2)
sentence_id = int(sentence_id)
cur.execute('INSERT INTO sentences VALUES (?)', (sentence_id,))
full_line = b''
with open('data/tatoeba/links.csv', 'rb') as f:
full_line = b''
for line in f:
full_line += line
if full_line.endswith(b'\\\n'):
continue
sentence_id, translation_id = full_line.split(sep=b'\t', maxsplit=2)
sentence_id = int(sentence_id)
translation_id = int(translation_id)
cur.execute('INSERT INTO links VALUES (?,?)', (sentence_id, translation_id))
full_line = b''
conn.commit()