forked from xxyzz/WordDumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_job.py
185 lines (154 loc) · 6.64 KB
/
parse_job.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
#!/usr/bin/env python3
import json
import re
import subprocess
import sys
from pathlib import Path
try:
from .database import (create_lang_layer, create_x_ray_db, get_ll_path,
get_x_ray_path, insert_lemma, save_db)
from .unzip import load_json_or_pickle
from .x_ray import X_Ray
except ImportError:
from database import (create_lang_layer, create_x_ray_db, get_ll_path,
get_x_ray_path, insert_lemma, save_db)
from unzip import load_json_or_pickle
from x_ray import X_Ray
def do_job(data, create_ww=True, create_x=True,
abort=None, log=None, notifications=None):
from calibre.constants import ismacos
from calibre.utils.config import config_dir
from calibre_plugins.worddumb import VERSION
from .config import prefs
from .deps import InstallDeps
from .metadata import get_asin_etc
(book_id, book_fmt, book_path, mi, lang) = data
is_kfx = book_fmt == 'KFX'
(asin, acr, revision, update_asin,
kfx_json, mobi_html, mobi_codec) = get_asin_etc(book_path, is_kfx, mi)
model = lang['spacy'] + prefs['model_size']
plugin_path = str(Path(config_dir).joinpath('plugins/WordDumb.zip'))
create_ww = create_ww and not get_ll_path(asin, book_path).exists()
create_x = create_x and not get_x_ray_path(asin, book_path).exists()
if create_x:
install_deps = InstallDeps(model, plugin_path, notifications)
if notifications:
notifications.put((0, 'Creating files'))
version = '.'.join(map(str, VERSION))
if ismacos and create_x:
args = [install_deps.py, plugin_path, asin, book_path, acr, revision,
model, lang['wiki'], mobi_codec, plugin_path, version,
prefs['zh_wiki_variant']]
if create_ww:
args.append('-l')
if prefs['search_people']:
args.append('-s')
if is_kfx:
input_str = json.dumps(kfx_json)
else:
input_str = mobi_html.decode(mobi_codec)
subprocess.run(
args, input=input_str, check=True, capture_output=True, text=True)
else:
create_files(
create_ww, create_x, asin, book_path, acr, revision, model,
lang['wiki'], kfx_json, mobi_html, mobi_codec, plugin_path,
version, prefs['zh_wiki_variant'], prefs['search_people'],
notifications)
return book_id, asin, book_path, mi, update_asin
def insert_lib_path(path):
if path not in sys.path:
sys.path.insert(0, path)
def create_files(create_ww, create_x, asin, book_path, acr, revision, model,
wiki_lang, kfx_json, mobi_html, mobi_codec, plugin_path,
plugin_version, zh_wiki, search_people, notif):
if notif:
if kfx_json:
last_start = max(d['position'] for d in kfx_json if d['type'] == 1)
else:
last_start = len(mobi_html)
if create_ww:
ll_conn, ll_path = create_lang_layer(asin, book_path, acr, revision)
insert_lib_path(str(Path(plugin_path).joinpath('libs'))) # flashtext
kw_processor = load_json_or_pickle(plugin_path, 'lemmas_dump')
if create_x:
for path in Path(plugin_path).parent.glob('worddumb-libs-py*'):
insert_lib_path(str(path))
import spacy
x_ray_conn, x_ray_path = create_x_ray_db(
asin, book_path, wiki_lang, plugin_path)
nlp = spacy.load(model, exclude=[
'tok2vec', 'morphologizer', 'tagger',
'parser', 'attribute_ruler', 'lemmatizer'])
nlp.enable_pipe("senter")
x_ray = X_Ray(
x_ray_conn, wiki_lang, kfx_json, mobi_html, mobi_codec,
plugin_path, plugin_version, zh_wiki, search_people)
for doc, start in nlp.pipe(
parse_book(kfx_json, mobi_html, mobi_codec), as_tuples=True):
find_named_entity(start, x_ray, doc, mobi_codec)
if create_ww:
find_lemma(
start, doc.text, kw_processor, ll_conn, mobi_codec)
if notif:
notif.put((start / last_start, 'Creating files'))
x_ray.finish(x_ray_path)
elif create_ww:
for text, start in parse_book(kfx_json, mobi_html, mobi_codec):
find_lemma(start, text, kw_processor, ll_conn, mobi_codec)
if create_ww:
save_db(ll_conn, ll_path)
def parse_book(kfx_json, mobi_html, mobi_codec):
if kfx_json:
return ((e['content'],
e['position']) for e in kfx_json if e['type'] == 1)
else:
# match text inside HTML tags
return ((m.group(0)[1:-1].decode(mobi_codec),
m.start() + 1) for m in re.finditer(b'>[^<>]+<', mobi_html))
def find_lemma(start, text, kw_processor, ll_conn, mobi_codec):
for data, token_start, token_end in kw_processor.extract_keywords(
text, span_info=True):
end = None
lemma = text[token_start:token_end]
if mobi_codec:
index = start + len(text[:token_start].encode(mobi_codec))
else:
index = start + token_start
if ' ' in lemma:
if mobi_codec:
end = index + len(lemma.encode(mobi_codec))
else:
end = index + len(lemma)
insert_lemma(ll_conn, (index, end) + tuple(data))
# https://github.com/explosion/spaCy/blob/master/spacy/glossary.py#L318
NER_LABELS = {
'EVENT', 'FAC', 'GPE', 'LANGUAGE', 'LAW', 'LOC', 'NORP', 'ORG',
'PERSON', 'PRODUCT', 'WORK_OF_ART', 'MISC', 'PER', 'FACILITY',
'ORGANIZATION', 'NAT_REL_POL', # Romanian
'geogName', 'orgName', 'persName', 'placeName' # Polish
}
def find_named_entity(start, x_ray, doc, mobi_codec):
len_limit = 3 if x_ray.lang == 'en' else 2
for ent in doc.ents:
if ent.label_ not in NER_LABELS:
continue
text = re.sub(r'^\W+', '', ent.text)
text = re.sub(r'\W+$', '', text)
if x_ray.lang == 'en':
if re.match(r'c?hapter', text, re.IGNORECASE):
continue
text = re.sub(r"['’][sd]$", '', text)
text = re.sub(r'^(?:the|an?) ', '', text, flags=re.IGNORECASE)
if len(text) < len_limit or re.fullmatch(r'[\W\d]+', text):
continue
new_start_char = ent.start_char + ent.text.index(text)
if mobi_codec:
ent_start = start + len(
doc.text[:new_start_char].encode(mobi_codec))
ent_len = len(text.encode(mobi_codec))
else:
ent_start = start + len(doc.text[:new_start_char])
ent_len = len(text)
x_ray.search(text, ent.label_ in ['PERSON', 'PER', 'persName'],
ent_start, ent.sent.text, ent_len)