Skip to content

Commit

Permalink
Added text distance measure: Jaro-Winkler and Levenshtein (edit) dist…
Browse files Browse the repository at this point in the history
…ances.
  • Loading branch information
adamb0mb committed Mar 13, 2012
1 parent 1a7d862 commit 4101593
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/natural/distance/jaro-winkler_distance.js
@@ -0,0 +1,65 @@
/*
Copyright (c) 2012, Adam Phillabaum
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Unless otherwise stated by a specific section of code
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


// Computes the Jaro distance between two string -- intrepreted from:
// http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance
// s1 is the first string to compare
// s2 is the second string to compare
function jaro_distance(s1,s2) {
match_window = (Math.floor(Math.max(s1.length,s2.length))/2)-1;
matches = Array();
m = 0;
for (i=0;i<s1.length;i++) {
matches[i] = Array();
matched = false;
// this for loop is a little brutal
for (k=(i<=match_window)? 0 : i - match_window;
(k < i+match_window) && k<s2.length && !matched;
k++) {
if (s1[i] == s2[k]) {
m++;
matched = true;
}
}
}
t = 0; // hack for now... this should actually be computed
return (1/3)*(m/s1.length + m/s2.length + (m - t)/m)
}

// Computes the Winkler distance between two string -- intrepreted from:
// http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance
// s1 is the first string to compare
// s2 is the second string to compare
// dj is the Jaro Distance (if you've already computed it), leave blank and the method handles it
function JaroWinklerDistance(s1, s2, dj) {
(typeof(dj) == 'undefined')? jaro = jaro_distance(s1,s2) : jaro = dj;
p = 0.1; //
l=0 // length of the matching prefix
while(s1[l] == s2[l] && l <4)
l++;

return jaro + l*p*(1-jaro);
}
module.exports = JaroWinklerDistance;
23 changes: 23 additions & 0 deletions lib/natural/distance/levenshtein_distance.js
@@ -0,0 +1,23 @@
// This function is licensed under Creative Commons Share Alike V3 - http://en.wikibooks.org/wiki/Wikibooks:Creative_Commons_Attribution-ShareAlike_3.0_Unported_License
// Computes the Levenshtein distance between two string -- originally found on:
// http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript
// s1 is the first string to compare
// s2 is the second string to compare
function LevenshteinDistance(s1, s2) {
var d = [0], c = [0]; // two rows of the distance matrix
var distance;
for(var e=0; s1[e]; e++) // loop through a and reset the 1st distance
for(var f=0; s2[++f];) // loop through b and reset the 1st col of the next row
distance=
d[f]=
(e == 0)? // not the first row ?
1+Math.min( // then compute the cost of each change
d[--f],
c[f]-(s1[e-1]==s2[f]),
c[++f]=d[f] // and copy the previous row of the distance matrix
)
: // otherwise
f; // init the very first row of the distance matrix
return distance;
}
module.exports = LevenshteinDistance;
2 changes: 2 additions & 0 deletions lib/natural/index.js
Expand Up @@ -39,3 +39,5 @@ exports.TfIdf = require('./tfidf/tfidf');
exports.SentenceAnalyzer = require('./analyzers/sentence_analyzer');
exports.stopwords = require('./util/stopwords').words;
exports.NGrams = require('./ngrams/ngrams');
exports.LevenshteinDistance = require('./distance/levenshtein_distance');
exports.JaroWinklerDistance = require('./distance/jaro-winkler_distance');

0 comments on commit 4101593

Please sign in to comment.