UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

96 lines (78 loc) 2.61 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://tessmore.nl" rel="nofollow"></a> </strong> on 2012-03-26 02:31:44 <br /> Not sure if it is shorter than Andrew Maxwell his version, but since it didn't make it.. I thought I give it a shot. <pre><code> function levenshtein(a, b) { var i, j, r=[]; r[0] = []; r[0][0] = 0; for(i=1; i&lt;=a.length; i++) { r[i] = []; r[i][0] = i; for(j=1; j&lt;=b.length; j++) { r[0][j] = j; r[i][j] = Math.min( r[i-1][j]+1, r[i][j-1]+1, r[i-1][j-1] + (a[i-1]!==b[j-1]) ); } } return r[a.length][b.length]; } </code></pre> <hr /> <strong> <a href="http://an3m1.com/" rel="nofollow">????? ????? ????</a> </strong> on 2012-03-22 13:52:04 <br /> New in the gaming world and all that exclusive games, visit the Portal Forums <hr /> <strong> Andrew Maxwell </strong> on 2011-03-01 19:04:07 <br /> 1. It's always faster to access array elements than characters in a string. Always split it. 2. This function is unnecessarily verbose. For most if not all circumstances, the following would more than suffice: <pre><code> function levenshtein (s1, s2) { if (s1===s2){return 0;} if (s1.length===0){return s2.length;} if (s2.length===0){return s1.length;} var v0=[],v1=[], j=0, k=0; s1=s1.split(''); s2=s2.split(''); for (j=0; j&lt;=s1.length; j++){v0[j]=j;} for (k=1; k&lt;=s2.length; k++){ v1[0]=k; for (j=0; j&lt;s1.length; j++){v1[j+1]=Math.min(v0[j+1]+1, v1[j]+1, v0[j]+((s1[j]===s2[k-1])? 0: 1));} var v_tmp = v0; v0 = v1; v1 = v_tmp; } return v0[s1.length]; } </code></pre> <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-25 14:10:14 <br /> @ Florian: PS, was this by any chance Firefox 3.0? <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2009-10-25 14:09:43 <br /> @ Florian: Yeah witnessed on a colleague's PC recently. Should be fixed now! <hr /> <strong> <a href="http://duran2.de/felektro" rel="nofollow">Florian</a> </strong> on 2009-10-24 11:23:57 <br /> 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! <hr />