55

给定两个字符串text1text2

public SOMEUSABLERETURNTYPE Compare(string text1, string text2)
{
     // DO SOMETHING HERE TO COMPARE
}

例子:

  1. 第一个字符串:堆栈溢出

    第二个字符串:StaqOverflow

    返回:相似度为 91%

    回报可以是 % 或类似的东西。

  2. 第一个字符串:简单的文本测试

    第二个字符串:复杂的文本测试

    返回:可以认为值相等

有任何想法吗?做这个的最好方式是什么?

4

12 回答 12

42

有多种不同的方法可以做到这一点。查看Wikipedia“字符串相似性度量”页面以获取到其他具有算法的页面的链接。

然而,我认为这些算法中的任何一个都没有考虑到声音 - 所以“staq 溢出”与“堆栈溢出”一样类似于“staw 溢出”,尽管第一个在发音方面更相似。

我刚刚找到另一个页面,它提供了更多选项......特别是,Soundex算法(维基百科)可能更接近你所追求的。

于 2009-06-23T19:30:02.230 回答
27

Levenshtein 距离可能是您正在寻找的。

于 2009-06-23T19:29:05.507 回答
14

这是我为我正在从事的项目编写的一些代码。我需要知道字符串的相似度和基于字符串单词的相似度。最后一个,我想知道最小字符串的单词相似率(所以如果所有单词都存在并且在较大的字符串中匹配,结果将是 100%)和较大字符串的单词相似率(我称之为 RealWordsRatio )。我使用 Levenshtein 算法来找到距离。到目前为止,该代码尚未优化,但可以按预期工作。希望对你有帮助。

public static int Compute(string s, string t)
    {
        int n = s.Length;
        int m = t.Length;
        int[,] d = new int[n + 1, m + 1];

        // Step 1
        if (n == 0)
        {
            return m;
        }

        if (m == 0)
        {
            return n;
        }

        // Step 2
        for (int i = 0; i <= n; d[i, 0] = i++)
        {
        }

        for (int j = 0; j <= m; d[0, j] = j++)
        {
        }

        // Step 3
        for (int i = 1; i <= n; i++)
        {
            //Step 4
            for (int j = 1; j <= m; j++)
            {
                // Step 5
                int cost = (t[j - 1] == s[i - 1]) ? 0 : 1;

                // Step 6
                d[i, j] = Math.Min(
                    Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
                    d[i - 1, j - 1] + cost);
            }
        }
        // Step 7
        return d[n, m];
    }

double GetSimilarityRatio(String FullString1, String FullString2, out double WordsRatio, out double RealWordsRatio)
    {
        double theResult = 0;
        String[] Splitted1 = FullString1.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
        String[] Splitted2 = FullString2.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
        if (Splitted1.Length < Splitted2.Length)
        {
            String[] Temp = Splitted2;
            Splitted2 = Splitted1;
            Splitted1 = Temp;
        }
        int[,] theScores = new int[Splitted1.Length, Splitted2.Length];//Keep the best scores for each word.0 is the best, 1000 is the starting.
        int[] BestWord = new int[Splitted1.Length];//Index to the best word of Splitted2 for the Splitted1.

        for (int loop = 0; loop < Splitted1.Length; loop++) 
        {
            for (int loop1 = 0; loop1 < Splitted2.Length; loop1++) theScores[loop, loop1] = 1000;
            BestWord[loop] = -1;
        }
        int WordsMatched = 0;
        for (int loop = 0; loop < Splitted1.Length; loop++)
        {
            String String1 = Splitted1[loop];
            for (int loop1 = 0; loop1 < Splitted2.Length; loop1++)
            {
                String String2 = Splitted2[loop1];
                int LevenshteinDistance = Compute(String1, String2);
                theScores[loop, loop1] = LevenshteinDistance;
                if (BestWord[loop] == -1 || theScores[loop, BestWord[loop]] > LevenshteinDistance) BestWord[loop] = loop1;
            }
        }

        for (int loop = 0; loop < Splitted1.Length; loop++)
        {
            if (theScores[loop, BestWord[loop]] == 1000) continue;
            for (int loop1 = loop + 1; loop1 < Splitted1.Length; loop1++)
            {
                if (theScores[loop1, BestWord[loop1]] == 1000) continue;//the worst score available, so there are no more words left
                if (BestWord[loop] == BestWord[loop1])//2 words have the same best word
                {
                    //The first in order has the advantage of keeping the word in equality
                    if (theScores[loop, BestWord[loop]] <= theScores[loop1, BestWord[loop1]])
                    {
                        theScores[loop1, BestWord[loop1]] = 1000;
                        int CurrentBest = -1;
                        int CurrentScore = 1000;
                        for (int loop2 = 0; loop2 < Splitted2.Length; loop2++)
                        {
                            //Find next bestword
                            if (CurrentBest == -1 || CurrentScore > theScores[loop1, loop2])
                            {
                                CurrentBest = loop2;
                                CurrentScore = theScores[loop1, loop2];
                            }
                        }
                        BestWord[loop1] = CurrentBest;
                    }
                    else//the latter has a better score
                    {
                        theScores[loop, BestWord[loop]] = 1000;
                        int CurrentBest = -1;
                        int CurrentScore = 1000;
                        for (int loop2 = 0; loop2 < Splitted2.Length; loop2++)
                        {
                            //Find next bestword
                            if (CurrentBest == -1 || CurrentScore > theScores[loop, loop2])
                            {
                                CurrentBest = loop2;
                                CurrentScore = theScores[loop, loop2];
                            }
                        }
                        BestWord[loop] = CurrentBest;
                    }

                    loop = -1;
                    break;//recalculate all
                }
            }
        }
        for (int loop = 0; loop < Splitted1.Length; loop++)
        {
            if (theScores[loop, BestWord[loop]] == 1000) theResult += Splitted1[loop].Length;//All words without a score for best word are max failures
            else
            {
                theResult += theScores[loop, BestWord[loop]];
                if (theScores[loop, BestWord[loop]] == 0) WordsMatched++;
            }
        }
        int theLength = (FullString1.Replace(" ", "").Length > FullString2.Replace(" ", "").Length) ? FullString1.Replace(" ", "").Length : FullString2.Replace(" ", "").Length;
        if(theResult > theLength) theResult = theLength;
        theResult = (1 - (theResult / theLength)) * 100;
        WordsRatio = ((double)WordsMatched / (double)Splitted2.Length) * 100;
        RealWordsRatio = ((double)WordsMatched / (double)Splitted1.Length) * 100;
        return theResult;
    }
