word_arithmetic.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:GloVe-experiments 作者: brannondorsey 项目源码 文件源码
def find_nearest(words, vec, id_to_word, df, num_results, method='cosine'):

    if method == 'cosine':
        minim = [] # min, index
        for i, v in enumerate(df):
            # skip the base word, its usually the closest
            if id_to_word[i] in words:
                continue
            dist = cosine(vec, v)
            minim.append((dist, i))
        minim = sorted(minim, key=lambda v: v[0])
        # return list of (word, cosine distance) tuples
        return [(id_to_word[minim[i][1]], minim[i][0]) for i in range(num_results)]
    else:
        raise Exception('{} is not an excepted method parameter'.format(method))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号