我需要测量名称以字符串形式提供的两个位置之间的物理距离。由于有时名称的书写略有不同,我正在寻找一个库,它可以帮助我测量差异,然后将其与纬度和经度的度量相结合,以选择正确的匹配项。首选语言: Java或PHP。
有什么建议吗?
发布于 2009-05-25 20:45:11
看一看Levenshtein distance。这是一种测量两个字符串彼此不同程度的方法。
希望我正确理解了你的问题;在同一句话中使用“距离”和“纬度和经度”可能会让人感到困惑!
发布于 2009-05-25 20:59:10
虽然是用c编写的(使用python和tcl绑定),但libdistance将是一个在字符串/数据上应用几个距离度量的工具。
指标包括:
发布于 2009-05-25 21:50:01
我擅自将我为计算Levenshtein距离而编写的一段C#代码翻译成了Java代码。它只使用两个交替的一维数组,而不是一个大的锯齿数组:
public static int getDifference(String a, String b)
{
// Minimize the amount of storage needed:
if (a.length() > b.length())
{
// Swap:
String x = a;
a = b;
b = x;
}
// Store only two rows of the matrix, instead of a big one
int[] mat1 = new int[a.length() + 1];
int[] mat2 = new int[a.length() + 1];
int i;
int j;
for (i = 1; i <= a.length(); i++)
mat1[i] = i;
mat2[0] = 1;
for (j = 1; j <= b.length(); j++)
{
for (i = 1; i <= a.length(); i++)
{
int c = (a.charAt(i - 1) == b.charAt(j - 1) ? 0 : 1);
mat2[i] =
Math.min(mat1[i - 1] + c,
Math.min(mat1[i] + 1, mat2[i - 1] + 1));
}
// Swap:
int[] x = mat1;
mat1 = mat2;
mat2 = x;
mat2[0] = mat1[0] + 1;
}
// It's row #1 because we swap rows at the end of each outer loop,
// as we are to return the last number on the lowest row
return mat1[a.length()];
}它没有经过严格的测试,但似乎工作正常。它基于我为一次大学练习制作的Python实现。希望这能有所帮助!
https://stackoverflow.com/questions/907997
复制相似问题