Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions __tests__/unit/cosine-similarity.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import cosineSimilarity from "../../packages/graph/src/cosine-similarity";

describe('cosineSimilarity abnormal demo: ', () => {
it('item contains only zeros: ', () => {
const item = [0, 0, 0];
const targetTtem = [3, 1, 1];
const cosineSimilarityValue = cosineSimilarity(item, targetTtem);
expect(cosineSimilarityValue).toBe(0);
});
it('targetTtem contains only zeros: ', () => {
const item = [3, 5, 2];
const targetTtem = [0, 0, 0];
const cosineSimilarityValue = cosineSimilarity(item, targetTtem);
expect(cosineSimilarityValue).toBe(0);
});
it('item and targetTtem both contains only zeros: ', () => {
const item = [0, 0, 0];
const targetTtem = [0, 0, 0];
const cosineSimilarityValue = cosineSimilarity(item, targetTtem);
expect(cosineSimilarityValue).toBe(0);
});
});

describe('cosineSimilarity normal demo: ', () => {
it('demo similar: ', () => {
const item = [30, 0, 100];
const targetTtem = [32, 1, 120];
const cosineSimilarityValue = cosineSimilarity(item, targetTtem);
expect(cosineSimilarityValue).toBeGreaterThanOrEqual(0);
expect(cosineSimilarityValue).toBeLessThan(1);
expect(Number(cosineSimilarityValue.toFixed(3))).toBe(0.999);
});
it('demo dissimilar: ', () => {
const item = [10, 300, 2];
const targetTtem = [1, 2, 30];
const cosineSimilarityValue = cosineSimilarity(item, targetTtem);
expect(cosineSimilarityValue).toBeGreaterThanOrEqual(0);
expect(cosineSimilarityValue).toBeLessThan(1);
expect(Number(cosineSimilarityValue.toFixed(3))).toBe(0.074);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"build:ci": "pnpm -r run build:ci",
"prepare": "husky install",
"test": "jest",
"test_one": "jest ./__tests__/unit/kCore.spec.ts",
"test_one": "jest ./__tests__/unit/cosine-similarity.spec.ts",
"coverage": "jest --coverage",
"build:site": "vite build",
"deploy": "gh-pages -d site/dist",
Expand Down
29 changes: 29 additions & 0 deletions packages/graph/src/cosine-similarity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Vector } from "./vector";

/**
Calculates the cosine similarity
@param item - The element.
@param targetItem - The target element.
@returns The cosine similarity between the item and the targetItem.
*/
const cosineSimilarity = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nodes-cosine-similarity 也可以一起提~

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item: number[],
targetItem: number[],
): number => {
// Vector of the target element
const targetItemVector = new Vector(targetItem);
// Norm of the target element vector
const targetNodeNorm2 = targetItemVector.norm2();
// Vector of the item
const itemVector = new Vector(item);
// Norm of the item vector
const itemNorm2 = itemVector.norm2();
// Calculate the dot product of the item vector and the target element vector
const dot = targetItemVector.dot(itemVector);
const norm2Product = targetNodeNorm2 * itemNorm2;
// Calculate the cosine similarity between the item vector and the target element vector
const cosineSimilarity = norm2Product ? dot / norm2Product : 0;
return cosineSimilarity;
}

export default cosineSimilarity;