-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpackage-scoring.test.ts
More file actions
675 lines (574 loc) · 21.1 KB
/
Copy pathpackage-scoring.test.ts
File metadata and controls
675 lines (574 loc) · 21.1 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
import { describe, it, expect } from 'vitest';
import {
extractQueryTerms,
escapeRegExp,
textMatchesTerms,
scoreTextField,
countMatchingTerms,
scoreDatasetRelevance,
readDcatExtra
} from '../../src/tools/package';
describe('extractQueryTerms', () => {
it('extracts terms from simple query', () => {
const result = extractQueryTerms('health data');
expect(result).toEqual(['health', 'data']);
});
it('removes stopwords', () => {
const result = extractQueryTerms('the health and the data');
expect(result).toEqual(['health', 'data']);
});
it('removes single-character terms', () => {
const result = extractQueryTerms('a b health x data y');
expect(result).toEqual(['health', 'data']);
});
it('handles case insensitivity', () => {
const result = extractQueryTerms('Health DATA');
expect(result).toEqual(['health', 'data']);
});
it('removes duplicate terms', () => {
const result = extractQueryTerms('health health data health');
expect(result).toEqual(['health', 'data']);
});
it('handles empty query', () => {
const result = extractQueryTerms('');
expect(result).toEqual([]);
});
it('handles query with only stopwords', () => {
const result = extractQueryTerms('the and or but');
expect(result).toEqual([]);
});
it('handles special characters', () => {
const result = extractQueryTerms('health-care, data.csv');
expect(result).toEqual(['health', 'care', 'data', 'csv']);
});
it('handles unicode characters', () => {
const result = extractQueryTerms('sanità mobilità');
expect(result).toEqual(['sanità', 'mobilità']);
});
it('handles numbers', () => {
const result = extractQueryTerms('covid19 year2024');
expect(result).toEqual(['covid19', 'year2024']);
});
});
describe('escapeRegExp', () => {
it('escapes special regex characters', () => {
expect(escapeRegExp('.')).toBe('\\.');
expect(escapeRegExp('*')).toBe('\\*');
expect(escapeRegExp('+')).toBe('\\+');
expect(escapeRegExp('?')).toBe('\\?');
expect(escapeRegExp('^')).toBe('\\^');
expect(escapeRegExp('$')).toBe('\\$');
});
it('escapes brackets and braces', () => {
expect(escapeRegExp('{')).toBe('\\{');
expect(escapeRegExp('}')).toBe('\\}');
expect(escapeRegExp('(')).toBe('\\(');
expect(escapeRegExp(')')).toBe('\\)');
expect(escapeRegExp('[')).toBe('\\[');
expect(escapeRegExp(']')).toBe('\\]');
});
it('escapes pipe and backslash', () => {
expect(escapeRegExp('|')).toBe('\\|');
expect(escapeRegExp('\\')).toBe('\\\\');
});
it('handles strings with multiple special characters', () => {
expect(escapeRegExp('a.b*c+d?')).toBe('a\\.b\\*c\\+d\\?');
});
it('does not modify regular text', () => {
expect(escapeRegExp('hello world')).toBe('hello world');
});
it('handles empty string', () => {
expect(escapeRegExp('')).toBe('');
});
});
describe('textMatchesTerms', () => {
it('matches single term in text', () => {
const result = textMatchesTerms('health data portal', ['health']);
expect(result).toBe(true);
});
it('matches multiple terms (any match)', () => {
const result = textMatchesTerms('environmental data', ['health', 'data']);
expect(result).toBe(true);
});
it('does not match when no terms match', () => {
const result = textMatchesTerms('environmental data', ['health', 'covid']);
expect(result).toBe(false);
});
it('handles undefined text', () => {
const result = textMatchesTerms(undefined, ['health']);
expect(result).toBe(false);
});
it('handles empty terms array', () => {
const result = textMatchesTerms('health data', []);
expect(result).toBe(false);
});
it('is case insensitive', () => {
const result = textMatchesTerms('HEALTH DATA', ['health']);
expect(result).toBe(true);
});
it('matches word boundaries', () => {
const result = textMatchesTerms('healthcare data', ['health']);
expect(result).toBe(false);
});
it('normalizes underscores to spaces', () => {
const result = textMatchesTerms('health_data', ['data']);
expect(result).toBe(true);
});
it('handles partial word matches correctly', () => {
const result = textMatchesTerms('environmental', ['environment']);
expect(result).toBe(false);
});
it('handles unicode text', () => {
const result = textMatchesTerms('dati sanitari', ['sanitari']);
expect(result).toBe(true);
});
});
describe('scoreTextField', () => {
it('returns weight when text matches terms', () => {
const result = scoreTextField('health data portal', ['health'], 5);
expect(result).toBe(5);
});
it('returns 0 when text does not match terms', () => {
const result = scoreTextField('environmental data', ['health'], 5);
expect(result).toBe(0);
});
it('handles undefined text', () => {
const result = scoreTextField(undefined, ['health'], 5);
expect(result).toBe(0);
});
it('handles empty terms', () => {
const result = scoreTextField('health data', [], 5);
expect(result).toBe(0);
});
it('uses correct weight', () => {
const result = scoreTextField('covid data', ['covid'], 10);
expect(result).toBe(10);
});
it('handles zero weight', () => {
const result = scoreTextField('health data', ['health'], 0);
expect(result).toBe(0);
});
});
describe('scoreDatasetRelevance', () => {
describe('basic scoring', () => {
it('scores dataset with matching title', () => {
const dataset = {
title: 'Health Data Portal',
notes: 'Environmental information',
tags: [],
organization: null
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.total).toBeGreaterThan(0);
expect(result.breakdown.title).toBeGreaterThan(0);
expect(result.breakdown.notes).toBe(0);
});
it('scores dataset with matching notes', () => {
const dataset = {
title: 'Data Portal',
notes: 'Health information system',
tags: [],
organization: null
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.total).toBeGreaterThan(0);
expect(result.breakdown.title).toBe(0);
expect(result.breakdown.notes).toBeGreaterThan(0);
});
it('scores dataset with matching tags', () => {
const dataset = {
title: 'Data Portal',
notes: 'Information system',
tags: [{ name: 'health' }, { name: 'data' }],
organization: null
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.total).toBeGreaterThan(0);
expect(result.breakdown.tags).toBeGreaterThan(0);
});
it('scores dataset with matching organization', () => {
const dataset = {
title: 'Data Portal',
notes: 'Information system',
tags: [],
organization: { title: 'Ministry of Health' }
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.total).toBeGreaterThan(0);
expect(result.breakdown.organization).toBeGreaterThan(0);
});
it('returns zero score when no matches', () => {
const dataset = {
title: 'Environmental Data',
notes: 'Climate information',
tags: [{ name: 'climate' }],
organization: { title: 'EPA' }
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.total).toBe(0);
});
});
describe('combined scoring', () => {
it('sums scores from multiple fields', () => {
const dataset = {
title: 'Health Data',
notes: 'Health information',
tags: [{ name: 'health' }],
organization: { title: 'Health Ministry' }
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.total).toBe(
result.breakdown.title +
result.breakdown.notes +
result.breakdown.tags +
result.breakdown.organization +
result.breakdown.holder +
result.breakdown.publisher
);
});
it('uses default weights correctly', () => {
const dataset = {
title: 'Health Data',
notes: '',
tags: [],
organization: null
};
const result = scoreDatasetRelevance('health', dataset);
// Default weight for title is 4
expect(result.breakdown.title).toBe(4);
});
it('uses custom weights correctly', () => {
const dataset = {
title: 'Health Data',
notes: '',
tags: [],
organization: null
};
const weights = { title: 10, notes: 5, tags: 3, organization: 1, holder: 4, publisher: 2 };
const result = scoreDatasetRelevance('health', dataset, weights);
expect(result.breakdown.title).toBe(10);
});
});
describe('edge cases', () => {
it('handles dataset with missing fields', () => {
const dataset = {};
const result = scoreDatasetRelevance('health', dataset);
expect(result.total).toBe(0);
});
it('handles empty query', () => {
const dataset = {
title: 'Health Data',
notes: 'Information'
};
const result = scoreDatasetRelevance('', dataset);
expect(result.total).toBe(0);
expect(result.terms).toEqual([]);
});
it('handles query with only stopwords', () => {
const dataset = {
title: 'Health Data',
notes: 'Information'
};
const result = scoreDatasetRelevance('the and or', dataset);
expect(result.total).toBe(0);
expect(result.terms).toEqual([]);
});
it('uses name as fallback when title is missing', () => {
const dataset = {
name: 'health-data-portal',
notes: 'Information'
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.total).toBeGreaterThan(0);
});
it('handles tags as strings', () => {
const dataset = {
title: 'Data Portal',
tags: ['health', 'data']
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.breakdown.tags).toBeGreaterThan(0);
});
it('handles organization.name when title is missing', () => {
const dataset = {
title: 'Data',
organization: { name: 'health-ministry' }
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.breakdown.organization).toBeGreaterThan(0);
});
it('handles owner_org as fallback', () => {
const dataset = {
title: 'Data',
owner_org: 'health-ministry'
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.breakdown.organization).toBeGreaterThan(0);
});
});
describe('returns structure', () => {
it('returns total, breakdown, and terms', () => {
const dataset = {
title: 'Health Data'
};
const result = scoreDatasetRelevance('health data', dataset);
expect(result).toHaveProperty('total');
expect(result).toHaveProperty('breakdown');
expect(result).toHaveProperty('terms');
expect(result.breakdown).toHaveProperty('title');
expect(result.breakdown).toHaveProperty('notes');
expect(result.breakdown).toHaveProperty('tags');
expect(result.breakdown).toHaveProperty('organization');
expect(result.breakdown).toHaveProperty('holder');
expect(result.breakdown).toHaveProperty('publisher');
});
it('includes extracted terms in result', () => {
const dataset = { title: 'Data' };
const result = scoreDatasetRelevance('health data portal', dataset);
expect(result.terms).toContain('health');
expect(result.terms).toContain('data');
expect(result.terms).toContain('portal');
});
});
});
describe('readDcatExtra', () => {
it('returns extras value when key is present', () => {
const dataset = {
extras: [
{ key: 'holder_name', value: 'Comune di Lecce' }
]
};
expect(readDcatExtra(dataset, 'holder_name')).toBe('Comune di Lecce');
});
it('prefers extras over root field when both are present', () => {
const dataset = {
holder_name: 'Regione Puglia',
extras: [
{ key: 'holder_name', value: 'Comune di Lecce' }
]
};
expect(readDcatExtra(dataset, 'holder_name')).toBe('Comune di Lecce');
});
it('falls back to root field when key not in extras', () => {
const dataset = {
holder_name: 'Health Canada',
extras: [
{ key: 'other_field', value: 'something' }
]
};
expect(readDcatExtra(dataset, 'holder_name')).toBe('Health Canada');
});
it('falls back to root field when extras is empty', () => {
const dataset = {
holder_name: 'data.gov',
extras: []
};
expect(readDcatExtra(dataset, 'holder_name')).toBe('data.gov');
});
it('falls back to root field when extras is absent', () => {
const dataset = {
publisher_name: 'Open Government'
};
expect(readDcatExtra(dataset, 'publisher_name')).toBe('Open Government');
});
it('returns empty string when neither extras nor root have the key', () => {
const dataset = {
extras: [{ key: 'other', value: 'x' }]
};
expect(readDcatExtra(dataset, 'holder_name')).toBe('');
});
it('returns empty string for completely empty dataset', () => {
const dataset = {};
expect(readDcatExtra(dataset, 'holder_name')).toBe('');
});
it('skips extras entry with empty string value', () => {
const dataset = {
holder_name: 'Root Value',
extras: [
{ key: 'holder_name', value: '' }
]
};
expect(readDcatExtra(dataset, 'holder_name')).toBe('Root Value');
});
it('skips extras entry with non-string value', () => {
const dataset = {
holder_name: 'Root Value',
extras: [
{ key: 'holder_name', value: 42 }
]
};
expect(readDcatExtra(dataset, 'holder_name')).toBe('Root Value');
});
it('handles publisher_name key', () => {
const dataset = {
publisher_name: 'Root Publisher',
extras: [
{ key: 'publisher_name', value: 'Extras Publisher' }
]
};
expect(readDcatExtra(dataset, 'publisher_name')).toBe('Extras Publisher');
});
});
describe('scoreDatasetRelevance — holder and publisher', () => {
it('scores holder from extras (DCAT-AP_IT pattern)', () => {
const dataset = {
title: 'Defibrillatori DAE',
organization: { name: 'regione-puglia', title: 'Regione Puglia' },
extras: [{ key: 'holder_name', value: 'Comune di Lecce' }]
};
const result = scoreDatasetRelevance('Comune di Lecce', dataset);
expect(result.breakdown.holder).toBeGreaterThan(0);
expect(result.breakdown.organization).toBe(0);
});
it('extras holder wins over root holder when they differ (federated portal fix)', () => {
const dataset = {
title: 'Dataset',
holder_name: 'Regione Puglia',
extras: [{ key: 'holder_name', value: 'Comune di Mesagne' }]
};
const withFix = scoreDatasetRelevance('Mesagne', dataset);
expect(withFix.breakdown.holder).toBeGreaterThan(0);
const datasetRootOnly = {
title: 'Dataset',
holder_name: 'Comune di Mesagne'
};
const withRoot = scoreDatasetRelevance('Mesagne', datasetRootOnly);
expect(withRoot.breakdown.holder).toBeGreaterThan(0);
});
it('scores publisher from extras', () => {
const dataset = {
title: 'Dataset',
extras: [{ key: 'publisher_name', value: 'Regione Siciliana' }]
};
const result = scoreDatasetRelevance('Siciliana', dataset);
expect(result.breakdown.publisher).toBeGreaterThan(0);
});
it('holder and publisher contribute to total', () => {
const dataset = {
title: 'Dataset',
extras: [
{ key: 'holder_name', value: 'Comune di Lecce' },
{ key: 'publisher_name', value: 'Comune di Lecce' }
]
};
const result = scoreDatasetRelevance('Lecce', dataset);
expect(result.breakdown.holder).toBeGreaterThan(0);
expect(result.breakdown.publisher).toBeGreaterThan(0);
expect(result.total).toBe(
result.breakdown.title +
result.breakdown.notes +
result.breakdown.tags +
result.breakdown.organization +
result.breakdown.holder +
result.breakdown.publisher
);
});
it('non-DCAT portal: holder and publisher score 0 when fields absent', () => {
const dataset = {
title: 'Health Dataset',
organization: { name: 'health-canada', title: 'Health Canada' }
};
const result = scoreDatasetRelevance('health', dataset);
expect(result.breakdown.holder).toBe(0);
expect(result.breakdown.publisher).toBe(0);
});
it('uses default weight 4 for holder', () => {
const dataset = {
title: 'Data',
extras: [{ key: 'holder_name', value: 'Comune di Lecce' }]
};
const result = scoreDatasetRelevance('Lecce', dataset);
expect(result.breakdown.holder).toBe(4);
});
it('uses default weight 2 for publisher', () => {
const dataset = {
title: 'Data',
extras: [{ key: 'publisher_name', value: 'Comune di Lecce' }]
};
const result = scoreDatasetRelevance('Lecce', dataset);
expect(result.breakdown.publisher).toBe(2);
});
});
describe('relevance dilution (regression from v0.4.122)', () => {
it('drops Italian stopwords, which used to carry a whole field', () => {
// `di` matched "Provincia Autonoma di Trento" and awarded the full holder
// weight on a query asking for Lecce.
expect(extractQueryTerms('defibrillatori Comune di Lecce')).toEqual([
'defibrillatori',
'comune',
'lecce'
]);
expect(extractQueryTerms('elenco dei siti della regione')).toEqual([
'elenco',
'siti',
'regione'
]);
});
it('scores a field by the share of terms it carries', () => {
const terms = ['defibrillatori', 'comune', 'lecce'];
expect(scoreTextField('Comune di Lecce', terms, 4)).toBe(2.7);
expect(scoreTextField('Comune di Martina Franca', terms, 4)).toBe(1.3);
expect(scoreTextField('Provincia Autonoma di Trento', terms, 4)).toBe(0);
});
it('still gives the full weight when every term matches', () => {
expect(scoreTextField('health data portal', ['health'], 5)).toBe(5);
expect(scoreTextField('mobilità urbana', ['mobilità', 'urbana'], 6)).toBe(6);
});
it('matches a term ending in an accented letter', () => {
// `\\b` is ASCII-only in JavaScript, so `mobilità` never found its boundary and
// scored 0 on a catalog that is mostly not in English.
expect(countMatchingTerms('mobilità urbana', ['mobilità'])).toBe(1);
expect(countMatchingTerms('qualità dell aria', ['qualità'])).toBe(1);
expect(countMatchingTerms('città metropolitana', ['città'])).toBe(1);
// and still respects boundaries
expect(countMatchingTerms('immobilità', ['mobilità'])).toBe(0);
});
it('counts matching terms without double counting', () => {
expect(countMatchingTerms('comune di lecce, comune', ['comune', 'lecce'])).toBe(2);
expect(countMatchingTerms(undefined, ['comune'])).toBe(0);
expect(countMatchingTerms('comune', [])).toBe(0);
});
});
describe('multilingual term extraction', () => {
it('keeps an all-caps acronym that collides with a stopword', () => {
// `un` is an Italian article and the United Nations: dropping it would rank
// `UN population` on `population` alone.
expect(extractQueryTerms('UN population')).toEqual(['un', 'population']);
expect(extractQueryTerms('EU open data')).toContain('eu');
});
it('still drops the lowercase article', () => {
expect(extractQueryTerms('un comune di lecce')).toEqual(['comune', 'lecce']);
});
it('never scores on a Solr operator, even though it is all-caps', () => {
// The acronym rule let `OR` through: `aria OR acqua` scored on three terms and
// a title carrying one of them got 4 × 1/3 instead of 4 × 1/2.
expect(extractQueryTerms('aria OR acqua')).toEqual(['aria', 'acqua']);
expect(extractQueryTerms('aria AND NOT rifiuti')).toEqual(['aria', 'rifiuti']);
// lowercase `or` was already a stopword; an English `Or` mid-sentence stays one too
expect(extractQueryTerms('water or sewage')).toEqual(['water', 'sewage']);
});
it('keeps a quoted operator: inside quotes Solr reads it as a literal', () => {
expect(extractQueryTerms('"OR" Oregon')).toEqual(['or', 'oregon']);
expect(extractQueryTerms('aria OR "AND"')).toEqual(['aria', 'and']);
// an escaped quote inside the phrase does not end it
expect(extractQueryTerms('"OR\\" AND" acqua')).toEqual(['or', 'and', 'acqua']);
});
it('matches across Unicode normal forms', () => {
const nfd = 'mobilita\u0300 urbana'; // decomposed
expect(countMatchingTerms(nfd, ['mobilità'])).toBe(1);
expect(extractQueryTerms('mobilita\u0300')).toEqual(['mobilità'.normalize('NFC')]);
});
});
describe('score total', () => {
it('does not leak binary-float noise into the output', () => {
// A real result printed as 8.299999999999999 before rounding the sum.
const dataset = {
title: 'Sinistri stradali rilevati nel territorio comunale',
notes: 'incidenti a Palermo',
tags: [{ name: 'incidenti' }],
organization: { title: 'Comune di Palermo' }
} as any;
const { total } = scoreDatasetRelevance('incidenti stradali Palermo', dataset);
expect(total).toBe(Math.round(total * 10) / 10);
});
});