String.levenshtein v1.0

This method calculates Levenshtein distance between two strings. In information theory and computer science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (called edit distance). The Levenshtein distance between two strings is given by minimum number of operations needed to transform one string into another given string, where possible operations are insertion, deletion, or substitution of a single character. The Levenshtein distance algorithm has been used in: * Spell checking * Speech recognition * DNA analysis * plagiarism detection

The complexity of the algorithm is O(mXn), where n and m are the length of string1 and string2.



Details

Author
Adrian Statescu
Current version
v1.0
GitHub
thinkphp/String.levenshtein
Downloads
1632
Category
Native
Tags
Report
GitHub Issues

Releases


Dependencies

  • core/1.3: *

How to use

First you must to include the JS files in the head of your HTML document.

        #HTML
        <script type="text/javascript" src="mootools.js"></script>
        <script type="text/javascript" src="String.levenshtein.js"></script>

In your JavaScript code.

       #JavaScript     
       var words = ["rodion","dunia","raskolnikov","adrian","statescu","sunday","saturday","jquery","mootools","dojo"];
       function matchWords(input) {

          return words.filter(function(word){

            //calculates levenshtein distance
            var lev = word.levenshtein(input);  

            //in this case the distance is in the iterval [0,1,2,3];
            if(lev >=0 && lev <= 3 ) {

               return word;
            }

         });    
        }
        var input = "rascolnicov";
        var similarities = matchWords(input);
        if(window.console) console.log("Did you mean: " + similarities);
        var similar = matchWords("motols"); 
        if(window.console) console.log("Did you mean: " + similar);     

Discuss

A note on comments here: These comments are moderated. No comments will show up until they are approved. Comments that are not productive (i.e. inflammatory, rude, etc) will not be approved.

Found a bug in this plugin? Please report it this repository's Github Issues.

blog comments powered by Disqus