From df95e2af64a6c3d2862e4317a450e8e694e2d406 Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Tue, 28 Jul 2020 21:56:02 +0200 Subject: [PATCH] Add load_vectors_into_model util --- spacy/util.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spacy/util.py b/spacy/util.py index 72e68463b..4e3a8d203 100644 --- a/spacy/util.py +++ b/spacy/util.py @@ -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(),