【发布时间】:2017-12-09 20:46:53
【问题描述】:
我正在编写一个算法来检查一个字符串与另一个字符串的相等程度。我正在使用 Sklearn 余弦相似度。
我的代码是:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
example_1 = ("I am okey", "I am okeu")
example_2 = ("I am okey", "I am crazy")
tfidf_vectorizer = TfidfVectorizer()
tfidf_matrix = tfidf_vectorizer.fit_transform(example_1)
result_cos = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix)
print(result_cos[0][1])
为 example_1 运行此代码,打印 0.336096927276。为 example_2 运行它,它会打印相同的分数。两种情况下的结果都是一样的,因为只有一个不同的词。
我想要为 example_1 获得更高的分数,因为不同的单词“okey vs okeu”只有一个不同的字母。相比之下,在 example_2 中有两个完全不同的词“okey vs crazy”。
我的代码如何考虑到在某些情况下不同的词并不完全不同?
【问题讨论】:
标签: python scikit-learn string-matching