字符串的Sklearn余弦相似度,Python

时间:2017-12-09 11:27:31

标签: python scikit-learn string-matching

我正在编写一种算法,用于检查字符串与另一个字符串的相等程度。我正在使用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”。

我的代码如何考虑在某些情况下不同的单词并不完全不同?

1 个答案:

答案 0 :(得分:2)

对于短字符串,Levenshtein distance可能会产生比基于单词的余弦相似度更好的结果。以下算法改编自Wikibooks。由于这是距离度量,因此分数越小越好。

def levenshtein(s1, s2):
    if len(s1) < len(s2):
        s1, s2 = s2, s1

    if len(s2) == 0:
        return len(s1)

    previous_row = range(len(s2) + 1)
    for i, c1 in enumerate(s1):
        current_row = [i + 1]
        for j, c2 in enumerate(s2):
            insertions = previous_row[j + 1] + 1
            deletions = current_row[j] + 1
            substitutions = previous_row[j] + (c1 != c2)
            current_row.append(min(insertions, deletions, substitutions))
        previous_row = current_row

    return previous_row[-1]/float(len(s1))

example_1 = ("I am okey", "I am okeu")
example_2 = ("I am okey", "I am crazy")

print(levenshtein(*example_1))
print(levenshtein(*example_2))