Init from given files
This commit is contained in:
39
examples/js/distances/cosineSimilarity.js
Normal file
39
examples/js/distances/cosineSimilarity.js
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Calculate the distances by using the numbers, strings and dates.
|
||||
* @param {node} source
|
||||
* @param {node} target
|
||||
* @param {array} properties - the properties of the nodes.
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateCosineSimilarity(source, target, properties, normArgs) {
|
||||
var numerator = 0.0;
|
||||
|
||||
// console.log(properties);
|
||||
// Iterate through every column of data
|
||||
for (var i = 0; i < properties.length; i++) {
|
||||
property = properties[i];
|
||||
if (property.toLowerCase() !== "class" && property.toLowerCase() !== "app" && property.toLowerCase() !== "user" && property.toLowerCase() !== "weekday") {
|
||||
var s = source[property],
|
||||
t = target[property];
|
||||
|
||||
numerator += s * t;
|
||||
}
|
||||
}
|
||||
|
||||
let denominator = squareRooted(source, properties, normArgs) * squareRooted(target, properties, normArgs);
|
||||
|
||||
// console.log(Math.abs(numerator / denominator));
|
||||
return Math.abs(numerator / denominator);
|
||||
}
|
||||
|
||||
function squareRooted(node, properties, normArgs) {
|
||||
var sum = 0.0;
|
||||
|
||||
for (var i = 0, s; i < properties.length; i++) {
|
||||
var s = node[properties[i]];
|
||||
|
||||
sum += s * s;
|
||||
}
|
||||
|
||||
return Math.sqrt(sum);
|
||||
}
|
||||
Reference in New Issue
Block a user