แก้ coding style ภาค 10
This commit is contained in:
@@ -5,14 +5,14 @@
|
||||
* @param {array} properties - the properties of the nodes.
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateCosineSimilarity(source, target, properties, normArgs) {
|
||||
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") {
|
||||
if (property.toLowerCase() !== 'class' && property.toLowerCase() !== 'app' && property.toLowerCase() !== 'user' && property.toLowerCase() !== 'weekday') {
|
||||
var s = source[property],
|
||||
t = target[property];
|
||||
|
||||
@@ -26,7 +26,7 @@ function calculateCosineSimilarity(source, target, properties, normArgs) {
|
||||
return Math.abs(numerator / denominator);
|
||||
}
|
||||
|
||||
function squareRooted(node, properties, normArgs) {
|
||||
function squareRooted (node, properties, normArgs) {
|
||||
var sum = 0.0;
|
||||
|
||||
for (var i = 0, s; i < properties.length; i++) {
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
* @param {array} properties - the properties of the nodes.
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateDiceDissimilarity(source, target, properties, normArgs) {
|
||||
function calculateDiceDissimilarity (source, target, properties, normArgs) {
|
||||
var notShared = 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") {
|
||||
if (property.toLowerCase() !== 'class' && property.toLowerCase() !== 'app' && property.toLowerCase() !== 'user' && property.toLowerCase() !== 'weekday') {
|
||||
var s = source[property],
|
||||
t = target[property];
|
||||
|
||||
|
||||
@@ -6,23 +6,23 @@
|
||||
* @param {object} normArgs - the normalization arguments.
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateDistance(source, target, properties, normArgs) {
|
||||
function calculateDistance (source, target, properties, normArgs) {
|
||||
var val1 = 0.0, val2 = 0.0,
|
||||
sumDiff = 0.0,
|
||||
ordDiff = 1.0,
|
||||
ORD_FACTOR = 0.75,
|
||||
cols = 0,
|
||||
average = normArgs.avg,
|
||||
sigma = normArgs.sig,
|
||||
st_dev = normArgs.st_d;
|
||||
sumDiff = 0.0,
|
||||
ordDiff = 1.0,
|
||||
ORD_FACTOR = 0.75,
|
||||
cols = 0,
|
||||
average = normArgs.avg,
|
||||
sigma = normArgs.sig,
|
||||
st_dev = normArgs.st_d;
|
||||
|
||||
// Iterate through every column of data
|
||||
for (var i = 0; i < properties.length; i++) {
|
||||
property = properties[i];
|
||||
if (source.hasOwnProperty(property) && target.hasOwnProperty(property)
|
||||
&& property.toLowerCase() !== "index" && property.toLowerCase() !== "type") {
|
||||
if (source.hasOwnProperty(property) && target.hasOwnProperty(property) &&
|
||||
property.toLowerCase() !== 'index' && property.toLowerCase() !== 'type') {
|
||||
var s = source[property],
|
||||
t = target[property];
|
||||
t = target[property];
|
||||
|
||||
// Comparing Floats and Integers
|
||||
if ((isNumeric(s) && isNumeric(t))) {
|
||||
@@ -32,7 +32,7 @@ function calculateDistance(source, target, properties, normArgs) {
|
||||
val1 = (val1 - average[i]) / (st_dev[i] * sigma[i]);
|
||||
val2 = (val2 - average[i]) / (st_dev[i] * sigma[i]);
|
||||
}
|
||||
sumDiff += (val1-val2) * (val1-val2);
|
||||
sumDiff += (val1 - val2) * (val1 - val2);
|
||||
cols++;
|
||||
// Comparing strings
|
||||
} else if (/[a-zA-Z]/.test(s) && /[a-zA-Z]/.test(t) && s === t) {
|
||||
@@ -42,9 +42,8 @@ function calculateDistance(source, target, properties, normArgs) {
|
||||
// Comparing Dates
|
||||
var parsedDateS = Date.parse(s);
|
||||
var parsedDateT = Date.parse(t);
|
||||
if (isNaN(s) && !isNaN(parsedDateS)
|
||||
&& isNaN(t) && !isNaN(parsedDateT)) {
|
||||
|
||||
if (isNaN(s) && !isNaN(parsedDateS) &&
|
||||
isNaN(t) && !isNaN(parsedDateT)) {
|
||||
val1 = parsedDateS.valueOf(),
|
||||
val2 = parsedDateT.valueOf();
|
||||
|
||||
@@ -52,7 +51,7 @@ function calculateDistance(source, target, properties, normArgs) {
|
||||
val1 = (val1 - average[i]) / (st_dev[i] * sigma[i]);
|
||||
val2 = (val2 - average[i]) / (st_dev[i] * sigma[i]);
|
||||
}
|
||||
sumDiff += (val1-val2) * (val1-val2);
|
||||
sumDiff += (val1 - val2) * (val1 - val2);
|
||||
cols++;
|
||||
}
|
||||
}
|
||||
@@ -62,9 +61,9 @@ function calculateDistance(source, target, properties, normArgs) {
|
||||
sumDiff *= ordDiff;
|
||||
|
||||
if (cols > 0) {
|
||||
sumDiff *= properties.length/cols;
|
||||
sumDiff *= properties.length / cols;
|
||||
}
|
||||
|
||||
//console.log(sumDiff);
|
||||
// console.log(sumDiff);
|
||||
return sumDiff;
|
||||
}
|
||||
|
||||
@@ -6,33 +6,33 @@
|
||||
* @param {node} target
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateDistancePoker(source, target) {
|
||||
function calculateDistancePoker (source, target) {
|
||||
var sumDiff = 0.0,
|
||||
ordDiff = 1.0,
|
||||
ORD_FACTOR = 1.5,
|
||||
cards = ["C1", "C2", "C3", "C4", "C5"],
|
||||
cols = 0;
|
||||
ordDiff = 1.0,
|
||||
ORD_FACTOR = 1.5,
|
||||
cards = ['C1', 'C2', 'C3', 'C4', 'C5'],
|
||||
cols = 0;
|
||||
|
||||
// Iterate through cards
|
||||
for (var i = 0; i < cards.length; i++) {
|
||||
card = cards[i];
|
||||
if (source.hasOwnProperty(card) && target.hasOwnProperty(card)) {
|
||||
var s = parseInt(source[card]),
|
||||
t = parseInt(target[card]);
|
||||
t = parseInt(target[card]);
|
||||
// Calculate the squared difference.
|
||||
sumDiff += (s-t) * (s-t);
|
||||
sumDiff += (s - t) * (s - t);
|
||||
}
|
||||
}
|
||||
|
||||
// Class of poker hands describes the similarities the best
|
||||
// so give it more priority than checking the differences between cards.
|
||||
if (source.hasOwnProperty("CLASS") && target.hasOwnProperty("CLASS")) {
|
||||
var s = parseInt(source["CLASS"]),
|
||||
t = parseInt(target["CLASS"]);
|
||||
if (source.hasOwnProperty('CLASS') && target.hasOwnProperty('CLASS')) {
|
||||
var s = parseInt(source['CLASS']),
|
||||
t = parseInt(target['CLASS']);
|
||||
|
||||
// If classes differ, then scale them by a factor.
|
||||
if (s !== t) {
|
||||
ordDiff *= (ORD_FACTOR * (Math.abs(s-t)))
|
||||
ordDiff *= (ORD_FACTOR * (Math.abs(s - t)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,4 +40,4 @@ function calculateDistancePoker(source, target) {
|
||||
sumDiff *= ordDiff;
|
||||
|
||||
return sumDiff;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
* @param {array} properties - the properties of the nodes.
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateEuclideanDistance(source, target, properties, normArgs) {
|
||||
function calculateEuclideanDistance (source, target, properties, normArgs) {
|
||||
var sumDiff = 0.0;
|
||||
|
||||
// console.log(normArgs);
|
||||
// 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" && property.toLowerCase() !== "type") {
|
||||
if (property.toLowerCase() !== 'class' && property.toLowerCase() !== 'app' && property.toLowerCase() !== 'user' && property.toLowerCase() !== 'weekday' && property.toLowerCase() !== 'type') {
|
||||
var s = source[property],
|
||||
t = target[property];
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* @param {array} properties - the properties of the nodes.
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateEuclideanDistanceTSNE(source, target, properties, normArgs) {
|
||||
function calculateEuclideanDistanceTSNE (source, target, properties, normArgs) {
|
||||
var dotProduct = 0.0,
|
||||
sumX = 0.0,
|
||||
sumY = 0.0;
|
||||
@@ -15,7 +15,7 @@ function calculateEuclideanDistanceTSNE(source, target, properties, normArgs) {
|
||||
for (var i = 0; i < properties.length; i++) {
|
||||
property = properties[i];
|
||||
if (source.hasOwnProperty(property) && target.hasOwnProperty(property) &&
|
||||
property.toLowerCase() !== "class") {
|
||||
property.toLowerCase() !== 'class') {
|
||||
var s = source[property],
|
||||
t = target[property];
|
||||
|
||||
|
||||
@@ -5,14 +5,14 @@
|
||||
* @param {array} properties - the properties of the nodes.
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateJaccardDissimilarity(source, target, properties, normArgs) {
|
||||
function calculateJaccardDissimilarity (source, target, properties, normArgs) {
|
||||
var notShared = 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") {
|
||||
if (property.toLowerCase() !== 'class' && property.toLowerCase() !== 'app' && property.toLowerCase() !== 'user' && property.toLowerCase() !== 'weekday') {
|
||||
var s = source[property],
|
||||
t = target[property];
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* @param {array} properties - the properties of the nodes.
|
||||
* @return {number} the distance between source and target nodes.
|
||||
*/
|
||||
function calculateManhattanDistance(source, target, properties, normArgs) {
|
||||
function calculateManhattanDistance (source, target, properties, normArgs) {
|
||||
var sum = 0.0,
|
||||
cols = 0;
|
||||
|
||||
@@ -13,7 +13,7 @@ function calculateManhattanDistance(source, target, properties, normArgs) {
|
||||
// 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") {
|
||||
if (property.toLowerCase() !== 'class' && property.toLowerCase() !== 'app' && property.toLowerCase() !== 'user' && property.toLowerCase() !== 'weekday') {
|
||||
var s = source[property],
|
||||
t = target[property];
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* @param {array} nodes
|
||||
* @return {object} that contains the normalization parameters.
|
||||
*/
|
||||
function calculateNormalization(nodes) {
|
||||
function calculateNormalization (nodes) {
|
||||
var STANDARD_DEV = 2.0,
|
||||
properties = Object.keys(nodes[0]),
|
||||
sums = calculateSums(nodes, properties),
|
||||
@@ -23,10 +23,8 @@ function calculateNormalization(nodes) {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function standardDevation(nodes, properties, avg) {
|
||||
|
||||
var stDev = new Array(properties.length).fill(0)
|
||||
function standardDevation (nodes, properties, avg) {
|
||||
var stDev = new Array(properties.length).fill(0);
|
||||
|
||||
for (var i = 0; i < properties.length; i++) {
|
||||
var sum = 0;
|
||||
@@ -48,11 +46,10 @@ function standardDevation(nodes, properties, avg) {
|
||||
sum += Math.pow(val - propAvg, 2);
|
||||
});
|
||||
|
||||
stDev[i] = Math.sqrt(sum/nodes.length);
|
||||
stDev[i] = Math.sqrt(sum / nodes.length);
|
||||
}
|
||||
|
||||
return stDev;
|
||||
|
||||
return stDev;
|
||||
}
|
||||
|
||||
// Calculate the sum of values and the squared sum
|
||||
@@ -63,7 +60,7 @@ function standardDevation(nodes, properties, avg) {
|
||||
* @return {object} that contains arrays with sum of values
|
||||
* and the squared sums.
|
||||
*/
|
||||
function calculateSums(nodes, properties) {
|
||||
function calculateSums (nodes, properties) {
|
||||
var sumOfValues = new Array(properties.length).fill(0),
|
||||
sumOfSquares = new Array(properties.length).fill(0);
|
||||
|
||||
@@ -90,4 +87,4 @@ function calculateSums(nodes, properties) {
|
||||
sumOfVal: sumOfValues,
|
||||
sumOfSq: sumOfSquares
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,6 @@
|
||||
* @param {object} n - object to check.
|
||||
* @return {Boolean} true, if it is a number, false otherwise.
|
||||
*/
|
||||
function isNumeric(n) {
|
||||
function isNumeric (n) {
|
||||
return !isNaN(parseFloat(n)) && isFinite(n);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user