-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcreate_load_db.py
More file actions
70 lines (55 loc) · 2.02 KB
/
create_load_db.py
File metadata and controls
70 lines (55 loc) · 2.02 KB
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
#!/usr/bin/env python3
"""Create the autocomplete SQLite database from a TSV of names and IDs."""
import os
import sqlite3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", required=True, help="Output database path")
parser.add_argument("-i", "--input", required=True, help="Input TSV path")
args = parser.parse_args()
# Normalize paths
args.output = os.path.abspath(args.output)
args.input = os.path.abspath(args.input)
# Ensure output directory exists
os.makedirs(os.path.dirname(args.output), exist_ok=True)
# Ensure input file exists
if not os.path.exists(args.input):
raise FileNotFoundError(f"Input not found: {args.input}")
database_name = args.output
# Remove existing DB if present
try:
os.remove(database_name)
except FileNotFoundError:
pass
# Create SQLite DB
conn = sqlite3.connect(database_name)
conn.text_factory = str
c = conn.cursor()
print("Creating tables")
c.execute("CREATE TABLE terms(term VARCHAR(255) COLLATE NOCASE)")
c.execute("CREATE TABLE cached_fragments(fragment VARCHAR(255) COLLATE NOCASE)")
c.execute("CREATE TABLE cached_fragment_terms(fragment_id INT, term VARCHAR(255) COLLATE NOCASE)")
row_count = 0
uc_terms = {}
print("Loading node names")
with open(args.input, 'r', encoding="latin-1", errors="replace") as nodeData:
for line in nodeData:
parts = line.rstrip("\n").split("\t")
if len(parts) < 2:
continue
curie, name = parts[0], parts[1]
for term in (name, curie):
if not term:
continue
uc_term = term.upper()
if uc_term not in uc_terms:
c.execute("INSERT INTO terms(term) VALUES(?)", (term,))
uc_terms[uc_term] = 1
row_count += 1
if row_count % 1_000_000 == 0:
print(f"{row_count}...", end="", flush=True)
print("\nCreating indexes")
c.execute("CREATE INDEX idx_terms_term ON terms(term)")
c.execute("CREATE INDEX idx_cached_fragments_fragment ON cached_fragments(fragment)")
conn.commit()
conn.close()