-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkge_mapping.py
More file actions
321 lines (280 loc) · 10.2 KB
/
kge_mapping.py
File metadata and controls
321 lines (280 loc) · 10.2 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# Copyright (c) 2022 Graphcore Ltd. All rights reserved.
"""Provide search and autocomplete for entity IDs."""
import dataclasses
import gzip
import json
import re
import sqlite3
from pathlib import Path
from typing import Callable, List
import numpy as np
import ogb.linkproppred
@dataclasses.dataclass
class Entry:
"""A database entry, corresponding to an entity or relation."""
type: str # "entity" | "relation"
idx: int
wikidata_id: str
wikidata_label: str
@dataclasses.dataclass
class RawData:
"""Utility class for creating a mapping `Database` from a mapping dump and OGB dataset."""
mapping: List[Entry]
relation_count: np.ndarray
head_entity_count: np.ndarray
@classmethod
def load(cls, mapping_path: Path, data_path: Path) -> "RawData":
"""Load raw data from raw inputs.
E.g. `load("ogbl_wikikg2_mapping.jsonl.gz", "data/")`
"""
with gzip.open(mapping_path, "rt") as f:
mapping = [Entry(**json.loads(line)) for line in f]
ogb_data = ogb.linkproppred.LinkPropPredDataset("ogbl-wikikg2", root=data_path)
return cls(
mapping=mapping,
relation_count=np.bincount(ogb_data.graph["edge_reltype"].flatten()),
head_entity_count=np.bincount(
ogb_data.graph["edge_index"][0], minlength=ogb_data.graph["num_nodes"]
),
)
class Database:
"""Uses SQLite FTS3 to provide text search on entity & relation names."""
def __init__(self, path: Path):
self.connection = sqlite3.connect(str(path))
@staticmethod
def build(path: Path, data: RawData) -> None:
connection = sqlite3.connect(str(path))
with connection:
connection.execute("BEGIN TRANSACTION")
# Drop any previous contents
connection.execute("DROP TABLE IF EXISTS mapping")
connection.execute("DROP TABLE IF EXISTS entity_label")
connection.execute("DROP TABLE IF EXISTS relation_label")
# Create main table & FTS index
# Note that we create separate FTS tables for entity & relation for performance reasons
connection.execute(
"CREATE TABLE mapping ("
"type TEXT NOT NULL"
", idx INTEGER NOT NULL"
", wikidata_id TEXT NOT NULL"
", wikidata_label TEXT NOT NULL"
", score REAL NOT NULL"
")"
)
for type in ["entity", "relation"]:
connection.execute(
f"CREATE VIRTUAL TABLE {type}_label USING fts3 (label, id)"
)
connection.execute(
f"CREATE TRIGGER mapping_insert_{type} AFTER INSERT ON mapping"
f" WHEN new.type = '{type}'"
" BEGIN"
f" INSERT INTO {type}_label"
" (docid, label, id) VALUES (new.rowid, new.wikidata_label, new.wikidata_id);"
" END;"
)
connection.executemany(
"INSERT INTO mapping VALUES (:type, :idx, :wikidata_id, :wikidata_label, :score)",
[
dict(
**entry.__dict__,
score=float(
dict(
relation=data.relation_count,
entity=data.head_entity_count,
)[entry.type][entry.idx]
),
)
for entry in data.mapping
],
)
connection.execute(
"CREATE UNIQUE INDEX mapping_index_typeidx ON mapping(type, idx)"
)
connection.close()
@staticmethod
def _entries(cursor: sqlite3.Cursor) -> List[Entry]:
columns = [c[0] for c in cursor.description]
return [Entry(**dict(zip(columns, x))) for x in cursor.fetchall()]
def search(self, query: str, type: str, limit: int) -> List[Entry]:
cursor = self.connection.cursor()
cursor.execute(
"SELECT m.type, m.idx, m.wikidata_id, m.wikidata_label"
" FROM mapping as m"
f" INNER JOIN {type}_label as w"
" WHERE m.rowid = w.docid"
" AND m.type = :type"
+ (f" AND {type}_label MATCH :query" if query else "")
+ " ORDER BY m.score DESC"
" LIMIT :limit",
dict(type=type, query=query, limit=limit),
)
return self._entries(cursor)
def get_entry(self, idx: int, type: str) -> Entry:
cursor = self.connection.cursor()
cursor.execute(
"SELECT m.type, m.idx, m.wikidata_id, m.wikidata_label"
" FROM mapping as m"
" WHERE m.type = ? AND m.idx = ?",
[type, int(idx)],
)
(result,) = self._entries(cursor)
return result
@property
def all_relations(self) -> List[Entry]:
cursor = self.connection.cursor()
cursor.execute(
"SELECT m.type, m.idx, m.wikidata_id, m.wikidata_label"
" FROM mapping as m"
" WHERE m.type = 'relation'"
" ORDER BY m.score DESC"
)
return self._entries(cursor)
@dataclasses.dataclass
class Prediction(Entry):
"""Additional metadata for a tail entity prediction."""
in_training: bool
@dataclasses.dataclass
class Predictions:
"""Render head & relation from a search query, and tail predictions from a model."""
heads: List[Entry]
relations: List[Entry]
tail_predictions: List[Prediction]
STYLE = """
<style>
.kge-q-part {
display: inline-block;
vertical-align: top;
}
.kge-q-part h1 {
font-size: large;
}
.kge-q-part ol {
list-style-type: none;
}
.kge-q-part li {
padding: 0.8em;
margin: 0.2em;
background-color: #eee;
color: #888;
}
.kge-q-part-head li:nth-of-type(1),.kge-q-part-relation li:nth-of-type(1) {
background-color: #aaf;
color: #000;
}
.kge-q-part-tail_predictions li {
color: #000;
}
.kge-q-part a {
color: #008 !important;
}
.kge-tail-present {
outline: solid #4a48 3px;
}
</style>
"""
@staticmethod
def _shorten_text(text: str, limit: int) -> str:
if len(text) < limit:
return text
return f"{text[:limit]}…"
@classmethod
def _render_entry(cls, entry: Entry) -> str:
url = dict(
entity=f"https://www.wikidata.org/wiki/{entry.wikidata_id}",
relation=f"https://www.wikidata.org/wiki/Property:{entry.wikidata_id}",
)[entry.type]
html = (
f"<b>{cls._shorten_text(entry.wikidata_label, 40)}</b>"
f' (<a href="{url}" target=”_blank”>{entry.wikidata_id}</a>)'
)
return html
@classmethod
def _render_part(cls, entries: List[Entry]) -> str:
html = "<ol>"
for entry in entries:
class_ = ""
if isinstance(entry, Prediction) and entry.in_training:
class_ = "kge-tail-present"
html += f'<li class="{class_}">{cls._render_entry(entry)}</li>'
html += "</ol>"
return html
def _repr_html_(self) -> str:
html = self.STYLE
for kind, entries in dict(
head=self.heads,
relation=self.relations,
tail_predictions=self.tail_predictions,
).items():
html += (
f'<div class="kge-q-part kge-q-part-{kind}">'
f'<h1>{kind.capitalize().replace("_", " ")}</h1>'
f"{self._render_part(entries)}</div>"
)
return html
class Predictor:
"""Provide a user-friendly query interface for use in a Jupyter notebook.
Constructing a predictor takes non-negligible time, so we recommend constructing once
rather than inline for each query.
"""
def __init__(
self,
database: Database,
predict: Callable[[int, int], List[int]],
train_hrt: np.ndarray,
n_entity: int,
n_relation_type: int,
n_suggestions: int = 10,
):
self.database = database
self.predict = predict
self.train_hrt_hash = {
int(h)
for h in np.sum(
[
n_entity * n_relation_type,
n_entity,
1,
]
* train_hrt.astype(np.int64),
-1,
)
}
self.n_entity = n_entity
self.n_relation_type = n_relation_type
self.n_suggestions = n_suggestions
def search(self, query: str, type: str) -> List[Entry]:
if query and not query.endswith(" ") and not re.match(r"^(P|Q)\d+$", query):
query = query + "*"
return self.database.search(query, type=type, limit=self.n_suggestions)
def __call__(self, head_query: str, relation_query: str) -> Predictions:
"""Run text search on {head, relation} and a model prediction query for {tail}.
head_query -- str
-- e.g. "england richa" -> "Richard I of England"
e.g. "Q62378" -> "Tower of London"
(note that "*" is appended for word completion unless
the query ends with " " or looks like a WikiData ID)
relation_query -- str -- same syntax (and ID e.g. "P770")
returns -- Predictions -- designed for rendering in Jupyter via `display()`
"""
heads = self.search(head_query, type="entity")
relations = self.search(relation_query, type="relation")
predictions = []
if heads and relations:
head = heads[0].idx
relation = relations[0].idx
predictions = [
Prediction(
**self.database.get_entry(tail, type="entity").__dict__,
in_training=(
(
self.n_entity * self.n_relation_type * head
+ self.n_entity * relation
+ tail
)
in self.train_hrt_hash
),
)
for tail in self.predict(heads[0].idx, relations[0].idx)
]
return Predictions(heads, relations, predictions)