我正在尝试用字典创建一个光学字符识别系统。
事实上我还没有实现字典=)
我听说有基于 Levenstein 距离的简单指标,它考虑了不同符号之间的不同距离。例如,'N' 和 'H' 彼此非常接近,并且 d("THEATRE", "TNEATRE") 应该小于 d("THEATRE", "TOEATRE") 使用基本 Levenstein 距离是不可能的。
你能帮我找到这样的指标吗?
我正在尝试用字典创建一个光学字符识别系统。
事实上我还没有实现字典=)
我听说有基于 Levenstein 距离的简单指标,它考虑了不同符号之间的不同距离。例如,'N' 和 'H' 彼此非常接近,并且 d("THEATRE", "TNEATRE") 应该小于 d("THEATRE", "TOEATRE") 使用基本 Levenstein 距离是不可能的。
你能帮我找到这样的指标吗?
这可能是您正在寻找的内容:http ://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance (链接中包含一些工作代码)
更新:
http://nlp.stanford.edu/IR-book/html/htmledition/edit-distance-1.html
这是一个示例 (C#),其中“替换字符”操作的权重取决于字符代码之间的距离:
static double WeightedLevenshtein(string b1, string b2) {
b1 = b1.ToUpper();
b2 = b2.ToUpper();
double[,] matrix = new double[b1.Length + 1, b2.Length + 1];
for (int i = 1; i <= b1.Length; i++) {
matrix[i, 0] = i;
}
for (int i = 1; i <= b2.Length; i++) {
matrix[0, i] = i;
}
for (int i = 1; i <= b1.Length; i++) {
for (int j = 1; j <= b2.Length; j++) {
double distance_replace = matrix[(i - 1), (j - 1)];
if (b1[i - 1] != b2[j - 1]) {
// Cost of replace
distance_replace += Math.Abs((float)(b1[i - 1]) - b2[j - 1]) / ('Z'-'A');
}
// Cost of remove = 1
double distance_remove = matrix[(i - 1), j] + 1;
// Cost of add = 1
double distance_add = matrix[i, (j - 1)] + 1;
matrix[i, j] = Math.Min(distance_replace,
Math.Min(distance_add, distance_remove));
}
}
return matrix[b1.Length, b2.Length] ;
}
你可以在这里看到它是如何工作的:http: //ideone.com/RblFK
几年太晚了,但是下面的 python 包(我不隶属于它)允许对所有 Levenshtein 编辑操作和 ASCII 字符映射等进行任意加权。
https://github.com/infoscout/weighted-levenshtein
pip install weighted-levenshtein
还有这个(也不是附属的):
https://github.com/luozhouyang/python-string-similarity