JSFiddle - React, Tailwind, and code Playground

by snehil suresh wakchaure

JavaScript

function getNormalizedVector(multiDimArray, vector){
	var normalizedVector = [];

	if(vector.length == supportedVectorLength){
		var normalizedValue = 0;

		for(var j = 0; j < supportedVectorLength ; j++){

			var min = getMinMaxForMultidimensionalArrayColumn(multiDimArray,j)[0];
			var max = getMinMaxForMultidimensionalArrayColumn(multiDimArray,j)[1];

			normalizedValue = (max == min) ? 0.5 : (vector[j] - min) / (max - min);
			normalizedVector.push(normalizedValue);	
		}
	}
	//console.log('normalizedVector='+normalizedVector);
	return normalizedVector;
}

function getCosineSimilarityScore(vectorA, vectorB) {
    
	var similarityScore;

	if((vectorA.length == supportedVectorLength) && (vectorB.length == supportedVectorLength)){
		var lenVectA = vectorA.length,
	        product = 0,      
	        normVectorA = 0,
	        normVectorB = 0;
	    for (var i = 0; i < lenVectA ; i++) {
	        product += vectorA[i] * vectorB[i];
	        normVectorA += vectorA[i] * vectorA[i];
	        normVectorB += vectorB[i] * vectorB[i];
	    }

		similarityScore = 	product / (Math.sqrt(normVectorA) * Math.sqrt(normVectorB));
	}
	else {
		//TODO: Handle exception/ Fire an event to notify the server about this exception
		console.log("Cosine similarity workload vectors are of unequal lengths");
	}

	return similarityScore;
    
}

function getWeightedVector(vector) {

	var vectorArray = []; //Initialize 

	if(vector.length == supportedVectorLength){
		for(var j = 0; j < supportedVectorLength ; j++){
			vectorArray.push(defaultVectorWeights[j]*vector[j]);
		}
	}
	else{
		//TODO: Handle exception/ Fire an event to notify the server about this exception
		console.log("Cosine similarity workload vector is of unsupported length");
	}

	return vectorArray;
}