我正在寻找一种比较字符串和字符串数组的方法。进行精确的搜索当然很容易,但我希望我的程序能够容忍拼写错误、字符串的缺失部分等等。
有没有某种框架可以执行这样的搜索?我的想法是,搜索算法将返回一些按匹配百分比排序的结果,或者类似这样的结果。
发布于 2010-02-27 03:44:06
你可以使用Levenshtein Distance algorithm。
“两个字符串之间的Levenshtein距离被定义为将一个字符串转换为另一个字符串所需的最小编辑次数,允许的编辑操作是插入、删除或替换单个字符。”- Wikipedia.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 distance algorithm,它还允许字符转置,这是数据输入中常见的人为错误。您将找到它的here的C#实现。
发布于 2010-02-27 03:58:43
.NET框架中没有任何东西可以帮助您完成这个开箱即用的操作。
最常见的拼写错误是那些字母是单词的适当语音表示,而不是单词的正确拼写。
例如,可能会认为单词sword和sord (是的,这是一个单词)具有相同的语音词根(当您发音时,它们的发音相同)。
也就是说,您可以使用许多算法将单词(甚至拼写错误的单词)转换为语音变体。
第一个是Soundex。它的实现相当简单,并且有相当数量的.NET implementations of this algorithm。它相当简单,但它提供了可以相互比较的真实价值。
另一个是Metaphone。虽然我找不到变音素的原生.NET实现,但提供的链接有许多其他可以转换的实现的链接。最容易转换的可能是Java implementation of the Metaphone algorithm。
应该注意的是,Metaphone算法已经经过了修订。有Double Metaphone (它有一个.NET implementation)和Metaphone 3。Metaphone 3是一个商业应用程序,但在针对常见英语单词数据库运行时,与Double Metaphone算法89%的准确率相比,它的准确率为98%。根据您的需要,您可能希望查找(在双变音位的情况下)或购买(在变音位3的情况下)算法的源代码,并通过P/Invoke层转换或访问它(有大量的C++实现)。
Metaphone和Soundex的不同之处在于,Soundex生成固定长度的数字键,而Metaphone生成不同长度的键,因此结果将不同。最后,两者都会为你做同样的比较,你只需要找出哪一个最适合你的需求和资源(当然还有对拼写错误的不容忍程度)。
发布于 2016-11-24 06:14:03
下面是LevenshteinDistance方法的一个实现,它使用更少的内存,同时生成相同的结果。这是在此wikipedia article中找到的伪代码的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)));
}https://stackoverflow.com/questions/2344320
复制相似问题