Skip to content

Instantly share code, notes, and snippets.

@mkonicek
Last active January 23, 2018 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkonicek/b6fabff699b4a413e75068b33eaf8bda to your computer and use it in GitHub Desktop.
Save mkonicek/b6fabff699b4a413e75068b33eaf8bda to your computer and use it in GitHub Desktop.
def sorted_by_similarity(words: List[Word], base_vector: Vector) -> List[Tuple[float, Word]]:
"""Returns words sorted by cosine distance to a given vector, most similar first"""
words_with_distance = [(cosine_similarity(base_vector, w.vector), w) for w in words]
# We want cosine similarity to be as large as possible (close to 1)
return sorted(words_with_distance, key=lambda t: t[0], reverse=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment