@pie-lib/categorize 2.0.3-next.1 → 2.0.3-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +127 -0
- package/dist/index.js +148 -0
- package/dist/scoring.d.ts +16 -0
- package/dist/scoring.js +66 -0
- package/package.json +31 -11
- package/CHANGELOG.json +0 -17
- package/CHANGELOG.md +0 -552
- package/LICENSE.md +0 -5
- package/lib/index.js +0 -561
- package/lib/index.js.map +0 -1
- package/lib/scoring.js +0 -130
- package/lib/scoring.js.map +0 -1
- package/src/__tests__/index.test.js +0 -1262
- package/src/__tests__/scoring.test.js +0 -281
- package/src/__tests__/utils.js +0 -19
- package/src/index.js +0 -586
- package/src/scoring.js +0 -118
package/src/index.js
DELETED
|
@@ -1,586 +0,0 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
import { clone, remove } from 'lodash-es';
|
|
3
|
-
import { score } from './scoring';
|
|
4
|
-
|
|
5
|
-
export { score };
|
|
6
|
-
|
|
7
|
-
export const FooTwo = 'foo-two';
|
|
8
|
-
|
|
9
|
-
const log = debug('@pie-lib:categorize');
|
|
10
|
-
|
|
11
|
-
export const limitChoices = (choiceId, count, choices) => {
|
|
12
|
-
const out = choices.reduce(
|
|
13
|
-
(acc, id) => {
|
|
14
|
-
const foundCount = countInChoices(choiceId, acc.choices);
|
|
15
|
-
|
|
16
|
-
if (id !== choiceId || foundCount < count) {
|
|
17
|
-
acc.choices.push(id);
|
|
18
|
-
}
|
|
19
|
-
return acc;
|
|
20
|
-
},
|
|
21
|
-
{ choices: [] },
|
|
22
|
-
);
|
|
23
|
-
return out.choices;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Limit in an array of arrays.
|
|
28
|
-
* So for an array with [[1,1],[1,1]] if you call limitInArray(1, arr, 3) you'll get: [[1,1], [1]]
|
|
29
|
-
* @param {*} id
|
|
30
|
-
* @param {*} arrays
|
|
31
|
-
* @param {*} limit
|
|
32
|
-
*/
|
|
33
|
-
export const limitInArrays = (id, arrays, limit) => {
|
|
34
|
-
arrays = arrays || [];
|
|
35
|
-
|
|
36
|
-
const result = arrays.reduce(
|
|
37
|
-
(acc, array) => {
|
|
38
|
-
const l = limit - acc.count;
|
|
39
|
-
const result = limitInArray(id, array, l);
|
|
40
|
-
|
|
41
|
-
acc.out.push(result.array);
|
|
42
|
-
acc.count = acc.count + (result.count || 0);
|
|
43
|
-
|
|
44
|
-
return acc;
|
|
45
|
-
},
|
|
46
|
-
{ out: [], count: 0 },
|
|
47
|
-
);
|
|
48
|
-
|
|
49
|
-
return result.out;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Limit the id in the array to the limit set.
|
|
54
|
-
* So for an array with [1,1,1,1] if you call limitInArray(1, arr, 2) you'll get: [1,1]
|
|
55
|
-
* @param {string} id
|
|
56
|
-
* @param {string[]} arr
|
|
57
|
-
* @param {number} limit
|
|
58
|
-
*/
|
|
59
|
-
export const limitInArray = (id, arr, limit) => {
|
|
60
|
-
arr = arr || [];
|
|
61
|
-
|
|
62
|
-
if (limit === 0) {
|
|
63
|
-
const stripped = arr.filter((v) => v !== id);
|
|
64
|
-
|
|
65
|
-
return { array: stripped, count: arr.length - stripped.length };
|
|
66
|
-
} else {
|
|
67
|
-
const result = arr.reduce(
|
|
68
|
-
(acc, v) => {
|
|
69
|
-
if (v === id) {
|
|
70
|
-
if (acc.count < limit) {
|
|
71
|
-
acc.count += 1;
|
|
72
|
-
acc.out.push(v);
|
|
73
|
-
}
|
|
74
|
-
} else {
|
|
75
|
-
acc.out.push(v);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return acc;
|
|
79
|
-
},
|
|
80
|
-
{ out: [], count: 0 },
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
return { array: result.out, count: result.count };
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Ensure that there are no extra choices in answer, if a choice.categoryCount is > 0.
|
|
89
|
-
* @param {{category: string, choices: string[]}[]} answer
|
|
90
|
-
* @param {{id:string,categoryCount:number}} choices
|
|
91
|
-
*/
|
|
92
|
-
export const ensureNoExtraChoicesInAnswer = (answer, choices) => {
|
|
93
|
-
choices = choices || [];
|
|
94
|
-
|
|
95
|
-
const out = choices.reduce((answerArray, choice) => {
|
|
96
|
-
log('choice: ----> ', choice.id, 'categoryCount: ', choice.categoryCount);
|
|
97
|
-
|
|
98
|
-
if (choice.categoryCount === undefined || choice.categoryCount === 0) {
|
|
99
|
-
return answerArray;
|
|
100
|
-
} else {
|
|
101
|
-
const choices = answerArray.map((a) => a.choices);
|
|
102
|
-
const result = limitInArrays(choice.id, choices, choice.categoryCount);
|
|
103
|
-
|
|
104
|
-
const updatedArray = result.map((r, index) => {
|
|
105
|
-
return {
|
|
106
|
-
category: answerArray[index].category,
|
|
107
|
-
alternateResponses: answerArray[index].alternateResponses,
|
|
108
|
-
choices: r,
|
|
109
|
-
};
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
return updatedArray;
|
|
113
|
-
}
|
|
114
|
-
}, answer);
|
|
115
|
-
return out;
|
|
116
|
-
};
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Ensure that there are no extra choices in alternate responses, if a choice.categoryCount is > 0.
|
|
120
|
-
* @param {{category: string, choices: string[]}[]} answer
|
|
121
|
-
* @param {{id:string,categoryCount:number}} choices
|
|
122
|
-
*/
|
|
123
|
-
|
|
124
|
-
export const ensureNoExtraChoicesInAlternate = (answer, choices) => {
|
|
125
|
-
choices = choices || [];
|
|
126
|
-
|
|
127
|
-
const out = choices.reduce((answerArray, choice) => {
|
|
128
|
-
log('choice: ----> ', choice.id, 'categoryCount: ', choice.categoryCount);
|
|
129
|
-
|
|
130
|
-
if (choice.categoryCount === undefined || choice.categoryCount === 0) {
|
|
131
|
-
return answerArray;
|
|
132
|
-
} else {
|
|
133
|
-
let alternatePair = {};
|
|
134
|
-
|
|
135
|
-
answerArray.forEach((answer) =>
|
|
136
|
-
(answer.alternateResponses || []).forEach((alternate, index) => {
|
|
137
|
-
if (index in alternatePair) {
|
|
138
|
-
alternatePair[index].push(alternate);
|
|
139
|
-
} else {
|
|
140
|
-
alternatePair[index] = [];
|
|
141
|
-
alternatePair[index].push(alternate);
|
|
142
|
-
}
|
|
143
|
-
}),
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
const recreatedAlternate = [];
|
|
147
|
-
|
|
148
|
-
Object.keys(alternatePair).forEach((k) => {
|
|
149
|
-
const limitAlternate = limitInArrays(choice.id, alternatePair[k], choice.categoryCount);
|
|
150
|
-
alternatePair[k] = limitAlternate;
|
|
151
|
-
|
|
152
|
-
alternatePair[k].forEach((item, index) => {
|
|
153
|
-
if (!recreatedAlternate[index]) {
|
|
154
|
-
recreatedAlternate[index] = [];
|
|
155
|
-
recreatedAlternate[index].push(item);
|
|
156
|
-
} else {
|
|
157
|
-
recreatedAlternate[index].push(item);
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const updatedArray = answerArray.map((answer, index) => {
|
|
163
|
-
return {
|
|
164
|
-
category: answer.category,
|
|
165
|
-
alternateResponses: recreatedAlternate[index] || answer.alternateResponses,
|
|
166
|
-
choices: answer.choices,
|
|
167
|
-
};
|
|
168
|
-
});
|
|
169
|
-
return updatedArray;
|
|
170
|
-
}
|
|
171
|
-
}, answer);
|
|
172
|
-
|
|
173
|
-
return out;
|
|
174
|
-
};
|
|
175
|
-
/**
|
|
176
|
-
* Count the number of choice ids in a given answer array
|
|
177
|
-
* @param {string} choiceId
|
|
178
|
-
* @param {{category: string, choices: string[]}[]} answer
|
|
179
|
-
*/
|
|
180
|
-
export const countInAnswer = (choiceId, answer) => {
|
|
181
|
-
const out = answer.reduce((acc, a) => acc + countInChoices(choiceId, a.choices), 0);
|
|
182
|
-
|
|
183
|
-
log('[countInAnswer] choiceId:', choiceId, answer);
|
|
184
|
-
|
|
185
|
-
return out;
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
export const countInChoices = (choiceId, choices) => (choices || []).filter((c) => c === choiceId).length;
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Count the number of times a choice has been selected in categories.
|
|
192
|
-
* @param {*} choice
|
|
193
|
-
* @param {*} categories
|
|
194
|
-
*/
|
|
195
|
-
export const countChosen = (choice, categories) => {
|
|
196
|
-
if (!choice || !choice.id) {
|
|
197
|
-
return 0;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (!Array.isArray(categories)) {
|
|
201
|
-
return 0;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
return categories.reduce((acc, c) => {
|
|
205
|
-
const count = (c.choices || []).filter((h) => h && h.id === choice.id).length;
|
|
206
|
-
|
|
207
|
-
return acc + count;
|
|
208
|
-
}, 0);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* @param {string?} categoryId
|
|
213
|
-
* @param {{category:string, choices: string[]}} answers
|
|
214
|
-
*/
|
|
215
|
-
export const removeCategory = (categoryId, answers) => answers.filter((a) => a.category !== categoryId);
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* @param {string} choiceId
|
|
219
|
-
* @param {{category:string, choices: string[]}} answers
|
|
220
|
-
* @param {string?} categoryId - optional categegory id
|
|
221
|
-
*/
|
|
222
|
-
export const removeAllChoices = (choiceId, answers, categoryId) => {
|
|
223
|
-
return answers.map((a) => {
|
|
224
|
-
if (!categoryId || a.category === categoryId) {
|
|
225
|
-
const cloned = clone(a.choices);
|
|
226
|
-
|
|
227
|
-
remove(cloned, (v) => v === choiceId);
|
|
228
|
-
|
|
229
|
-
return { ...a, choices: cloned };
|
|
230
|
-
} else {
|
|
231
|
-
return a;
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
};
|
|
235
|
-
|
|
236
|
-
export const rearrangeChoices = (choices, indexFrom, indexTo) => {
|
|
237
|
-
if (choices.length === 0) {
|
|
238
|
-
return choices;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
choices.splice(indexFrom, 1, choices.splice(indexTo, 1, choices[indexFrom])[0]);
|
|
242
|
-
|
|
243
|
-
return choices;
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
export const verifyAllowMultiplePlacements = (choice, categoryId, answers) => {
|
|
247
|
-
return answers.map((a) => {
|
|
248
|
-
if (a.category !== categoryId) {
|
|
249
|
-
a.choices = (a.choices || []).filter((c) => c !== choice.id);
|
|
250
|
-
|
|
251
|
-
return a;
|
|
252
|
-
} else {
|
|
253
|
-
a.choices = a.choices.reduce((acc, currentValue) => {
|
|
254
|
-
if (currentValue.id === choice.id) {
|
|
255
|
-
const foundIndex = acc.findIndex((c) => c.id === choice.id);
|
|
256
|
-
|
|
257
|
-
if (foundIndex === -1) {
|
|
258
|
-
acc.push(currentValue);
|
|
259
|
-
}
|
|
260
|
-
} else {
|
|
261
|
-
acc.push(currentValue);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
return acc;
|
|
265
|
-
}, []);
|
|
266
|
-
|
|
267
|
-
return a;
|
|
268
|
-
}
|
|
269
|
-
});
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
export const removeChoiceFromCategory = (choiceId, categoryId, choiceIndex, answers) => {
|
|
273
|
-
log('[removeChoiceFromCategory] choiceIndex:', choiceIndex);
|
|
274
|
-
|
|
275
|
-
return answers.map((a) => {
|
|
276
|
-
if (a.category === categoryId) {
|
|
277
|
-
const cloned = clone(a.choices);
|
|
278
|
-
const index = cloned.findIndex((v, index) => {
|
|
279
|
-
return v === choiceId && index >= choiceIndex;
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
if (index !== -1) {
|
|
283
|
-
cloned.splice(index, 1);
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
return { ...a, choices: cloned };
|
|
287
|
-
} else {
|
|
288
|
-
return a;
|
|
289
|
-
}
|
|
290
|
-
});
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
export const removeChoiceFromAlternate = (choiceId, categoryId, choiceIndex, altIndex, answers) => {
|
|
294
|
-
return (answers || []).map((a) => {
|
|
295
|
-
if (a?.category === categoryId) {
|
|
296
|
-
const newAlternates = (a?.alternateResponses || []).map((altArray, idx) => {
|
|
297
|
-
if (idx === altIndex) {
|
|
298
|
-
(altArray || []).splice(choiceIndex, 1);
|
|
299
|
-
return altArray;
|
|
300
|
-
}
|
|
301
|
-
return altArray;
|
|
302
|
-
});
|
|
303
|
-
return { ...a, alternateResponses: newAlternates };
|
|
304
|
-
}
|
|
305
|
-
return a;
|
|
306
|
-
});
|
|
307
|
-
};
|
|
308
|
-
|
|
309
|
-
export const moveChoiceToCategory = (choiceId, from, to, choiceIndex, answers) => {
|
|
310
|
-
log('[moveChoiceToCategory] choice: ', choiceId, 'from: ', from, 'to: ', to, 'answers: ', answers);
|
|
311
|
-
|
|
312
|
-
if (from === to) {
|
|
313
|
-
return answers;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
if (from) {
|
|
317
|
-
answers = removeChoiceFromCategory(choiceId, from, choiceIndex, answers);
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
const index = answers.findIndex((a) => a.category === to);
|
|
321
|
-
if (index === -1) {
|
|
322
|
-
answers.push({ category: to, choices: [choiceId] });
|
|
323
|
-
|
|
324
|
-
return answers;
|
|
325
|
-
} else {
|
|
326
|
-
return answers.map((a) => {
|
|
327
|
-
if (a.category === to) {
|
|
328
|
-
a.choices = a.choices || [];
|
|
329
|
-
a.choices.push(choiceId);
|
|
330
|
-
|
|
331
|
-
return a;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return a;
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
export const moveChoiceToAlternate = (choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount) => {
|
|
340
|
-
log(
|
|
341
|
-
'[moveChoiceToAlternate] choice: ',
|
|
342
|
-
choiceId,
|
|
343
|
-
'from: ',
|
|
344
|
-
from,
|
|
345
|
-
'to: ',
|
|
346
|
-
to,
|
|
347
|
-
'answers: ',
|
|
348
|
-
answers,
|
|
349
|
-
'chocieIndex: ',
|
|
350
|
-
choiceIndex,
|
|
351
|
-
);
|
|
352
|
-
|
|
353
|
-
if (from === to) {
|
|
354
|
-
return answers;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
return answers.map((a) => {
|
|
358
|
-
if (a.category === to) {
|
|
359
|
-
if (categoryCount !== 0 && a.alternateResponses) {
|
|
360
|
-
a.alternateResponses[alternateIndex] = (a.alternateResponses[alternateIndex] || []).filter(
|
|
361
|
-
(resp) => resp !== choiceId,
|
|
362
|
-
);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
if (!a.alternateResponses) {
|
|
366
|
-
a.alternateResponses = [];
|
|
367
|
-
a.alternateResponses[alternateIndex] = [];
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
a.alternateResponses[alternateIndex].push(choiceId);
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
if (a.category === from && categoryCount !== 0 && a.alternateResponses && a.alternateResponses[alternateIndex]) {
|
|
374
|
-
a.alternateResponses[alternateIndex] = a.alternateResponses[alternateIndex].filter((resp) => resp !== choiceId);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
return a;
|
|
378
|
-
});
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
export const stillSelectable = (h, builtCategories) => {
|
|
382
|
-
if (h.categoryCount > 0) {
|
|
383
|
-
const count = countChosen(h, builtCategories);
|
|
384
|
-
|
|
385
|
-
return count < h.categoryCount;
|
|
386
|
-
} else {
|
|
387
|
-
return true;
|
|
388
|
-
}
|
|
389
|
-
};
|
|
390
|
-
|
|
391
|
-
/**
|
|
392
|
-
* returns all choices with property 'correct' set to boolean
|
|
393
|
-
* @param {string[]} possibleResponseChoices
|
|
394
|
-
* @param {Object[]} builtCategoryChoices
|
|
395
|
-
*
|
|
396
|
-
* @returns {Object}, returns builtChoices: BuiltChoice[] and allChoicesAreCorrect: boolean
|
|
397
|
-
* BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
|
|
398
|
-
*/
|
|
399
|
-
export const buildChoices = (possibleResponseChoices, builtCategoryChoices) => {
|
|
400
|
-
return builtCategoryChoices.reduce(
|
|
401
|
-
(acc, builtChoice) => {
|
|
402
|
-
// set correct value on each choice that was selected by user
|
|
403
|
-
const index = acc.copyOfPossibleResponse.findIndex((cRC) => builtChoice && cRC === builtChoice.id);
|
|
404
|
-
|
|
405
|
-
// if the choice exists in the correct response
|
|
406
|
-
// set the correct: true
|
|
407
|
-
if (index >= 0) {
|
|
408
|
-
acc.builtChoices.push({
|
|
409
|
-
...builtChoice,
|
|
410
|
-
correct: true,
|
|
411
|
-
});
|
|
412
|
-
acc.copyOfPossibleResponse = [
|
|
413
|
-
...acc.copyOfPossibleResponse.slice(0, index),
|
|
414
|
-
...acc.copyOfPossibleResponse.slice(index + 1),
|
|
415
|
-
];
|
|
416
|
-
} else {
|
|
417
|
-
acc.builtChoices.push({
|
|
418
|
-
...builtChoice,
|
|
419
|
-
correct: false,
|
|
420
|
-
});
|
|
421
|
-
acc.allChoicesAreCorrect = false;
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
return acc;
|
|
425
|
-
},
|
|
426
|
-
{
|
|
427
|
-
builtChoices: [],
|
|
428
|
-
copyOfPossibleResponse: [...possibleResponseChoices],
|
|
429
|
-
allChoicesAreCorrect: true,
|
|
430
|
-
},
|
|
431
|
-
);
|
|
432
|
-
};
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* returns corrected answer using this possible correct response
|
|
436
|
-
* @param {Object} possibleResponse, has form: { CategoryId: ChoiceId[] }
|
|
437
|
-
* @param {Object[]} builtCategories
|
|
438
|
-
*
|
|
439
|
-
* @returns {Object}, returns builtCategories: BuiltCategory[] and correct: boolean
|
|
440
|
-
* BuiltCategory = { id: categoryId, label: categoryLabel, correct: boolean, choices: BuiltChoice[] }
|
|
441
|
-
* BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
|
|
442
|
-
*/
|
|
443
|
-
export const getBuiltCategories = (possibleResponse, builtCategories) =>
|
|
444
|
-
builtCategories.reduce(
|
|
445
|
-
(acc, builtCategory) => {
|
|
446
|
-
const possibleResponseChoices = possibleResponse[builtCategory.id] || []; // the correct choices for this category
|
|
447
|
-
const builtCategoryChoices = builtCategory.choices || []; // the choices in the answer
|
|
448
|
-
|
|
449
|
-
const { builtChoices, allChoicesAreCorrect } = buildChoices(possibleResponseChoices, builtCategoryChoices);
|
|
450
|
-
const allChoicesAreInAnswer = builtCategoryChoices.length === possibleResponseChoices.length;
|
|
451
|
-
|
|
452
|
-
acc.builtCategories.push({
|
|
453
|
-
...builtCategory,
|
|
454
|
-
correct: allChoicesAreCorrect && allChoicesAreInAnswer,
|
|
455
|
-
choices: builtChoices,
|
|
456
|
-
});
|
|
457
|
-
acc.correct = acc.correct && allChoicesAreCorrect && allChoicesAreInAnswer;
|
|
458
|
-
|
|
459
|
-
return acc;
|
|
460
|
-
},
|
|
461
|
-
{ builtCategories: [], correct: true },
|
|
462
|
-
);
|
|
463
|
-
|
|
464
|
-
/**
|
|
465
|
-
* returns all the possible responses by combining the proper alternate responses
|
|
466
|
-
* @param {Object[]} correctResponse
|
|
467
|
-
*
|
|
468
|
-
* @returns {Object[]}, returns Response[]
|
|
469
|
-
* Response = { CategoryId: ChoiceId[] },
|
|
470
|
-
*/
|
|
471
|
-
export const getAllPossibleResponses = (correctResponse) => {
|
|
472
|
-
if (!correctResponse) {
|
|
473
|
-
return [];
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
return correctResponse.reduce((acc, cR) => {
|
|
477
|
-
// main response
|
|
478
|
-
if (acc[0]) {
|
|
479
|
-
acc[0][cR.category] = cR.choices;
|
|
480
|
-
} else {
|
|
481
|
-
acc.push({ [cR.category]: cR.choices });
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
// alternate responses
|
|
485
|
-
if (cR.alternateResponses) {
|
|
486
|
-
cR.alternateResponses.forEach((aR, index) => {
|
|
487
|
-
if (acc[index + 1]) {
|
|
488
|
-
acc[index + 1][cR.category] = aR;
|
|
489
|
-
} else {
|
|
490
|
-
acc.push({ [cR.category]: aR });
|
|
491
|
-
}
|
|
492
|
-
});
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
return acc;
|
|
496
|
-
}, []);
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
/**
|
|
500
|
-
* build the categories to be displayed, where each category has its own list of choices
|
|
501
|
-
* @param {Object[]} categories
|
|
502
|
-
* @param {{id:string}[]} choices
|
|
503
|
-
* @param {{category: string, choices:string[]}[]} answers
|
|
504
|
-
*
|
|
505
|
-
* @returns {Object[]}, returns BuiltCategory[]
|
|
506
|
-
* BuiltCategory = { id: categoryId, label: categoryLabel, choices: BuiltChoice[]},
|
|
507
|
-
* BuiltChoice = { id: choiceId, content: choiceContent }
|
|
508
|
-
*/
|
|
509
|
-
export const buildCategories = (categories, choices, answers) => {
|
|
510
|
-
categories = categories || [];
|
|
511
|
-
answers = answers || [];
|
|
512
|
-
choices = choices || [];
|
|
513
|
-
|
|
514
|
-
return categories.map((category) => {
|
|
515
|
-
const answer = answers.find((answer) => answer.category === category.id);
|
|
516
|
-
const { choices: answerChoices = [] } = answer || {};
|
|
517
|
-
|
|
518
|
-
return {
|
|
519
|
-
...category,
|
|
520
|
-
choices: answerChoices.map((chId) => choices.find((ch) => ch.id === chId)),
|
|
521
|
-
};
|
|
522
|
-
});
|
|
523
|
-
};
|
|
524
|
-
|
|
525
|
-
/**
|
|
526
|
-
* build the choice and category state
|
|
527
|
-
* @param {Object[]} categories
|
|
528
|
-
* @param {{id:string}[]} choices
|
|
529
|
-
* @param {{category: string, choices:string[]}[]} answers
|
|
530
|
-
*
|
|
531
|
-
* @param correctResponse
|
|
532
|
-
* @returns { categories: Category[], choices: Choice[], correct: boolean, bestResponse: Response }, where Response: { categoryId: choiceId[] }
|
|
533
|
-
*/
|
|
534
|
-
export const buildState = (categories, choices, answers, correctResponse) => {
|
|
535
|
-
categories = categories || [];
|
|
536
|
-
choices = choices || [];
|
|
537
|
-
answers = answers || [];
|
|
538
|
-
|
|
539
|
-
const allResponses = getAllPossibleResponses(correctResponse) || [];
|
|
540
|
-
|
|
541
|
-
let builtCategories = buildCategories(categories, choices, answers);
|
|
542
|
-
let bestResponse;
|
|
543
|
-
let entirelyCorrect;
|
|
544
|
-
|
|
545
|
-
// at least one defined correct response
|
|
546
|
-
if (allResponses.length) {
|
|
547
|
-
const builtData = allResponses.reduce(
|
|
548
|
-
(acc, possibleResponse) => {
|
|
549
|
-
// set correctness for each possible response
|
|
550
|
-
const { builtCategories: categoriesWithChoices, correct } = getBuiltCategories(
|
|
551
|
-
possibleResponse,
|
|
552
|
-
builtCategories,
|
|
553
|
-
);
|
|
554
|
-
|
|
555
|
-
// if bestResponse not found yet OR no correct response found yet and this one is the correct one
|
|
556
|
-
if (acc.bestResponse === null || (!acc.entirelyCorrect && correct)) {
|
|
557
|
-
return {
|
|
558
|
-
bestResponse: possibleResponse,
|
|
559
|
-
builtCategories: categoriesWithChoices,
|
|
560
|
-
entirelyCorrect: correct,
|
|
561
|
-
};
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
return acc;
|
|
565
|
-
},
|
|
566
|
-
{
|
|
567
|
-
bestResponse: null,
|
|
568
|
-
builtCategories: [],
|
|
569
|
-
entirelyCorrect: false,
|
|
570
|
-
},
|
|
571
|
-
);
|
|
572
|
-
|
|
573
|
-
builtCategories = builtData.builtCategories;
|
|
574
|
-
bestResponse = builtData.bestResponse;
|
|
575
|
-
entirelyCorrect = builtData.entirelyCorrect;
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
const filteredChoices = choices.map((ch) => (stillSelectable(ch, builtCategories) ? ch : { empty: true }));
|
|
579
|
-
|
|
580
|
-
return {
|
|
581
|
-
choices: filteredChoices,
|
|
582
|
-
categories: builtCategories,
|
|
583
|
-
correct: entirelyCorrect,
|
|
584
|
-
bestResponse,
|
|
585
|
-
};
|
|
586
|
-
};
|
package/src/scoring.js
DELETED
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
|
|
3
|
-
const log = debug('@pie-lib:categorize:scoring');
|
|
4
|
-
|
|
5
|
-
const getWeightingRules = (scoring) => {
|
|
6
|
-
if (!scoring || !scoring.weighting) {
|
|
7
|
-
return [];
|
|
8
|
-
} else {
|
|
9
|
-
if (!scoring.weighting.enabled) {
|
|
10
|
-
return [];
|
|
11
|
-
} else {
|
|
12
|
-
const rules = scoring.weighting ? scoring.weighting.rules || [] : [];
|
|
13
|
-
|
|
14
|
-
return rules;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
const getPartialRulesForCategory = (partial, categoryId) => {
|
|
20
|
-
log('partial? ', partial);
|
|
21
|
-
|
|
22
|
-
if (!partial || !partial.enabled) {
|
|
23
|
-
return;
|
|
24
|
-
} else {
|
|
25
|
-
const pr = partial.rules.find((pr) => pr.category === categoryId);
|
|
26
|
-
|
|
27
|
-
return pr ? pr.rules || [] : [];
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
*
|
|
33
|
-
* Calculate score based on the categories model generated from `buildState` and the partial scoring rules.
|
|
34
|
-
* @param {*} categories - this is the categories that you get from `buildState`.
|
|
35
|
-
* @param {*} scoring - scoring config.
|
|
36
|
-
* @see ../index#buildState
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
export const score = (categories, scoring) =>
|
|
40
|
-
new Promise((resolve) => {
|
|
41
|
-
log('categories: ', categories, 'scoring: ', scoring);
|
|
42
|
-
|
|
43
|
-
const weightingRules = getWeightingRules(scoring);
|
|
44
|
-
|
|
45
|
-
const weights = categories.map((c) => {
|
|
46
|
-
const r = weightingRules.find((r) => r.category === c.id) || { points: 1 };
|
|
47
|
-
|
|
48
|
-
return { ...r, category: c.id };
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
log('weights: ', weights);
|
|
52
|
-
|
|
53
|
-
const weightTotal = categories.reduce((total, c) => {
|
|
54
|
-
const r = weights.find((r) => r.category === c.id);
|
|
55
|
-
|
|
56
|
-
return (total += r.points);
|
|
57
|
-
}, 0);
|
|
58
|
-
|
|
59
|
-
log('total: ', weightTotal);
|
|
60
|
-
|
|
61
|
-
const withScore = categories.map((c) => {
|
|
62
|
-
const w = weights.find((r) => r.category === c.id).points;
|
|
63
|
-
|
|
64
|
-
log('category: ', c);
|
|
65
|
-
if (c.correct === true) {
|
|
66
|
-
return { category: c.id, score: w, points: w };
|
|
67
|
-
} else {
|
|
68
|
-
const rules = getPartialRulesForCategory(scoring.partial, c.id);
|
|
69
|
-
|
|
70
|
-
log('partial rules for category: ', rules);
|
|
71
|
-
|
|
72
|
-
if (!rules) {
|
|
73
|
-
return { category: c.id, score: 0, points: w };
|
|
74
|
-
} else {
|
|
75
|
-
const correctCount = (c.choices || []).filter((h) => h.correct).length;
|
|
76
|
-
|
|
77
|
-
log('correctCount: ', correctCount);
|
|
78
|
-
|
|
79
|
-
let rule = rules.find((u) => u.count === correctCount);
|
|
80
|
-
|
|
81
|
-
log('rule: ', rule);
|
|
82
|
-
|
|
83
|
-
rule = rule || { percent: 0, count: correctCount };
|
|
84
|
-
|
|
85
|
-
const score = w * (rule.percent / 100);
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
category: c.id,
|
|
89
|
-
score,
|
|
90
|
-
points: w,
|
|
91
|
-
partial: {
|
|
92
|
-
correctChoices: correctCount,
|
|
93
|
-
rule: rule,
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
const weightedScore = withScore.reduce((sum, c) => {
|
|
101
|
-
return (sum += c.score);
|
|
102
|
-
}, 0);
|
|
103
|
-
|
|
104
|
-
log('weightedScore: ', weightedScore);
|
|
105
|
-
|
|
106
|
-
const score = parseFloat((weightedScore / weightTotal).toFixed(2), 10);
|
|
107
|
-
|
|
108
|
-
resolve({
|
|
109
|
-
score,
|
|
110
|
-
details: {
|
|
111
|
-
weighted: {
|
|
112
|
-
total: weightTotal,
|
|
113
|
-
score: weightedScore,
|
|
114
|
-
},
|
|
115
|
-
categories: withScore,
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
});
|