From bd20ec0a6adc8a8c267ee316815d727981c04fcb Mon Sep 17 00:00:00 2001 From: Ines Montani Date: Thu, 12 Jan 2017 16:49:57 +0100 Subject: [PATCH] Add get_cosine util function --- spacy/tests/util.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spacy/tests/util.py b/spacy/tests/util.py index 60b7c29ee..a1d6b4c17 100644 --- a/spacy/tests/util.py +++ b/spacy/tests/util.py @@ -4,6 +4,8 @@ from __future__ import unicode_literals from ..tokens import Doc from ..attrs import ORTH, POS, HEAD, DEP +import numpy + def get_doc(vocab, words=[], pos=None, heads=None, deps=None, tags=None, ents=None): """Create Doc object from given vocab, words and annotations.""" @@ -36,3 +38,8 @@ def apply_transition_sequence(parser, doc, sequence): with parser.step_through(doc) as stepwise: for transition in sequence: stepwise.transition(transition) + + +def get_cosine(vec1, vec2): + """Get cosine for two given vectors""" + return numpy.dot(vec1, vec2) / (numpy.linalg.norm(vec1) * numpy.linalg.norm(vec2))