mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-12-14 07:38:14 +00:00
feat(utils): compute cosine similarity, remove package
This commit is contained in:
@@ -29,7 +29,6 @@
|
||||
"axios": "^1.8.3",
|
||||
"better-sqlite3": "^11.9.1",
|
||||
"clsx": "^2.1.0",
|
||||
"compute-cosine-similarity": "^1.1.0",
|
||||
"drizzle-orm": "^0.40.1",
|
||||
"framer-motion": "^12.23.24",
|
||||
"html-to-text": "^9.0.5",
|
||||
|
||||
@@ -1,7 +1,22 @@
|
||||
import cosineSimilarity from 'compute-cosine-similarity';
|
||||
|
||||
const computeSimilarity = (x: number[], y: number[]): number => {
|
||||
return cosineSimilarity(x, y) as number;
|
||||
if (x.length !== y.length)
|
||||
throw new Error('Vectors must be of the same length');
|
||||
|
||||
let dotProduct = 0;
|
||||
let normA = 0;
|
||||
let normB = 0;
|
||||
|
||||
for (let i = 0; i < x.length; i++) {
|
||||
dotProduct += x[i] * y[i];
|
||||
normA += x[i] * x[i];
|
||||
normB += y[i] * y[i];
|
||||
}
|
||||
|
||||
if (normA === 0 || normB === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
|
||||
};
|
||||
|
||||
export default computeSimilarity;
|
||||
|
||||
Reference in New Issue
Block a user