我正在寻找 R 中的Levenshtein距离代码 但我很难找到一个。
我在C,C ++中发现了一些......在这里:
http://rosettacode.org/wiki/Levenshtein_distance#C
你知道在哪里可以找到R中的翻译吗?
这是 rosettacode.org
中的 Cinclude <stdio.h>
include <string.h>
/* s, t: two strings; ls, lt: their respective length */
int levenshtein(const char *s, int ls, const char *t, int lt)
{
int a, b, c;
/* if either string is empty, difference is inserting all chars
* from the other
*/
if (!ls) return lt;
if (!lt) return ls;
/* if last letters are the same, the difference is whatever is
* required to edit the rest of the strings
*/
if (s[ls] == t[ls])
return levenshtein(s, ls - 1, t, lt - 1);
/* else try:
* changing last letter of s to that of t; or
* remove last letter of s; or
* remove last letter of t,
* any of which is 1 edit plus editing the rest of the strings
*/
a = levenshtein(s, ls - 1, t, lt - 1);
b = levenshtein(s, ls, t, lt - 1);
c = levenshtein(s, ls - 1, t, lt );
if (a > b) a = b;
if (a > c) a = c;
return a + 1;
}
int main()
{
const char *s1 = "rosettacode";
const char *s2 = "raisethysword";
printf("distance between `%s' and `%s': %d\n", s1, s2,
levenshtein(s1, strlen(s1), s2, strlen(s2)));
return 0;
}
提前致谢
答案 0 :(得分:1)
stringdist
包有一个计算Levenshtein距离的选项:
stringdist('abcabc','caca',method="lv") #4
如果你想要Full Damerau-Levenshtein距离:
stringdist('abcabc','caca',method="dl") #4