2015-06-20 57 views
-1

我在[R 尋找Levenshtein距離代碼Levenshtein距離的詳細代碼,但我在努力尋找一個。尋找R中

我找到了一些在C,C++,...這裏:

http://rosettacode.org/wiki/Levenshtein_distance#C 

你知道我在哪裏可以找到在R A 翻譯可以正常工作?

這是Ç發現提前rosettacode.org

include <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; 
} 

由於

+1

您可以自己搜索,例如'install.packages(「sos」);庫(SOS); findFn(「Levenshtein距離」)' – 2015-06-22 02:43:44

+0

非常有用@Pascal – giacomo

回答

1

stringdist封裝具有用於計算Levenshtein距離一個選項:

stringdist('abcabc','caca',method="lv") #4 

如果你想要完整的Damerau-Levenshtein距離:

stringdist('abcabc','caca',method="dl") #4 
+0

我正在尋找適當的代碼以適應某些東西 - 謝謝 – giacomo

+0

您可以從庫的源文件中訪問代碼 – jalapic