forked from getomni-ai/benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.ts
More file actions
236 lines (214 loc) · 6.24 KB
/
json.ts
File metadata and controls
236 lines (214 loc) · 6.24 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { diff } from 'json-diff';
interface DiffStats {
additions: number;
deletions: number;
modifications: number;
total: number;
}
export interface AccuracyResult {
score: number;
fullJsonDiff: Record<string, any>;
jsonDiff: Record<string, any>;
jsonDiffStats?: DiffStats;
totalFields: number;
}
/**
* Calculates accuracy for JSON structure and primitive values only
*
* The accuracy is calculated as:
* 1 - (number of differences / total fields in actual)
*
* Differences include:
* - Additions: Fields present in predicted but not in actual
* - Deletions: Fields present in actual but not in predicted
* - Modifications: Fields present in both but with different values
*
* A score of 1.0 means the JSONs are identical
* A score of 0.0 means completely different
*/
export const calculateJsonAccuracy = (
actual: Record<string, any>,
predicted: Record<string, any>,
ignoreCases: boolean = false,
): AccuracyResult => {
// Convert strings to uppercase if ignoreCases is true
const processedActual = ignoreCases ? convertStringsToUppercase(actual) : actual;
const processedPredicted = ignoreCases
? convertStringsToUppercase(predicted)
: predicted;
// Get the diff result
const fullDiffResult = diff(processedActual, processedPredicted, {
full: true,
sort: true,
});
const diffResult = diff(processedActual, processedPredicted, { sort: true });
const totalFields = countTotalFields(processedActual);
if (!diffResult) {
// If there's no diff, the JSONs are identical
return {
score: 1,
jsonDiff: {},
fullJsonDiff: {},
jsonDiffStats: {
additions: 0,
deletions: 0,
modifications: 0,
total: 0,
},
totalFields,
};
}
const changes = countChanges(diffResult);
const score = Math.max(
0,
1 - (changes.additions + changes.deletions + changes.modifications) / totalFields,
);
return {
score: Number(score.toFixed(4)),
jsonDiff: diffResult,
fullJsonDiff: fullDiffResult,
jsonDiffStats: changes,
totalFields,
};
};
/**
* Recursively converts all string values in an object to uppercase
*/
const convertStringsToUppercase = (obj: any): any => {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((item) => convertStringsToUppercase(item));
}
const result: Record<string, any> = {};
for (const key in obj) {
const value = obj[key];
if (typeof value === 'string') {
result[key] = value.toUpperCase();
} else if (typeof value === 'object' && value !== null) {
result[key] = convertStringsToUppercase(value);
} else {
result[key] = value;
}
}
return result;
};
export const countChanges = (diffResult: any): DiffStats => {
const changes: DiffStats = {
additions: 0,
deletions: 0,
modifications: 0,
total: 0,
};
const traverse = (obj: any) => {
if (!obj || typeof obj !== 'object') {
return;
}
for (const key in obj) {
const value = obj[key];
if (Array.isArray(value)) {
// Handle array diffs
value.forEach((item) => {
// Check if item is in the expected [operation, element] format
if (!Array.isArray(item) || item.length !== 2) {
return;
}
const [operation, element] = item;
if (element === null || typeof element !== 'object') {
// Handle primitive value changes in arrays
switch (operation) {
case '+':
changes.additions++;
break;
case '-':
changes.deletions++;
break;
}
} else {
switch (operation) {
// Handle array element additions and deletions
case '+':
changes.additions += countTotalFields(element);
break;
case '-':
changes.deletions += countTotalFields(element);
break;
case '~':
// Handle array element modifications
traverse(element);
break;
}
}
});
} else {
if (key.endsWith('__deleted')) {
if (value === null || typeof value !== 'object') {
changes.deletions++;
} else {
changes.deletions += countTotalFields(value);
}
} else if (key.endsWith('__added')) {
if (value === null || typeof value !== 'object') {
changes.additions++;
} else {
changes.additions += countTotalFields(value);
}
} else if (typeof value === 'object' && value !== null) {
if (value.__old !== undefined && value.__new !== undefined) {
if (value.__old === null && value.__new !== null) {
changes.modifications += countTotalFields(value.__new) || 1;
} else {
changes.modifications += countTotalFields(value.__old) || 1;
}
} else {
traverse(value);
}
}
}
}
};
traverse(diffResult);
changes.total = changes.additions + changes.deletions + changes.modifications;
return changes;
};
export function countTotalFields(obj: any): number {
let count = 0;
const traverse = (current: any) => {
if (!current || typeof current !== 'object') {
return;
}
if (Array.isArray(current)) {
// Traverse into array elements if they're objects
current.forEach((item) => {
if (typeof item === 'object' && item !== null) {
traverse(item);
} else {
count++;
}
});
} else {
for (const key in current) {
// Skip diff metadata keys
if (key.includes('__')) {
continue;
}
// Only count primitive value fields
if (
current[key] === null ||
typeof current[key] === 'string' ||
typeof current[key] === 'number' ||
typeof current[key] === 'boolean'
) {
count++;
}
// Recurse into nested objects and arrays
else if (typeof current[key] === 'object') {
traverse(current[key]);
}
}
}
};
traverse(obj);
return count;
}