-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcosineSimilarity.test.js
More file actions
56 lines (47 loc) · 2.25 KB
/
cosineSimilarity.test.js
File metadata and controls
56 lines (47 loc) · 2.25 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const assert = require('assert');
const cosineSimilarity = require('../algorithms/cosineSimilarity');
describe('Cosine Similarity', () => {
it('should return 1 for identical strings', () => {
assert.strictEqual(cosineSimilarity('hello', 'hello'), 1);
assert.strictEqual(cosineSimilarity('test', 'test'), 1);
assert.strictEqual(cosineSimilarity('', ''), 1);
});
it('should return 0 for orthogonal vectors (no common characters)', () => {
const result = cosineSimilarity('abc', 'xyz');
assert.strictEqual(result, 0);
});
it('should handle empty strings', () => {
const result1 = cosineSimilarity('', 'hello');
const result2 = cosineSimilarity('hello', '');
assert(isNaN(result1) || result1 === 0, 'Should handle empty strings gracefully');
assert(isNaN(result2) || result2 === 0, 'Should handle empty strings gracefully');
});
it('should calculate similarity based on character frequency vectors', () => {
const result = cosineSimilarity('hello', 'help');
assert(result > 0 && result < 1, 'Should return value between 0 and 1 for partial similarity');
});
it('should handle repeated characters correctly', () => {
const result = cosineSimilarity('aaa', 'aa');
assert.strictEqual(result, 1); // Same character distribution
});
it('should be symmetric', () => {
const str1 = 'hello';
const str2 = 'world';
assert.strictEqual(cosineSimilarity(str1, str2), cosineSimilarity(str2, str1));
});
it('should work with case sensitivity', () => {
const result1 = cosineSimilarity('Hello', 'hello');
const result2 = cosineSimilarity('hello', 'hello');
assert(result1 < result2, 'Should be case sensitive');
});
it('should handle single character strings', () => {
assert.strictEqual(cosineSimilarity('a', 'a'), 1);
assert.strictEqual(cosineSimilarity('a', 'b'), 0);
});
it('should work with different character frequencies', () => {
// 'aab' has vector {a:2, b:1}
// 'ab' has vector {a:1, b:1}
const result = cosineSimilarity('aab', 'ab');
assert(result > 0.8, 'Should have high similarity for similar character distributions');
});
});