-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredict_LR_worker.js
More file actions
31 lines (25 loc) · 902 Bytes
/
Copy pathpredict_LR_worker.js
File metadata and controls
31 lines (25 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Define function to compute cosine similarity between two vectors
function predictSimilarityScore(weights, bias, vector) {
let prediction = 0;
for (let i = 0; i < weights.length; i++){
for (let j = 0; j < vector.length; j++) {
prediction += vector[i] * weights[i];
}
}
prediction += bias;
return prediction;
}
// Add event listener for incoming messages
self.addEventListener("message", function (event) {
model = event.data[0];
testData = event.data[1];
// console.log(testData)
// Compute cosine similarity scores between test vector and all vectors in the corpus
testData = testData.map(obj =>{
obj.score = predictSimilarityScore(model.weights, model.bias, obj.vectors);
return obj;
})
testData = testData.sort((a, b) => a.score - b.score);
// Send the similarity scores back to the main thread
self.postMessage(testData);
});