Use PHP functions in JavaScript

JavaScript levenshtein

Calculate Levenshtein distance between two strings

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
function levenshtein (s1, s2) {
      // Calculate Levenshtein distance between two strings  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/levenshtein      // +            original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
      // +            bugfixed by: Onno Marsman
      // +             revised by: Andrea Giammarchi (http://webreflection.blogspot.com)
      // + reimplemented by: Brett Zamir (http://brett-zamir.me)
      // + reimplemented by: Alexander M Beedie      // *                example 1: levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld');
      // *                returns 1: 3
    if (s1 == s2) {
        return 0;
    } 
    var s1_len = s1.length;
    var s2_len = s2.length;
    if (s1_len === 0) {
        return s2_len;    }
    if (s2_len === 0) {
        return s1_len;
    }
     // BEGIN STATIC
    var split = false;
    try{
        split=!('0')[0];
    } catch (e){        split=true; // Earlier IE may not support access by string index
    }
    // END STATIC
    if (split){
        s1 = s1.split('');        s2 = s2.split('');
    }
 
    var v0 = new Array(s1_len+1);
    var v1 = new Array(s1_len+1); 
    var s1_idx=0, s2_idx=0, cost=0;
    for (s1_idx=0; s1_idx<s1_len+1; s1_idx++) {
        v0[s1_idx] = s1_idx;
    }    var char_s1='', char_s2='';
    for (s2_idx=1; s2_idx<=s2_len; s2_idx++) {
        v1[0] = s2_idx;
        char_s2 = s2[s2_idx - 1];
         for (s1_idx=0; s1_idx<s1_len;s1_idx++) {
            char_s1 = s1[s1_idx];
            cost = (char_s1 == char_s2) ? 0 : 1;
            var m_min = v0[s1_idx+1] + 1;
            var b = v1[s1_idx] + 1;            var c = v0[s1_idx] + cost;
            if (b < m_min) {
                m_min = b; }
            if (c < m_min) {
                m_min = c; }            v1[s1_idx+1] = m_min;
        }
        var v_tmp = v0;
        v0 = v1;
        v1 = v_tmp;    }
    return v0[s1_len];
}
external links: original PHP docs | raw js source

Examples

Running

1
levenshtein('Kevin van Zonneveld', 'Kevin van Sommeveld');

Should return

1
3

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have levenshtein goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
25 Oct '09 Permalink

q  @ Florian: PS, was this by any chance Firefox 3.0?

Gravatar
Kevin van Zonneveld
25 Oct '09 Permalink

q  @ Florian: Yeah witnessed on a colleague's PC recently. Should be fixed now!

Gravatar
Florian
24 Oct '09 Permalink

q  Thanks for this wonderful script! I used it for my javascript based search engine.
But it seems if there's a little bug on your website: when you come on this site, it redirects you to a site with only digg buttons for example. It would be good if you fix that!


Contribute a New function