Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ngrams.test.ts 2.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. import { getNGrams } from '../../src/assertions/ngrams';
  2. describe('getNGrams', () => {
  3. it('should generate unigrams correctly', () => {
  4. const words = ['hello', 'world', 'how', 'are', 'you'];
  5. const expected = ['hello', 'world', 'how', 'are', 'you'];
  6. const result = getNGrams(words, 1);
  7. expect(result).toEqual(expected);
  8. });
  9. it('should generate bigrams correctly', () => {
  10. const words = ['hello', 'world', 'how', 'are', 'you'];
  11. const expected = ['hello world', 'world how', 'how are', 'are you'];
  12. const result = getNGrams(words, 2);
  13. expect(result).toEqual(expected);
  14. });
  15. it('should generate trigrams correctly', () => {
  16. const words = ['hello', 'world', 'how', 'are', 'you'];
  17. const expected = ['hello world how', 'world how are', 'how are you'];
  18. const result = getNGrams(words, 3);
  19. expect(result).toEqual(expected);
  20. });
  21. it('should handle n greater than words length', () => {
  22. const words = ['hello', 'world'];
  23. const result = getNGrams(words, 3);
  24. expect(result).toEqual([]);
  25. });
  26. it('should handle n equal to words length', () => {
  27. const words = ['hello', 'world', 'how'];
  28. const expected = ['hello world how'];
  29. const result = getNGrams(words, 3);
  30. expect(result).toEqual(expected);
  31. });
  32. it('should handle empty words array', () => {
  33. const words: string[] = [];
  34. const result = getNGrams(words, 1);
  35. expect(result).toEqual([]);
  36. });
  37. it('should handle sentence with repeated words', () => {
  38. const words = ['the', 'cat', 'the', 'cat'];
  39. const expected = ['the cat', 'cat the', 'the cat'];
  40. const result = getNGrams(words, 2);
  41. expect(result).toEqual(expected);
  42. });
  43. it('should handle single word array', () => {
  44. const words = ['hello'];
  45. const result = getNGrams(words, 1);
  46. expect(result).toEqual(['hello']);
  47. });
  48. it('should return empty array for n <= 0', () => {
  49. const words = ['hello', 'world'];
  50. // TypeScript allows this even though it doesn't make logical sense
  51. const result = getNGrams(words, 0);
  52. expect(result).toEqual([]);
  53. });
  54. it('should work with special characters in words', () => {
  55. const words = ['hello,', 'world!', 'how?'];
  56. const expected = ['hello, world!', 'world! how?'];
  57. const result = getNGrams(words, 2);
  58. expect(result).toEqual(expected);
  59. });
  60. it('should maintain word order', () => {
  61. const words = ['one', 'two', 'three', 'four', 'five'];
  62. const expected = ['one two three', 'two three four', 'three four five'];
  63. const result = getNGrams(words, 3);
  64. expect(result).toEqual(expected);
  65. expect(result).not.toEqual(['three two one', 'four three two', 'five four three']);
  66. });
  67. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...