Exemplo n.º 1
0
def most_similar(base_vector, words):
    start = time.time()
    # finds n words with smallest cosine similarity for a given word
    words_with_distance = [
        (v.cosine_similarity_normalized(base_vector, w.vector), w)
        for w in words
    ]
    # want as large as 1
    sorted_by_dis = sorted(words_with_distance)
    print(f"Elapsed {time.time() - start} s")
    return sorted_by_dis
Exemplo n.º 2
0
def most_similar(base_vector: Vector,
                 words: List[Word]) -> List[Tuple[float, Word]]:
    """Finds n words with smallest cosine similarity to a given word"""
    words_with_distance = [
        (v.cosine_similarity_normalized(base_vector, w.vector), w)
        for w in words
    ]
    # We want cosine similarity to be as large as possible (close to 1)
    sorted_by_distance = sorted(words_with_distance,
                                key=lambda t: t[0],
                                reverse=True)
    return sorted_by_distance