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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
- import { getNGrams } from '../../src/assertions/ngrams';
- describe('getNGrams', () => {
- it('should generate unigrams correctly', () => {
- const words = ['hello', 'world', 'how', 'are', 'you'];
- const expected = ['hello', 'world', 'how', 'are', 'you'];
- const result = getNGrams(words, 1);
- expect(result).toEqual(expected);
- });
- it('should generate bigrams correctly', () => {
- const words = ['hello', 'world', 'how', 'are', 'you'];
- const expected = ['hello world', 'world how', 'how are', 'are you'];
- const result = getNGrams(words, 2);
- expect(result).toEqual(expected);
- });
- it('should generate trigrams correctly', () => {
- const words = ['hello', 'world', 'how', 'are', 'you'];
- const expected = ['hello world how', 'world how are', 'how are you'];
- const result = getNGrams(words, 3);
- expect(result).toEqual(expected);
- });
- it('should handle n greater than words length', () => {
- const words = ['hello', 'world'];
- const result = getNGrams(words, 3);
- expect(result).toEqual([]);
- });
- it('should handle n equal to words length', () => {
- const words = ['hello', 'world', 'how'];
- const expected = ['hello world how'];
- const result = getNGrams(words, 3);
- expect(result).toEqual(expected);
- });
- it('should handle empty words array', () => {
- const words: string[] = [];
- const result = getNGrams(words, 1);
- expect(result).toEqual([]);
- });
- it('should handle sentence with repeated words', () => {
- const words = ['the', 'cat', 'the', 'cat'];
- const expected = ['the cat', 'cat the', 'the cat'];
- const result = getNGrams(words, 2);
- expect(result).toEqual(expected);
- });
- it('should handle single word array', () => {
- const words = ['hello'];
- const result = getNGrams(words, 1);
- expect(result).toEqual(['hello']);
- });
- it('should return empty array for n <= 0', () => {
- const words = ['hello', 'world'];
- // TypeScript allows this even though it doesn't make logical sense
- const result = getNGrams(words, 0);
- expect(result).toEqual([]);
- });
- it('should work with special characters in words', () => {
- const words = ['hello,', 'world!', 'how?'];
- const expected = ['hello, world!', 'world! how?'];
- const result = getNGrams(words, 2);
- expect(result).toEqual(expected);
- });
- it('should maintain word order', () => {
- const words = ['one', 'two', 'three', 'four', 'five'];
- const expected = ['one two three', 'two three four', 'three four five'];
- const result = getNGrams(words, 3);
- expect(result).toEqual(expected);
- expect(result).not.toEqual(['three two one', 'four three two', 'five four three']);
- });
- });
|