-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisambiguation.py
341 lines (281 loc) · 11.5 KB
/
disambiguation.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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Interface to add disambiguation tables
"""
from typing import Optional
import pandas as pd
from loguru import logger
from sqlalchemy.schema import AddConstraint, UniqueConstraint
from sqlalchemy.sql.expression import bindparam, select, text, update
from belb.kbs.db import SqlDialects
from belb.kbs.kb import BelbKb
from belb.kbs.query import Queries
from belb.kbs.schema import Tables
from belb.utils import CompressedFileWriter, chunkize, save_json
class DisambiguationModule(CompressedFileWriter):
"""
Interface to add disambiguation tables
"""
def __init__(self, kb: BelbKb, foreign_kb: Optional[BelbKb] = None):
self.kb = kb
self.kb_config = self.kb.schema.kb_config
self.foreign_kb = foreign_kb
def _iter_find_homonyms(
self,
kb: BelbKb,
query_name: Queries,
table_name: Tables,
find_batch_size: int = 1000,
insert_batch_size: int = 10000,
):
assert query_name in [
Queries.IDENTIFIER_HOMONYMS,
Queries.NAME_HOMONYMS,
], "Can use this only with quereis IDENTIFIER_HOMONYMS or NAME_HOMONYMS"
assert table_name in [
Tables.IDENTIFIER_HOMONYMS,
Tables.NAME_HOMONYMS,
], "Can use this only with tables IDENTIFIER_HOMONYMS or NAME_HOMONYMS"
fi_table = kb.schema.get(Tables.FOREIGN_IDENTIFIERS)
for rows in chunkize(kb.query(select(fi_table.c.identifier)), find_batch_size):
logger.debug(
"Query `{}` for {} `foreign identifier`s", query_name, len(
rows)
)
query = self.kb.queries.get(
query_name, foreign_identifiers=[r["identifier"] for r in rows]
)
kb.populate_table_from_query(
query=query,
query_name=query_name,
table_name=table_name,
chunksize=insert_batch_size,
)
def disambiguate_foreign_name_homonyms(self, kb: BelbKb, chunksize: int = 10000):
"""
Handle disambiguation for name homonyms by foreign identifier
"""
logger.info(
"Disambiguating foreign name homonyms (same name, different foreign identifier)..."
)
kb.populate_table_from_query(
query_name=Queries.FOREIGN_NAME_HOMONYMS,
table_name=Tables.FOREIGN_NAME_HOMONYMS,
)
table = self.kb.schema.get(Tables.KB)
stmt = (
update(table)
.where(table.c.uid == bindparam("_uid"))
.values(
{
"disambiguation": table.c.disambiguation
+ "F:"
+ bindparam("_name")
+ self.kb.queries.connector
}
)
)
fi = self.kb.schema.get(Tables.FOREIGN_IDENTIFIERS)
fnh = self.kb.schema.get(Tables.FOREIGN_NAME_HOMONYMS)
query = select(fnh.c.uid.label("uid"), fi.c.name.label("name")).join(
fi, fi.c.identifier == fnh.c.identifier
)
# https://groups.google.com/g/sqlalchemy/c/xue9j34XSzk
for rows in chunkize(kb.query(query), chunksize):
for r in rows:
r["_uid"] = r.pop("uid")
r["_name"] = r.pop("name")
if r["_name"] is None:
logger.warning(
"Could not find foreign name for uid: {}", r["_uid"])
r["_name"] = ""
kb.connection.execute(stmt, rows)
kb.connection.commit()
def update_name_homonyms(self, kb: BelbKb, chunksize: int = 10000):
"""
Add disambiguation names to table `NAME_HOMONYMS`
"""
table = self.kb.schema.get(Tables.NAME_HOMONYMS)
stmt = (
update(table)
.where(table.c.uid == bindparam("_uid"))
.values(disambiguation_name=bindparam("disambiguation_name"))
)
for query_name in [
Queries.NON_SYMBOL_DISAMBIGUATION,
Queries.SYMBOL_DISAMBIGUATION,
]:
logger.info("Query: `{}`...", query_name)
query = self.kb.queries.get(query_name)
for rows in chunkize(kb.query(query), chunksize):
parsed_rows = [
self.kb.queries.parse_result(name=query_name, row=r) for r in rows
]
for r in parsed_rows:
assert isinstance(r, dict)
# https://docs.sqlalchemy.org/en/14/tutorial/data_update.html#updating-and-deleting-rows-with-core
r["_uid"] = r.pop("uid")
kb.connection.execute(stmt, parsed_rows)
kb.connection.commit()
def disambiguate_name_homonyms(
self,
kb: BelbKb,
find_batch_size: int = 1000,
insert_batch_size: int = 10000,
):
"""
Find name homonyms and create disambiguation names
"""
logger.info(
"Disambiguating name homonyms (same name, different identifier[+foreign identifier])..."
)
query_name = Queries.NAME_HOMONYMS
table_name = Tables.NAME_HOMONYMS
if self.kb_config.foreign_identifier:
self._iter_find_homonyms(
query_name=query_name,
table_name=table_name,
kb=kb,
find_batch_size=find_batch_size,
insert_batch_size=insert_batch_size,
)
else:
kb.populate_table_from_query(
query_name=query_name,
table_name=table_name,
chunksize=insert_batch_size,
)
self.update_name_homonyms(kb=kb, chunksize=insert_batch_size)
table = self.kb.schema.get(Tables.KB)
nh = self.kb.schema.get(Tables.NAME_HOMONYMS)
stmt = (
update(table)
.where(table.c.uid == bindparam("_uid"))
.values(
{
"disambiguation": table.c.disambiguation
+ "D:"
+ bindparam("disambiguation_name")
+ self.kb.queries.connector
}
)
)
query = select(nh.c.uid, nh.c.disambiguation_name)
for rows in chunkize(kb.query(query), insert_batch_size):
# https://docs.sqlalchemy.org/en/14/tutorial/data_update.html#updating-and-deleting-rows-with-core
for r in rows:
r["_uid"] = r.pop("uid")
# for N ambiguous names, we only need N-1 disambiguation names.
# If one ambiguous name does not provide a disambiguation name
# (e.g. it is the only name associated to the given identifier)
# we can still guarantee uniqueness with `disambiguation`.
rows = [r for r in rows if r["disambiguation_name"] is not None]
kb.connection.execute(stmt, rows)
kb.connection.commit()
def add_attribute_disambiguation(self, kb: BelbKb, chunksize: int = 10000):
"""
Add `attribute_name` to those entries which are still ambiguous
aftern `disambiguation_name` and `foreign_name` have been added
"""
table = kb.schema.get(Tables.KB)
stmt = (
update(table)
.where(table.c.uid == bindparam("_uid"))
.values(
{
"disambiguation": table.c.disambiguation
+ "A:"
+ bindparam("_attribute")
}
)
)
query = kb.queries.get(Queries.ATTRIBUTE_DISAMBIGUATION)
for rows in chunkize(kb.query(query), chunksize):
parsed_rows = [
kb.queries.parse_result(
name=Queries.ATTRIBUTE_DISAMBIGUATION, row=row)
for row in rows
]
parsed_rows = [r for rows in parsed_rows for r in rows]
for r in parsed_rows:
assert isinstance(r, dict)
r["_uid"] = r.pop("uid")
r["_attribute"] = r.pop("attribute")
kb.connection.execute(stmt, parsed_rows)
kb.connection.commit()
def _sqlite_update_contraint(self, kb: BelbKb):
"""
Sqlite does not support UPDATE CONSTRAINT:
1. Modify table name
2. Create new table w/ new/modified constraint
3. Copy data from original table
4. Delete original table
"""
logger.info(
"SQLite does not support `UPDATE` constraint: copying data to new table..."
)
table = self.kb.schema.get(Tables.KB)
# 1. rename the old table and remove associated stuff
kb.connection.execute(
text(f"ALTER TABLE {table.name} RENAME TO {table.name}_backup")
)
for index in table.indexes:
kb.connection.execute(text(f"DROP INDEX {index.name}"))
# # 2. create the new table
kb.schema.metadata.clear()
_ = kb.schema.get(Tables.KB, disambiguation=True)
kb.schema.metadata.create_all(kb.engine)
# 3. copy the data back
kb.connection.execute(
text(f"INSERT INTO {table.name} SELECT * FROM {table.name}_backup")
)
# from sqlalchemy.exc import IntegrityError
#
# kb.schema.metadata.clear()
# updated_table = self.kb.schema.get(Tables.KB, disambiguation=True)
# print(updated_table)
# kb.schema.metadata.create_all(kb.engine)
# for row in kb.query(text(f"select * from {table.name}_backup")):
# try:
# kb.connection.execute(updated_table.insert(), row)
# kb.connection.commit()
# except IntegrityError as error:
# raise ValueError(f"Duplicate disambiguation row: {row}") from error
# # 4. drop old table
kb.connection.execute(text(f"DROP TABLE {table.name}_backup"))
def update_constraint(self, kb: BelbKb):
"""
Update constraint on KB table to check disambiguation consistency
"""
logger.info(
"Add constraint for disambiguation: UNIQUE(name,disambiguation)")
if kb.db_config.dialect == SqlDialects.SQLITE:
self._sqlite_update_contraint(kb=kb)
else:
table = self.kb.schema.get(Tables.KB)
columns = [table.c.name, table.c.disambiguation]
unique_constraint = UniqueConstraint(*columns, name="unique_name")
kb.connection.execute(AddConstraint(unique_constraint))
kb.connection.commit()
def disambiguate(self, find_batch_size: int = 1000, insert_batch_size: int = 10000):
"""
Add disambiguation tables to create VALID (name,identifier) pairs, i.e. UNIQUE(name, disambiguation):
1. Find and disambiguate homonyms by foreign identifier: same name, different foreign identifier
2. Find and disambiguate homonyms: same name[+foreign identifier], different identifier
"""
with self.kb as handle:
if self.kb_config.foreign_identifier:
self.disambiguate_foreign_name_homonyms(
kb=handle, chunksize=insert_batch_size
)
self.disambiguate_name_homonyms(
kb=handle,
find_batch_size=find_batch_size,
insert_batch_size=insert_batch_size,
)
self.update_constraint(kb=handle)
save_json(
path=self.kb.sentinel_file, item={
"status": "up", "disambiguation": True}
)