Add load_vectors_into_model util

This commit is contained in:
Matthew Honnibal 2020-07-28 21:56:02 +02:00
parent 475d7c1c7c
commit df95e2af64
1 changed files with 17 additions and 0 deletions

View File

@ -189,6 +189,23 @@ def get_module_path(module: ModuleType) -> Path:
return Path(sys.modules[module.__module__].__file__).parent
def load_vectors_into_model(
nlp: "Language",
name: Union[str, Path],
*,
add_strings=True
) -> None:
"""Load word vectors from an installed model or path into a model instance."""
vectors_nlp = load_model(name)
nlp.vocab.vectors = vectors_nlp.vocab.vectors
if add_strings:
# I guess we should add the strings from the vectors_nlp model?
# E.g. if someone does a similarity query, they might expect the strings.
for key in nlp.vocab.vectors.key2row:
if key in vectors_nlp.strings:
nlp.vocab.strings.add(vectors_nlp.strings[key])
def load_model(
name: Union[str, Path],
disable: Iterable[str] = tuple(),