于 2012-06-22T18:37:05.473 回答
5

不久前,我用 C# 编写了一个Double Metaphone 实现。您会发现它大大优于 Soundex 等。

Levenshtein distance 也被提出,它是一个很好的算法,有很多用途,但语音匹配并不是它真正的作用;有时似乎只是这样,因为语音上相似的单词通常也拼写相似。我对各种模糊匹配算法进行了分析,您可能也会发现它们很有用。

于 2009-06-25T20:32:08.387 回答
4

要处理“声音相似”,您可能需要考虑使用语音算法(如 Double Metaphone 或 soundex)进行编码。我不知道在语音编码字符串上计算 Levenshtein 距离是否有益,但可能是一种可能性。或者,您可以使用启发式方法:将字符串中的每个单词转换为其编码形式,并删除两个字符串中出现的任何单词,并在计算 Levenshtein 距离之前用单个表示替换它们。

于 2009-06-23T19:38:45.160 回答
3

您可能会寻找字符串“距离”,例如Levenshtein distance

于 2009-06-23T19:29:07.410 回答
3

Perl 模块Text::Phonetic具有各种算法的实现。

于 2009-06-23T19:40:22.223 回答
2

如果要比较 SQL 数据库中的值,可以使用SOUNDEX函数。如果你在 Google 上查询 SOUNDEX 和 C#,有些人已经为它和 VB 编写了类似的函数。

于 2009-06-24T00:59:45.917 回答
2

Jeff Atwood 写了关于寻找类似的解决方案来确定 wiki 帖子的作者身份,这可以帮助您缩小搜索范围。

于 2009-06-23T19:30:04.773 回答
1

我也必须推荐 Soundex,我过去曾用它来处理拼写错误的城市名称。这是一个很好的使用链接:http ://whitepapers.zdnet.com/abstract.aspx?docid=352953

于 2009-06-24T17:03:01.987 回答
1

Metaphone 3 是 Metaphone 算法的第三代。它将语音编码的准确性从 Double Metaphone 的 89% 提高到98%,这是针对北美最常见的英语单词、名称和非英语单词的数据库进行测试的。这为美式发音产生了极其可靠的语音编码。

Metaphone 3 由设计和开发原始 Metaphone 和 Double Metaphone 算法的 Lawrence Philips 设计和开发。

于 2013-02-06T05:25:24.540 回答
1

如果您想在语音上进行比较,请查看 Soundex 和 Metaphone 算法: http: //www.blackbeltcoder.com/Articles/algorithms/phonetic-string-comparison-with-soundex

于 2011-01-14T06:03:34.833 回答