-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
dump_lemmas.py
191 lines (176 loc) · 5.28 KB
/
dump_lemmas.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
import sqlite3
from operator import itemgetter
from pathlib import Path
try:
from .utils import (
Prefs,
custom_lemmas_folder,
insert_installed_libs,
load_plugin_json,
use_kindle_ww_db,
)
except ImportError:
from utils import (
Prefs,
custom_lemmas_folder,
insert_installed_libs,
load_plugin_json,
use_kindle_ww_db,
)
def spacy_doc_path(
spacy_model: str,
model_version: str,
lemma_lang: str,
is_kindle: bool,
is_phrase: bool,
plugin_path: Path,
prefs: Prefs,
use_lemma_matcher: bool,
):
import platform
gloss_lang = prefs["kindle_gloss_lang" if is_kindle else "wiktionary_gloss_lang"]
if is_kindle and not use_kindle_ww_db(lemma_lang, prefs):
is_kindle = False
py_version = ".".join(platform.python_version_tuple()[:2])
path = custom_lemmas_folder(plugin_path, lemma_lang).joinpath(
f"{spacy_model or lemma_lang}_{'kindle' if is_kindle else 'wiktionary'}"
f"_{gloss_lang}_{model_version}_{py_version}"
)
if use_lemma_matcher:
if is_phrase:
path = path.with_name(path.name + "_phrase")
path = path.with_name(path.name + "_pos")
return path
def dump_spacy_docs(
spacy_model: str,
is_kindle: bool,
lemma_lang: str,
db_path: Path,
plugin_path: Path,
prefs: Prefs,
):
insert_installed_libs(plugin_path)
import spacy
use_lemma_matcher = prefs["use_pos"] and lemma_lang != "zh" and spacy_model != ""
excluded_components = ["ner", "parser"]
if not use_lemma_matcher:
excluded_components.extend(
["tok2vec", "morphologizer", "tagger", "attribute_ruler", "lemmatizer"]
)
nlp = (
spacy.load(spacy_model, exclude=excluded_components)
if spacy_model != ""
else spacy.blank(lemma_lang)
)
lemmas_conn = sqlite3.connect(db_path)
pkg_versions = load_plugin_json(plugin_path, "data/deps.json")
save_spacy_docs(
nlp,
spacy_model,
pkg_versions[
"spacy_trf_model" if spacy_model.endswith("_trf") else "spacy_cpu_model"
],
lemma_lang,
is_kindle,
lemmas_conn,
plugin_path,
prefs,
use_lemma_matcher,
)
lemmas_conn.close()
def save_spacy_docs(
nlp,
spacy_model: str,
model_version: str,
lemma_lang: str,
is_kindle: bool,
lemmas_conn: sqlite3.Connection,
plugin_path: Path,
prefs: Prefs,
use_lemma_matcher: bool,
):
from spacy.tokens import DocBin
phrases_doc_bin = DocBin(attrs=["LOWER"])
if use_lemma_matcher:
lemmas_doc_bin = DocBin(attrs=["LEMMA"])
difficulty_limit = (
5 if is_kindle else prefs[f"{lemma_lang}_wiktionary_difficulty_limit"]
)
if use_lemma_matcher:
for doc in create_lemma_patterns_with_pos(
lemma_lang, lemmas_conn, nlp, difficulty_limit
):
if " " in doc.text:
phrases_doc_bin.add(doc)
else:
lemmas_doc_bin.add(doc)
else:
for doc in create_lemma_patterns_without_pos(
lemmas_conn, nlp, difficulty_limit
):
phrases_doc_bin.add(doc)
phrases_doc_bin.to_disk(
spacy_doc_path(
spacy_model,
model_version,
lemma_lang,
is_kindle,
True,
plugin_path,
prefs,
use_lemma_matcher,
)
)
if use_lemma_matcher:
lemmas_doc_bin.to_disk(
spacy_doc_path(
spacy_model,
model_version,
lemma_lang,
is_kindle,
False,
plugin_path,
prefs,
use_lemma_matcher,
)
)
def create_lemma_patterns_with_pos(lemma_lang, conn, nlp, difficulty_limit):
if lemma_lang == "zh":
query_sql = """
SELECT DISTINCT lemma
FROM lemmas l
JOIN senses s ON l.id = s.lemma_id AND enabled = 1 AND difficulty <= :difficulty
UNION ALL
SELECT DISTINCT form FROM forms f
JOIN senses s ON f.lemma_id = s.lemma_id AND f.pos = s.pos
AND enabled = 1 AND difficulty <= :difficulty
"""
else:
query_sql = """
SELECT DISTINCT lemma
FROM lemmas l
JOIN senses s ON l.id = s.lemma_id AND enabled = 1 AND difficulty <= :difficulty
UNION ALL
SELECT DISTINCT form
FROM lemmas l
JOIN forms f ON l.id = f.lemma_id
JOIN senses s ON l.id = s.lemma_id AND f.pos = s.pos
AND enabled = 1 AND difficulty <= :difficulty
WHERE lemma LIKE '% %'
"""
yield from nlp.pipe(
map(itemgetter(0), conn.execute(query_sql, {"difficulty": difficulty_limit}))
)
def create_lemma_patterns_without_pos(conn, nlp, difficulty_limit):
query_sql = """
SELECT DISTINCT lemma
FROM lemmas l JOIN senses s ON l.id = s.lemma_id
AND enabled = 1 AND difficulty <= :difficulty
UNION ALL
SELECT DISTINCT form
FROM forms f JOIN senses s ON f.lemma_id = s.lemma_id
AND f.pos = s.pos AND enabled = 1 AND difficulty <= :difficulty
"""
yield from nlp.tokenizer.pipe(
map(itemgetter(0), conn.execute(query_sql, {"difficulty": difficulty_limit}))
)