Permalink
Browse files

working version

  • Loading branch information...
aceakash committed Nov 23, 2014
0 parents commit a512e50458d96b58d8d2a3d7035fef9792fe3c2e
Showing with 94 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +40 −0 compare-strings.js
  3. +27 −0 compare-strings.spec.js
  4. +10 −0 gulpfile.js
  5. +15 −0 package.json
@@ -0,0 +1,2 @@
.idea
node_modules
@@ -0,0 +1,40 @@
module.exports = function compareStrings(str1, str2) {
var pairs1 = wordLetterPairs(str1.toUpperCase());
var pairs2 = wordLetterPairs(str2.toUpperCase());
var intersection = 0;
var union = pairs1.length + pairs2.length;
for(var i=0; i<pairs1.length; i++) {
var pair1 = pairs1[i];
for(var j=0; j <pairs2.length; j++) {
var pair2 = pairs2[j];
if (pair1 === pair2) {
intersection++;
pairs2.splice(j, 1);
break;
}
}
}
return (2.0*intersection)/union;

// private functions ---------------------------
function letterPairs(str) {
var numPairs = str.length - 1;
var pairs = [];
for(var i=0; i<numPairs; i++) {
pairs[i] = str.substring(i, i+2);
}
return pairs;
}

function wordLetterPairs(str) {
var allPairs = [];
var words = str.split(' ');
for(var w=0; w < words.length; w++) {
var pairsInWord = letterPairs(words[w]);
for(var p=0; p < pairsInWord.length; p++) {
allPairs.push(pairsInWord[p]);
}
}
return allPairs;
}
};
@@ -0,0 +1,27 @@
var compareStrings = require('./compare-strings');

describe('compareStrings', function () {
it('is a function', function () {
expect(typeof compareStrings).toBe('function');
});

it('returns 0.8 for healed and sealed', function () {
expect(compareStrings('healed', 'sealed')).toEqual(0.8);
});

it('returns 0 for french and quebec', function () {
expect(compareStrings('french', 'quebec')).toEqual(0);
});

it('returns 0.896551724137931 for "web applications" and "applications of the web"', function () {
expect(compareStrings('web applications', 'applications of the web')).toEqual(0.896551724137931);
});

it('returns 0.9 for "this will have a typo somewhere" and "this will huve a typo somewhere"', function () {
expect(compareStrings("this will have a typo somewhere", "this will huve a typo somewhere")).toEqual(0.9);
});

it('returns 0.8333333333333334 for "this has one extra word" and "this has one word"', function () {
expect(compareStrings("this has one extra word", "this has one word")).toEqual(0.8333333333333334);
});
});
@@ -0,0 +1,10 @@
var gulp = require('gulp');
var jasmine = require('gulp-jasmine');

gulp.task('test', function () {
gulp
.src('compare-strings.spec.js')
.pipe(jasmine());
});

gulp.task('default', ['test']);
@@ -0,0 +1,15 @@
{
"name": "string-similarity",
"version": "1.0.0",
"description": "Finds degree of similarity between two strings, based on Dice's Coefficient, which is mostly better than Levenshtein distance.",
"main": "compare-strings.js",
"scripts": {
"test": "gulp"
},
"author": "Akash Kurdekar <npm@kurdekar.com> (http://untilfalse.com/)",
"license": "ISC",
"devDependencies": {
"gulp": "^3.8.10",
"gulp-jasmine": "^1.0.1"
}
}

0 comments on commit a512e50

Please sign in to comment.