-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
x_ray.py
304 lines (289 loc) · 10.5 KB
/
x_ray.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
import re
from collections import defaultdict
from functools import partial
from pathlib import Path
from sqlite3 import Connection
try:
from .database import (
create_x_indices,
insert_x_book_metadata,
insert_x_entities,
insert_x_entity_description,
insert_x_excerpt_image,
insert_x_occurrences,
insert_x_types,
save_db,
)
from .mediawiki import (
MediaWiki,
Wikidata,
inception_text,
query_wikidata,
)
from .metadata import KFXJson
from .utils import Prefs
from .x_ray_share import (
FUZZ_THRESHOLD,
PERSON_LABELS,
CustomXDict,
XRayEntity,
is_full_name,
)
except ImportError:
from database import (
create_x_indices,
insert_x_book_metadata,
insert_x_entities,
insert_x_entity_description,
insert_x_excerpt_image,
insert_x_occurrences,
insert_x_types,
save_db,
)
from mediawiki import (
MediaWiki,
Wikidata,
inception_text,
query_wikidata,
)
from metadata import KFXJson
from utils import Prefs
from x_ray_share import (
FUZZ_THRESHOLD,
PERSON_LABELS,
CustomXDict,
XRayEntity,
is_full_name,
)
class X_Ray:
def __init__(
self,
conn: Connection,
mediawiki: MediaWiki,
wikidata: Wikidata | None,
custom_x_ray: CustomXDict,
) -> None:
self.conn = conn
self.entity_id = 1
self.entities: dict[str, XRayEntity] = {}
self.num_images = 0
self.mediawiki = mediawiki
self.wikidata = wikidata
self.entity_occurrences: dict[int, list[tuple[int, int]]] = defaultdict(list)
self.custom_x_ray = custom_x_ray
def insert_descriptions(self, search_people: bool) -> None:
for entity_name, entity_data in self.entities.items():
if custom_data := self.custom_x_ray.get(entity_name):
if custom_data.desc is not None and len(custom_data.desc) > 0:
insert_x_entity_description(
self.conn,
(
custom_data.desc,
entity_name,
custom_data.source_id,
entity_data.id,
),
)
continue
if (search_people or entity_data.label not in PERSON_LABELS) and (
intro_cache := self.mediawiki.get_cache(entity_name)
):
summary = intro_cache.intro
if self.wikidata is not None and (
wikidata_cache := self.wikidata.get_cache(
intro_cache.wikidata_item_id
)
):
if inception := wikidata_cache.get("inception"):
summary += "\n" + inception_text(inception)
insert_x_entity_description(
self.conn,
(
summary,
entity_name,
1 if self.mediawiki.is_wikipedia else 2,
entity_data.id,
),
)
else:
insert_x_entity_description(
self.conn, (entity_data.quote, entity_name, None, entity_data.id)
)
def add_entity(
self, entity: str, ner_label: str, start: int, quote: str, entity_len: int
) -> None:
from rapidfuzz.fuzz import token_set_ratio
from rapidfuzz.process import extractOne
from rapidfuzz.utils import default_process
if entity_data := self.entities.get(entity):
entity_id = entity_data.id
entity_data.count += 1
elif entity not in self.custom_x_ray and (
r := extractOne(
entity,
self.entities.keys(),
score_cutoff=FUZZ_THRESHOLD,
scorer=partial(token_set_ratio, processor=default_process),
)
):
matched_name = r[0]
matched_entity = self.entities[matched_name]
matched_entity.count += 1
entity_id = matched_entity.id
if is_full_name(matched_name, matched_entity.label, entity, ner_label):
# replace partial name with full name
self.entities[entity] = self.entities[matched_name]
del self.entities[matched_name]
else:
entity_id = self.entity_id
self.entities[entity] = XRayEntity(entity_id, quote, ner_label, 1)
self.entity_id += 1
self.entity_occurrences[entity_id].append((start, entity_len))
def merge_entities(self, prefs: Prefs) -> None:
for entity_name, entity_data in self.entities.copy().items():
if entity_name in self.custom_x_ray:
continue
redirect_to = self.mediawiki.redirect_to_page(entity_name)
if redirect_to in self.entities:
self.entity_occurrences[self.entities[redirect_to].id].extend(
self.entity_occurrences[entity_data.id]
)
self.entities[redirect_to].count += entity_data.count
del self.entity_occurrences[entity_data.id]
del self.entities[entity_name]
continue
has_cache = self.mediawiki.get_cache(entity_name) is not None
is_person = entity_data.label in PERSON_LABELS
if entity_data.count < prefs["minimal_x_ray_count"] and (
(prefs["search_people"] and not has_cache)
or (
not prefs["search_people"]
and (is_person or (not is_person and not has_cache))
)
):
del self.entity_occurrences[entity_data.id]
del self.entities[entity_name]
def finish(
self,
db_path: Path,
erl: int,
kfx_json: list[KFXJson],
mobi_html: bytes,
mobi_codec: str,
prefs: Prefs,
) -> None:
self.mediawiki.query(self.entities, prefs["search_people"])
if self.wikidata is not None:
query_wikidata(self.entities, self.mediawiki, self.wikidata)
self.merge_entities(prefs)
insert_x_entities(
self.conn,
(
(
entity_data.id,
entity_name,
1 if entity_data.label in PERSON_LABELS else 2,
entity_data.count,
)
for entity_name, entity_data in self.entities.items()
),
)
insert_x_occurrences(
self.conn,
(
(entity_id, start, entity_length)
for entity_id, occurrence_list in self.entity_occurrences.items()
for start, entity_length in occurrence_list
),
)
self.insert_descriptions(prefs["search_people"])
if kfx_json:
self.find_kfx_images(kfx_json)
else:
self.find_mobi_images(mobi_html, mobi_codec)
if self.num_images:
preview_images = ",".join(map(str, range(self.num_images)))
else:
preview_images = None
insert_x_book_metadata(
self.conn,
erl,
self.num_images,
preview_images,
)
insert_x_types(self.conn)
create_x_indices(self.conn)
save_db(self.conn, db_path)
self.mediawiki.close()
if self.wikidata is not None:
self.wikidata.close()
def find_kfx_images(self, kfx_json: list[KFXJson]) -> None:
images = set()
for index, image in filter(lambda x: x[1]["type"] == 2, enumerate(kfx_json)):
if image["content"] in images:
continue
images.add(image["content"])
caption_start = image["position"]
caption_length = 0
if (
index + 1 < len(kfx_json)
and kfx_json[index + 1]["type"] == 1
and len(kfx_json[index + 1]["content"]) < 450
):
caption = kfx_json[index + 1]
caption_start = caption["position"]
caption_length = len(caption["content"])
insert_x_excerpt_image(
self.conn,
(
self.num_images,
caption_start,
caption_length,
image["content"],
image["position"],
),
)
self.num_images += 1
def find_mobi_images(self, mobi_html: bytes, mobi_codec: str) -> None:
images = set()
for match_img in re.finditer(b"<img [^>]+/>", mobi_html):
if match_src := re.search(
r'src="([^"]+)"', match_img.group(0).decode(mobi_codec)
):
image_src = match_src.group(1)
if image_src in images:
continue
images.add(image_src)
caption_start = match_img.start()
caption_length = 0
previous_match_end = match_img.end()
for _ in range(2):
match_caption = re.search(
b">[^<]{2,}<", mobi_html[previous_match_end:]
)
if not match_caption:
break
if not match_caption.group(0)[1:-1].strip():
previous_match_end += match_caption.end()
continue
if not re.match(
b"<html|<img",
mobi_html[
previous_match_end : previous_match_end
+ match_caption.start()
],
):
caption_start = previous_match_end + match_caption.start() + 1
caption_length = match_caption.end() - match_caption.start() - 2
break
insert_x_excerpt_image(
self.conn,
(
self.num_images,
caption_start,
caption_length,
image_src,
match_img.start(),
),
)
self.num_images += 1