-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_db.py
57 lines (50 loc) · 1.69 KB
/
gen_db.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
"""
Generate SQLite database answers.db from dataset.md
"""
import sqlite3
def parse_file(file_path):
with open(file_path, 'r') as file:
content = []
mode = 'Q' # always starts from question
for line in file:
stripped_line = line.strip()
if stripped_line.startswith('Q:'):
content.append(stripped_line[3:])
elif stripped_line.startswith('A:'):
content.append(stripped_line[3:])
elif stripped_line == '---':
yield mode, ("\n".join(content)).strip()
content = []
mode = 'A'
elif stripped_line == '-----':
yield mode, "\n".join(content)
content = []
mode = 'Q'
else:
content.append(stripped_line)
def store_in_db(file_path, db_path):
# Connect to the SQLite database
conn = sqlite3.connect(db_path)
cur = conn.cursor()
# Create table if it doesn't exist
cur.execute('''
CREATE TABLE IF NOT EXISTS answers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
question TEXT,
answer TEXT
)
''')
# Parse the file and insert data into the database
question = None
for type, value in parse_file(file_path):
if type == 'Q':
question = value
elif type == 'A' and question is not None:
cur.execute('INSERT INTO answers (question, answer) VALUES (?, ?)', (question, value))
question = None
# Commit the changes and close the connection
conn.commit()
conn.close()
file_path = './datasets/dataset.md'
db_path = 'answers.db'
store_in_db(file_path, db_path)