如果我们有两个字符串列表:
A = "Hello how are you? The weather is fine. I'd like to go for a walk.".split()
B = "bank, weather, sun, moon, fun, hi".split(",")
列表A
中的单词构成了我的单词向量基础。
如何计算B中每个单词的余弦相似度分数?
到目前为止我做了什么: 我可以使用以下函数计算两个整个列表的余弦相似度:
def counter_cosine_similarity(c1, c2):
terms = set(c1).union(c2)
dotprod = sum(c1.get(k, 0) * c2.get(k, 0) for k in terms)
magA = math.sqrt(sum(c1.get(k, 0)**2 for k in terms))
magB = math.sqrt(sum(c2.get(k, 0)**2 for k in terms))
return dotprod / (magA * magB)
但是我如何整合我的矢量基础,如何计算B中术语之间的相似性?