68

文字列を文字列の配列と比較する方法を探しています。もちろん、正確な検索を行うのは非常に簡単ですが、スペルミスや文字列の一部の欠落などをプログラムに許容させたいと思っています。

そのような検索を実行できるフレームワークはありますか? 私は、検索アルゴリズムが一致のパーセンテージなどでいくつかの結果の順序を返すことを念頭に置いています。

4

5 に答える 5

75

レーベンシュタイン距離アルゴリズムを使用できます。

「2 つの文字列間のレーベンシュタイン距離は、1 つの文字列を別の文字列に変換するために必要な編集の最小数として定義されます。許容される編集操作は、1 文字の挿入、削除、または置換です。」-ウィキペディア.com

これはdotnetperls.comからのものです:

using System;

/// <summary>
/// Contains approximate string matching
/// </summary>
static class LevenshteinDistance
{
    /// <summary>
    /// Compute the distance between two strings.
    /// </summary>
    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];
    }
}

class Program
{
    static void Main()
    {
        Console.WriteLine(LevenshteinDistance.Compute("aunt", "ant"));
        Console.WriteLine(LevenshteinDistance.Compute("Sam", "Samantha"));
        Console.WriteLine(LevenshteinDistance.Compute("flomax", "volmax"));
    }
}

実際、 Damerau-Levenshtein 距離アルゴリズムを使用することをお勧めします。このアルゴリズムでは、データ入力における一般的な人的エラーである文字の転置も可能です。C# 実装はこちらにあります。

于 2010-02-26T19:44:05.757 に答える
23

.NET フレームワークには、このすぐに使用できるもので役立つものは何もありません。

最も一般的なスペルミスは、文字が単語の適切な音声表現であるが、単語の正しいスペルではない場合です.

たとえば、単語swordsord(はい、それは単語です) は同じ音声ルーツを持っていると主張することができます (発音すると同じように聞こえます)。

そうは言っても、単語 (つづりが間違っているものも含む) を発音記号に変換するために使用できるアルゴリズムは多数あります。

1つ目はSoundexです。実装は非常に簡単で、このアルゴリズムの .NET 実装がかなりの数あります。かなり単純ですが、相互に比較できる実際の値が得られます。

もう一つはMetaphoneです。Metaphone のネイティブ .NET 実装は見つかりませんが、提供されているリンクには、変換可能な他の多くの実装へのリンクがあります。最も簡単に変換できるのは、Metaphone アルゴリズムの Java 実装でしょう。

Metaphone アルゴリズムは改訂されていることに注意してください。Double Metaphone ( .NET 実装) とMetaphone 3があります。Metaphone 3 は商用アプリケーションですが、一般的な英単語のデータベースに対して実行した場合、Double Metaphone アルゴリズムの正確度が 89% であるのに対し、正確度は 98% です。必要に応じて、アルゴリズムのソースを検索 (Double Metaphone の場合) または購入 (Metaphone 3 の場合) し、P/Invoke レイヤーを介して変換またはアクセスすることができます (C++ 実装があります)。多い)。

Metaphone と Soundex は、Soundex が固定長の数字キーを生成するのに対し、Metaphone は異なる長さのキーを生成するという意味で異なるため、結果は異なります。最終的には、どちらも同じ種類の比較を行います。要件とリソース (そしてもちろんスペルミスに対する不寛容レベル) を考慮して、どちらがニーズに最も適しているかを見つける必要があります。

于 2010-02-26T19:58:43.227 に答える
13

これは、同じ結果を生成しながらはるかに少ないメモリを使用する LevenshteinDistance メソッドの実装です。これは、このウィキペディアの記事の「2 つの行列行による反復」という見出しの下にある疑似コードを C# に適応させたものです。

