从Python:tf-idf-cosine:查找文档相似度,可以使用tf-idf余弦计算文档相似度。如果不导入外部库,是否有任何方法可以计算2个字符串之间的余弦相似度? s1 = "This is a foo bar sentence ." s2 = "This sentence is similar to a foo bar sentence ." s3 = "What is this string ? Totally not related to the other two lines ." cosine_sim(s1, s2) # Should give high cosine similarity cosine_sim(s1, s3) # Shouldn't …