-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_duckdb.py
executable file
·112 lines (97 loc) · 3.18 KB
/
create_duckdb.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
import glob
import json
import duckdb
json_files = glob.glob("s3-concepts/*.json")
con = duckdb.connect("concepts.db")
con.execute(
"""
DROP TABLE IF EXISTS concept_related_relations;
DROP TABLE IF EXISTS concept_subconcept_relations;
DROP TABLE IF EXISTS concepts;
"""
)
con.execute(
"""
-- Main concepts table
CREATE TABLE IF NOT EXISTS concepts (
wikibase_id VARCHAR PRIMARY KEY,
preferred_label VARCHAR,
alternative_labels VARCHAR[],
negative_labels VARCHAR[],
description VARCHAR,
definition VARCHAR,
labelled_passages JSON
);
-- Relationship tables with unique constraints
CREATE TABLE IF NOT EXISTS concept_subconcept_relations (
concept_id VARCHAR,
subconcept_id VARCHAR,
FOREIGN KEY (concept_id) REFERENCES concepts(wikibase_id),
FOREIGN KEY (subconcept_id) REFERENCES concepts(wikibase_id),
UNIQUE(concept_id, subconcept_id) -- Prevents duplicate relationships
);
CREATE TABLE IF NOT EXISTS concept_related_relations (
concept_id1 VARCHAR,
concept_id2 VARCHAR,
FOREIGN KEY (concept_id1) REFERENCES concepts(wikibase_id),
FOREIGN KEY (concept_id2) REFERENCES concepts(wikibase_id),
UNIQUE(concept_id1, concept_id2) -- Prevents duplicate relationships
);
"""
)
# First pass: Insert all concepts
for file_path in json_files:
with open(file_path, "r") as f:
data = json.load(f)
# Insert main concept data with ON CONFLICT DO NOTHING for deduplication
con.execute(
"""
INSERT INTO concepts VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
data["wikibase_id"],
data["preferred_label"],
data["alternative_labels"],
data["negative_labels"],
data["description"],
data["definition"],
data["labelled_passages"],
),
)
# Second pass: Insert all relationships
missing_concepts = set()
for file_path in json_files:
with open(file_path, "r") as f:
data = json.load(f)
# Insert subconcept relationships
for subconcept_id in data["subconcept_of"]:
try:
con.execute(
"""
INSERT INTO concept_subconcept_relations (concept_id, subconcept_id)
VALUES (?, ?)
""",
(data["wikibase_id"], subconcept_id),
)
except duckdb.ConstraintException as e:
print(f"Error: {e}")
missing_concepts.add(subconcept_id)
# Insert related concept relationships
for related_id in data["related_concepts"]:
# Only insert if concept_id1 < concept_id2 to avoid duplicates
if data["wikibase_id"] < related_id:
try:
con.execute(
"""
INSERT INTO concept_related_relations (concept_id1, concept_id2)
VALUES (?, ?)
""",
(data["wikibase_id"], related_id),
)
except duckdb.ConstraintException as e:
print(f"Error: {e}")
missing_concepts.add(related_id)
print(f"Done. Found {len(missing_concepts)} missing concept IDs:")
for concept_id in sorted(missing_concepts):
print(f" - {concept_id}")
print("Done")