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

gleu.test.ts 7.4 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  1. import { calculateGleuScore, handleGleuScore } from '../../src/assertions/gleu';
  2. import type { AssertionParams } from 'src/types';
  3. describe('GLEU score calculation', () => {
  4. it('identical sentences should have GLEU score of 1', () => {
  5. const references = ['The cat sat on the mat'];
  6. const candidate = 'The cat sat on the mat';
  7. const score = calculateGleuScore(candidate, references);
  8. expect(score).toBe(1);
  9. });
  10. it('should handle period after words', () => {
  11. const references = ['The cat sat on the mat'];
  12. const candidate = 'The cat sat on the mat.';
  13. const score = calculateGleuScore(candidate, references);
  14. expect(score).toBeGreaterThan(0.95);
  15. });
  16. it('should handle the infamous "the the the … " example', () => {
  17. const references = ['The cat sat on the mat'];
  18. const candidate = 'the the the the the the the';
  19. const score = calculateGleuScore(candidate, references);
  20. // Due to how n-grams are counted, this will be approximately 0.09
  21. expect(score).toBeCloseTo(0.09, 2);
  22. });
  23. it('should evaluate normal machine translation outputs correctly', () => {
  24. const references = [
  25. 'It is a guide to action that ensures that the military will forever heed Party commands',
  26. ];
  27. const candidate =
  28. 'It is a guide to action which ensures that the military always obeys the commands of the party';
  29. const score = calculateGleuScore(candidate, references);
  30. expect(score).toBeGreaterThan(0.35);
  31. expect(score).toBeLessThanOrEqual(0.46);
  32. });
  33. it('should calculate the minimum of precision and recall correctly', () => {
  34. // This test specifically checks the min(precision, recall) calculation
  35. const references = ['One two three four five'];
  36. const candidate = 'One two three';
  37. const score = calculateGleuScore(candidate, references);
  38. // Due to how n-grams are counted, the result is approximately 0.429
  39. expect(score).toBeCloseTo(0.429, 1);
  40. });
  41. it('should calculate correctly when candidate is longer', () => {
  42. const references = ['One two three'];
  43. const candidate = 'One two three four five';
  44. const score = calculateGleuScore(candidate, references);
  45. // Due to how n-grams are counted, the result is approximately 0.429
  46. expect(score).toBeCloseTo(0.429, 1);
  47. });
  48. it('should handle empty or single word sentences', () => {
  49. const references = ['cat'];
  50. const candidate = 'cat';
  51. const score = calculateGleuScore(candidate, references);
  52. expect(score).toBe(1);
  53. });
  54. it('should handle sentences with different lengths', () => {
  55. const references = ['The cat sat on the mat.'];
  56. const candidate = 'The cat sat.';
  57. const score = calculateGleuScore(candidate, references);
  58. // Due to how n-grams are counted, the result is approximately 0.33
  59. expect(score).toBeCloseTo(0.33, 2);
  60. });
  61. it('should handle multiple references and take best matching score', () => {
  62. const references = [
  63. 'The cat sat on the mat.',
  64. 'There is a cat on the mat.',
  65. 'A cat is sitting on the mat.',
  66. ];
  67. const candidate = 'The cat was sitting on the mat.';
  68. const score = calculateGleuScore(candidate, references);
  69. expect(score).toBeGreaterThanOrEqual(0.5);
  70. });
  71. it('should throw error for empty reference array', () => {
  72. expect(() => {
  73. calculateGleuScore('test', []);
  74. }).toThrow('Invalid inputs');
  75. });
  76. it('should handle multiple references with varying lengths', () => {
  77. const references = ['The small cat sat.', 'A cat was sitting.', 'The cat is on the mat.'];
  78. const candidate = 'The small cat sat.';
  79. const score = calculateGleuScore(candidate, references);
  80. expect(score).toBe(1);
  81. });
  82. it('should handle different minN values', () => {
  83. const references = ['The cat sat on the mat.'];
  84. const candidate = 'the the the the the the.';
  85. const score = calculateGleuScore(candidate, references, 2);
  86. expect(score).toBe(0); // This is 0 because there are no 2-grams in common
  87. });
  88. it('should handle different maxN values', () => {
  89. const references = ['The cat sat on the mat.'];
  90. const candidate = 'the the the the the the the.';
  91. // Only using unigrams (n=1) here
  92. const score = calculateGleuScore(candidate, references, 1, 1);
  93. // Due to how n-grams are counted, the result is approximately 0.286
  94. expect(score).toBeCloseTo(0.286, 1);
  95. });
  96. it('should aggregate n-gram matches across different n values', () => {
  97. const references = ['The cat sat on the mat'];
  98. const candidate = 'The cat on the mat';
  99. // Using n-grams from 1 to 2
  100. const score = calculateGleuScore(candidate, references, 1, 2);
  101. // Due to how n-grams are counted, the result is approximately 0.727
  102. expect(score).toBeCloseTo(0.727, 1);
  103. });
  104. describe('handleGleuScore', () => {
  105. it('should handle string reference with passing score', () => {
  106. const params = {
  107. assertion: { type: 'gleu', value: 'The cat sat on the mat.' },
  108. renderedValue: 'The cat sat on the mat.',
  109. outputString: 'The cat sat on the mat.',
  110. inverse: false,
  111. } as AssertionParams;
  112. expect(handleGleuScore(params)).toEqual({
  113. pass: true,
  114. score: expect.any(Number),
  115. reason: 'Assertion passed',
  116. assertion: expect.any(Object),
  117. });
  118. });
  119. it('should handle array of references', () => {
  120. const params = {
  121. assertion: {
  122. type: 'gleu',
  123. value: ['The cat sat on mat.', 'The cat is sitting on mat.'],
  124. },
  125. renderedValue: ['The cat sat on mat.', 'The cat is sitting on mat.'],
  126. outputString: 'The cat sat on mat.',
  127. inverse: false,
  128. } as AssertionParams;
  129. expect(handleGleuScore(params)).toEqual({
  130. pass: true,
  131. score: expect.any(Number),
  132. reason: 'Assertion passed',
  133. assertion: expect.any(Object),
  134. });
  135. });
  136. it('should handle custom threshold', () => {
  137. const params = {
  138. assertion: { type: 'gleu', value: 'The cat sat on the mat.', threshold: 0.8 },
  139. renderedValue: 'The cat sat on the mat.',
  140. outputString: 'The dog sat on the mat.',
  141. inverse: false,
  142. } as AssertionParams;
  143. expect(handleGleuScore(params)).toEqual({
  144. pass: false,
  145. score: expect.any(Number),
  146. reason: expect.stringMatching(/GLEU score \d+\.\d+ is less than threshold 0\.8/),
  147. assertion: expect.any(Object),
  148. });
  149. });
  150. it('should handle inverse assertion', () => {
  151. const params = {
  152. assertion: { type: 'gleu', value: 'The cat sat on the mat.', threshold: 0.8 },
  153. renderedValue: 'The cat sat on the mat.',
  154. outputString: 'The dog ran in the park.',
  155. inverse: true,
  156. } as AssertionParams;
  157. expect(handleGleuScore(params)).toEqual({
  158. pass: true,
  159. score: expect.any(Number),
  160. reason: 'Assertion passed',
  161. assertion: expect.any(Object),
  162. });
  163. });
  164. it('should use default threshold of 0.5', () => {
  165. const params = {
  166. assertion: { type: 'gleu', value: 'The cat sat on the mat.' },
  167. renderedValue: 'The cat sat on the mat.',
  168. outputString: 'The dog ran in the park.',
  169. inverse: false,
  170. } as AssertionParams;
  171. expect(handleGleuScore(params)).toEqual({
  172. pass: false,
  173. score: expect.any(Number),
  174. reason: expect.stringMatching(/GLEU score \d+\.\d+ is less than threshold 0\.5/),
  175. assertion: expect.any(Object),
  176. });
  177. });
  178. });
  179. });
Tip!

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

Comments

Loading...