From dc2bb1259f3c31e1744f715e09e3449b61f03c5d Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Fri, 13 Jan 2017 14:26:30 +0100 Subject: [PATCH] Add util function to add vectors to vocab --- spacy/tests/util.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/spacy/tests/util.py b/spacy/tests/util.py index 41673ab1d..355a4ecae 100644 --- a/spacy/tests/util.py +++ b/spacy/tests/util.py @@ -40,6 +40,16 @@ def apply_transition_sequence(parser, doc, sequence): stepwise.transition(transition) +def add_vecs_to_vocab(vocab, vectors): + """Add list of vector tuples to given vocab. All vectors need to have the + same length. Format: [("text", [1, 2, 3])]""" + length = len(vectors[0][1]) + vocab.resize_vectors(length) + for word, vec in vectors: + vocab[word].vector = vec + return vocab + + def get_cosine(vec1, vec2): """Get cosine for two given vectors""" return numpy.dot(vec1, vec2) / (numpy.linalg.norm(vec1) * numpy.linalg.norm(vec2))