public static int LevenshteinDistance(string source, string target)
{
    // degenerate cases
    if (source == target) return 0;
    if (source.Length == 0) return target.Length;
    if (target.Length == 0) return source.Length;

    // create two work vectors of integer distances
    int[] v0 = new int[target.Length + 1];
    int[] v1 = new int[target.Length + 1];

    // initialize v0 (the previous row of distances)
    // this row is A[0][i]: edit distance for an empty s
    // the distance is just the number of characters to delete from t
    for (int i = 0; i < v0.Length; i++)
        v0[i] = i;

    for (int i = 0; i < source.Length; i++)
    {
        // calculate v1 (current row distances) from the previous row v0

        // first element of v1 is A[i+1][0]
        //   edit distance is delete (i+1) chars from s to match empty t
        v1[0] = i + 1;

        // use formula to fill in the rest of the row
        for (int j = 0; j < target.Length; j++)
        {
            var cost = (source[i] == target[j]) ? 0 : 1;
            v1[j + 1] = Math.Min(v1[j] + 1, Math.Min(v0[j + 1] + 1, v0[j] + cost));
        }

        // copy v1 (current row) to v0 (previous row) for next iteration
        for (int j = 0; j < v0.Length; j++)
            v0[j] = v1[j];
    }

    return v1[target.Length];
}

これは、類似度のパーセンテージを提供する関数です。

/// <summary>
/// Calculate percentage similarity of two strings
/// <param name="source">Source String to Compare with</param>
/// <param name="target">Targeted String to Compare</param>
/// <returns>Return Similarity between two strings from 0 to 1.0</returns>
/// </summary>
public static double CalculateSimilarity(string source, string target)
{
    if ((source == null) || (target == null)) return 0.0;
    if ((source.Length == 0) || (target.Length == 0)) return 0.0;
    if (source == target) return 1.0;

    int stepsToSame = LevenshteinDistance(source, target);
    return (1.0 - ((double)stepsToSame / (double)Math.Max(source.Length, target.Length)));
}
于 2016-11-23T22:14:03.397 に答える
7

もう 1 つのオプションは、Soundex または Metaphone を使用して音声的に比較することです。両方のアルゴリズムの C# コードを紹介する記事を完成させました。http://www.blackbeltcoder.com/Articles/algorithms/phonetic-string-comparison-with-soundexで表示できます。

于 2011-01-14T05:54:44.563 に答える
4

文字列間のレーベンシュタイン距離を計算する 2 つの方法を次に示します。

2 つの文字列間のレーベンシュタイン距離は、1 つの文字列を別の文字列に変換するために必要な編集の最小数として定義されます。許容される編集操作は、1 文字の挿入、削除、または置換です。

結果が得られたら、一致するかどうかのしきい値として使用する値を定義する必要があります。一連のサンプル データに対して関数を実行して、特定のしきい値を決定するためにどのように機能するかをよく理解してください。

    /// <summary>
    /// Calculates the Levenshtein distance between two strings--the number of changes that need to be made for the first string to become the second.
    /// </summary>
    /// <param name="first">The first string, used as a source.</param>
    /// <param name="second">The second string, used as a target.</param>
    /// <returns>The number of changes that need to be made to convert the first string to the second.</returns>
    /// <remarks>
    /// From http://www.merriampark.com/ldcsharp.htm
    /// </remarks>
    public static int LevenshteinDistance(string first, string second)
    {
        if (first == null)
        {
            throw new ArgumentNullException("first");
        }
        if (second == null)
        {
            throw new ArgumentNullException("second");
        }

        int n = first.Length;
        int m = second.Length;
        var d = new int[n + 1, m + 1]; // matrix

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

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

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

        for (int i = 1; i <= n; i++)
        {

            for (int j = 1; j <= m; j++)
            {
                int cost = (second.Substring(j - 1, 1) == first.Substring(i - 1, 1) ? 0 : 1); // cost
                d[i, j] = Math.Min(
                    Math.Min(
                        d[i - 1, j] + 1,
                        d[i, j - 1] + 1),
                    d[i - 1, j - 1] + cost);
            }
        }

        return d[n, m];
    }
于 2010-02-26T19:43:08.467 に答える