Last active
May 18, 2016 14:08
-
-
Save brh55/58dfa84595b61082e9767909827fb921 to your computer and use it in GitHub Desktop.
Similarity Coefficients in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Quickly wrote this to save me time in class instead of calculating all of this stuff | |
/** | |
var d1 = [0.8, 0.7, 0.9, 0, 0, 0.2]; | |
var d2 = [0.4, 0.3, 0.5, 0, 0, 0]; | |
var d3 = [0.9, 0, 0.8, 0.5, 0, 0]; | |
var d4 = [0.2, 0, 0, 0.8, 0, 0.9]; | |
var d5 = [0.7, 0.9, 0.7, 0.4, 0, 0]; | |
var d6 = [0.3, 0.6, 0.7, 0.7, 0, 0]; | |
similarity(d1, d2, "dice"); // 0.8571428571428571 | |
similarity(d1, d2, "jaccard"); // 0.75 | |
similarity(d1, d2, "cosine"); // 0.8660254037844387 | |
similarity(d1, d2, "overlap"); // 1 | |
**/ | |
/** | |
* Calculate the similarity measure of two documents within a matrix | |
* @param {array} d1 order dependent list of document 1 and term values | |
* @param {array} d2 order dependent list of document 2 and term values | |
* @param {string} coefficientMeasure the coefficient measure: jaccard, dice, cosine, or overlap | |
* @return {int} the coefficient measure of desired equation | |
*/ | |
function similarity(d1, d2, coefficientMeasure) { | |
var AND = [], | |
OR = [], | |
d1List = [], | |
d2List = []; | |
for (var i = 0; i < d1.length; i++) { | |
if (d1[i] > 0 & d2[i] > 0) { | |
AND.push(i); | |
} | |
if (d1[i] > 0 || d2[i] > 0) { | |
OR.push(i); | |
} | |
if (d1[i] > 0) { | |
d1List.push(i); | |
} | |
if (d2[i] > 0) { | |
d2List.push(i); | |
} | |
} | |
var n, d; | |
var coefficient = coefficientMeasure.toLowerCase(); | |
switch (coefficient) { | |
case "jaccard": | |
n = AND.length; | |
d = OR.length; | |
break; | |
case "dice": | |
n = 2 * AND.length; | |
d = d1List.length + d2List.length; | |
break; | |
case "cosine": | |
n = AND.length; | |
d = (Math.sqrt(d1List.length) * Math.sqrt(d2List.length)) | |
break; | |
case "overlap": | |
n = AND.length; | |
d = Math.min(d1List.length, d2List.length); | |
break; | |
} | |
return n/d; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment