@natlibfi/melinda-record-matching 1.0.6 → 2.0.0-alpha.1

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.
Files changed (64) hide show
  1. package/dist/candidate-search/candidate-search-utils.js +58 -0
  2. package/dist/candidate-search/candidate-search-utils.js.map +1 -0
  3. package/dist/candidate-search/index.js +4 -4
  4. package/dist/candidate-search/index.js.map +1 -1
  5. package/dist/candidate-search/index.spec.js +94 -86
  6. package/dist/candidate-search/index.spec.js.map +1 -1
  7. package/dist/candidate-search/query-list/bib.js +103 -20
  8. package/dist/candidate-search/query-list/bib.js.map +1 -1
  9. package/dist/candidate-search/query-list/bib.spec.js +25 -23
  10. package/dist/candidate-search/query-list/bib.spec.js.map +1 -1
  11. package/dist/candidate-search/query-list/index.js +6 -4
  12. package/dist/candidate-search/query-list/index.js.map +1 -1
  13. package/dist/index.js +3 -3
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.spec.js +53 -46
  16. package/dist/index.spec.js.map +1 -1
  17. package/dist/match-detection/features/bib/all-source-ids.js +122 -0
  18. package/dist/match-detection/features/bib/all-source-ids.js.map +1 -0
  19. package/dist/match-detection/features/bib/authors.js.map +1 -1
  20. package/dist/match-detection/features/bib/bibliographic-level.js.map +1 -1
  21. package/dist/match-detection/features/bib/host-component.js.map +1 -1
  22. package/dist/match-detection/features/bib/index.js +32 -16
  23. package/dist/match-detection/features/bib/index.js.map +1 -1
  24. package/dist/match-detection/features/bib/index.spec.js +50 -42
  25. package/dist/match-detection/features/bib/index.spec.js.map +1 -1
  26. package/dist/match-detection/features/bib/isbn.js.map +1 -1
  27. package/dist/match-detection/features/bib/issn.js.map +1 -1
  28. package/dist/match-detection/features/bib/language.js +42 -5
  29. package/dist/match-detection/features/bib/language.js.map +1 -1
  30. package/dist/match-detection/features/bib/melinda-id.js +52 -0
  31. package/dist/match-detection/features/bib/melinda-id.js.map +1 -0
  32. package/dist/match-detection/features/bib/melinda-identifier-factory.js +100 -0
  33. package/dist/match-detection/features/bib/melinda-identifier-factory.js.map +1 -0
  34. package/dist/match-detection/features/bib/other-standard-identifier.js.map +1 -1
  35. package/dist/match-detection/features/bib/publication-time.js.map +1 -1
  36. package/dist/match-detection/features/bib/record-type.js.map +1 -1
  37. package/dist/match-detection/features/bib/standard-identifier-factory.js +8 -0
  38. package/dist/match-detection/features/bib/standard-identifier-factory.js.map +1 -1
  39. package/dist/match-detection/features/bib/title.js.map +1 -1
  40. package/dist/match-detection/features/index.js +2 -2
  41. package/dist/match-detection/index.js +10 -6
  42. package/dist/match-detection/index.js.map +1 -1
  43. package/dist/match-detection/index.spec.js +32 -25
  44. package/dist/match-detection/index.spec.js.map +1 -1
  45. package/dist/matching-utils.js +95 -0
  46. package/dist/matching-utils.js.map +1 -0
  47. package/package.json +26 -29
  48. package/src/candidate-search/candidate-search-utils.js +53 -0
  49. package/src/candidate-search/index.js +1 -1
  50. package/src/candidate-search/index.spec.js +64 -58
  51. package/src/candidate-search/query-list/bib.js +120 -19
  52. package/src/candidate-search/query-list/bib.spec.js +16 -14
  53. package/src/candidate-search/query-list/index.js +3 -1
  54. package/src/index.spec.js +46 -39
  55. package/src/match-detection/features/bib/all-source-ids.js +122 -0
  56. package/src/match-detection/features/bib/index.js +2 -0
  57. package/src/match-detection/features/bib/index.spec.js +32 -23
  58. package/src/match-detection/features/bib/language.js +37 -3
  59. package/src/match-detection/features/bib/melinda-id.js +34 -0
  60. package/src/match-detection/features/bib/melinda-identifier-factory.js +94 -0
  61. package/src/match-detection/features/bib/standard-identifier-factory.js +6 -0
  62. package/src/match-detection/index.js +7 -4
  63. package/src/match-detection/index.spec.js +27 -20
  64. package/src/matching-utils.js +86 -0
@@ -1,3 +1,4 @@
1
+ /* eslint-disable max-statements */
1
2
  /**
2
3
  *
3
4
  * @licstart The following is the entire license notice for the JavaScript code in this file.
@@ -26,34 +27,67 @@
26
27
  *
27
28
  */
28
29
 
30
+ import createDebugLogger from 'debug';
31
+
32
+ const debug = createDebugLogger('@natlibfi/melinda-record-matching:match-detection:features:language');
33
+ const debugData = debug.extend('data');
34
+
29
35
  export default () => ({
30
36
  name: 'Language',
31
37
  extract: record => {
32
38
  const value008 = get008Value();
33
39
  const values041 = get041Values();
40
+ debugData(`008: ${JSON.stringify(value008)}, 041: ${JSON.stringify(values041)}`);
34
41
 
35
42
  if (value008 && values041.length > 0) {
43
+ debugData(`There's both 008 and 041, searching for value in both`);
36
44
  const correspondingValue = values041.find(v => v === value008);
45
+ debugData(`Corresponding value: ${correspondingValue}`);
37
46
  return correspondingValue ? [correspondingValue] : [];
38
47
  }
39
48
 
49
+ if (!value008 && values041.length < 1) {
50
+ debugData(`No actual values found`);
51
+ return [];
52
+ }
53
+
40
54
  return value008 ? [value008] : [values041[0]];
41
55
 
42
56
  function get008Value() {
43
- const value = record.get(/^008$/u)?.[0]?.value || [];
57
+ const value = record.get(/^008$/u)?.[0]?.value || undefined;
58
+ debugData(`008 value: ${value}`);
59
+
60
+ if (!value) {
61
+ return undefined;
62
+ }
63
+
44
64
  const code = value.slice(35, 38);
45
- return code === '|||' ? undefined : code;
65
+ debugData(`008 code: ${code}`);
66
+ return code === '|||' || code === ' ' || code === '^^^' ? undefined : code;
46
67
  }
47
68
 
69
+ // Main language for the resource: in the first f041 $a or f041 $d
70
+ // Uses only f041s that have 2nd ind ' ', which means that the codes used are MARC 21 language codes
71
+
48
72
  function get041Values() {
49
73
  return record.get(/^041$/u)
74
+ .filter(({ind2}) => ind2 === ' ')
50
75
  .map(({subfields}) => subfields)
51
76
  .flat()
52
- .filter(({code}) => code === 'a')
77
+ .filter(({code}) => code === 'a' || code === 'd')
53
78
  .map(({value}) => value);
54
79
  }
55
80
  },
56
81
  compare: (a, b) => {
82
+ debugData(`Comparing ${JSON.stringify(a[0])} and ${JSON.stringify(b[0])}`);
83
+
84
+ if (a.length === 0 || b.length === 0) {
85
+ debugData(`No language to compare`);
86
+ return 0;
87
+ }
88
+
89
+ debugData(`There area languages to compare`);
90
+
57
91
  if (a[0] === b[0]) {
58
92
  return 0.1;
59
93
  }
@@ -0,0 +1,34 @@
1
+ /**
2
+ *
3
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
4
+ *
5
+ * Melinda record matching modules for Javascript
6
+ *
7
+ * Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)
8
+ *
9
+ * This file is part of melinda-record-matching-js
10
+ *
11
+ * melinda-record-matching-js program is free software: you can redistribute it and/or modify
12
+ * it under the terms of the GNU Lesser General Public License as
13
+ * published by the Free Software Foundation, either version 3 of the
14
+ * License, or (at your option) any later version.
15
+ *
16
+ * melinda-record-matching-js is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ * GNU Lesser General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU Affero General Public License
22
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
+ *
24
+ * @licend The above is the entire license notice
25
+ * for the JavaScript code in this file.
26
+ *
27
+ */
28
+
29
+ import createInterface from './melinda-identifier-factory';
30
+
31
+ export default () => {
32
+ const {extract, compare} = createInterface();
33
+ return {extract, compare, name: 'melinda-id'};
34
+ };
@@ -0,0 +1,94 @@
1
+ /**
2
+ *
3
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
4
+ *
5
+ * Melinda record matching modules for Javascript
6
+ *
7
+ * Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)
8
+ *
9
+ * This file is part of melinda-record-matching-js
10
+ *
11
+ * melinda-record-matching-js program is free software: you can redistribute it and/or modify
12
+ * it under the terms of the GNU Lesser General Public License as
13
+ * published by the Free Software Foundation, either version 3 of the
14
+ * License, or (at your option) any later version.
15
+ *
16
+ * melinda-record-matching-js is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ * GNU Lesser General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU Affero General Public License
22
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
+ *
24
+ * @licend The above is the entire license notice
25
+ * for the JavaScript code in this file.
26
+ *
27
+ */
28
+
29
+ import createDebugLogger from 'debug';
30
+ import {getMelindaIdsF035} from '../../../matching-utils';
31
+
32
+ // 003+001 FI-MELINDA <melinda-id>
33
+ // 035 $a (FI-MELINDA)<melinda-id>
34
+ // 035 $z (FI-MELINDA)<melinda-id>
35
+ // 035 $a FCC<melinda-id>
36
+ // 035 $z FCC<melinda-id>
37
+ // melinda-id = 001234567
38
+
39
+ export default () => {
40
+
41
+ const debug = createDebugLogger('@natlibfi/melinda-record-matching:match-detection:features/bib/melinda-id');
42
+ const debugData = debug.extend('data');
43
+
44
+ return {extract, compare};
45
+
46
+ function extract(record) {
47
+
48
+ const isMelindaRecord = record.get('003').some(f003 => f003.value === 'FI-MELINDA');
49
+ const [f001] = record.get('001').map(field => field.value);
50
+ const f035MelindaIds = getMelindaIdsF035(record);
51
+
52
+ if (
53
+ isMelindaRecord === undefined &&
54
+ f001 === undefined &&
55
+ f035MelindaIds.length < 1) {
56
+
57
+ debug(`No Melinda-IDs found`);
58
+ return {};
59
+ }
60
+
61
+ return {isMelindaRecord, f001, f035MelindaIds};
62
+
63
+ }
64
+
65
+ // eslint-disable-next-line max-statements
66
+ function compare(a, b) {
67
+
68
+ if (a.isMelindaRecord && b.isMelindaRecord &&
69
+ a.f001 === b.f001) {
70
+ debugData(`Melinda record's A f001 ${a.f001} matches Melinda record's B f001 ${a.f001}`);
71
+ return 1;
72
+ }
73
+
74
+ if (a.isMelindaRecord && typeof b.f035MelindaIds !== 'undefined' &&
75
+ b.f035MelindaIds.some(id => id === a.f001)) {
76
+ debugData(`Melinda record's A f001 ${a.f001} matches record B f035 ${JSON.stringify(b.f035MelindaIds)}`);
77
+ return 1;
78
+ }
79
+
80
+ if (b.isMelindaRecord && typeof a.f035MelindaIds !== 'undefined' &&
81
+ a.f035MelindaIds.some(id => id === b.f001)) {
82
+ debugData(`Melinda record's B f001 ${b.f001} matches record A f035 ${JSON.stringify(a.f035MelindaIds)}`);
83
+ return 1;
84
+ }
85
+
86
+ if (typeof a.f035MelindaIds !== 'undefined' && typeof b.f035MelindaIds !== 'undefined' &&
87
+ a.f035MelindaIds.some(idA => b.f035MelindaIds.some(idB => idB === idA))) {
88
+ debugData(`Record A f035 ${JSON.stringify(a.f035MelindaIds)} matches record B f035 ${JSON.stringify(b.f035MelindaIds)}`);
89
+ return 1;
90
+ }
91
+ debug(`No matching Melinda-IDs.`);
92
+ return 0;
93
+ }
94
+ };
@@ -26,6 +26,11 @@
26
26
  *
27
27
  */
28
28
 
29
+ import createDebugLogger from 'debug';
30
+
31
+ const debug = createDebugLogger('@natlibfi/melinda-record-matching:match-detection:features:standard-identifiers');
32
+ const debugData = debug.extend('data');
33
+
29
34
  export default ({pattern, subfieldCodes}) => {
30
35
  return {extract, compare};
31
36
 
@@ -43,6 +48,7 @@ export default ({pattern, subfieldCodes}) => {
43
48
 
44
49
  function compare(a, b) {
45
50
  if (a.length === 0 || b.length === 0) {
51
+ debugData(`No standardidentifiers to compare`);
46
52
  return 0;
47
53
  }
48
54
 
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-console */
1
2
  /**
2
3
  *
3
4
  * @licstart The following is the entire license notice for the JavaScript code in this file.
@@ -31,15 +32,17 @@ import * as features from './features';
31
32
 
32
33
  export {features};
33
34
 
35
+ // eslint-disable-next-line max-statements
34
36
  export default ({strategy, treshold = 0.9}) => (recordA, recordB) => {
35
37
  const minProbabilityQuantifier = 0.5;
36
38
 
37
39
  const debug = createDebugLogger('@natlibfi/melinda-record-matching:match-detection');
40
+ const debugData = debug.extend('data');
38
41
  const featuresA = extractFeatures(recordA);
39
42
  const featuresB = extractFeatures(recordB);
40
43
 
41
- debug(`Features (a): ${JSON.stringify(featuresA)}`);
42
- debug(`Features (b): ${JSON.stringify(featuresB)}`);
44
+ debugData(`Features (a): ${JSON.stringify(featuresA)}`);
45
+ debugData(`Features (b): ${JSON.stringify(featuresB)}`);
43
46
 
44
47
  const featurePairs = generateFeaturePairs();
45
48
  const similarityVector = generateSimilarityVector();
@@ -50,7 +53,7 @@ export default ({strategy, treshold = 0.9}) => (recordA, recordB) => {
50
53
  return {match: probability >= treshold, probability};
51
54
  }
52
55
 
53
- debug(`No feature yielded minimum probability amount of points (${minProbabilityQuantifier})`);
56
+ debugData(`No feature yielded minimum probability amount of points (${minProbabilityQuantifier})`);
54
57
  return {match: false, probability: 0.0};
55
58
 
56
59
  function calculateprobability() {
@@ -69,7 +72,7 @@ export default ({strategy, treshold = 0.9}) => (recordA, recordB) => {
69
72
  return {name, points};
70
73
  });
71
74
 
72
- debug(`Points: ${JSON.stringify(compared)}`);
75
+ debugData(`Points: ${JSON.stringify(compared)}`);
73
76
  return compared.map(({points}) => points);
74
77
  }
75
78
 
@@ -33,28 +33,35 @@ import {MarcRecord} from '@natlibfi/marc-record';
33
33
  import * as features from './features';
34
34
  import createDetectionInterface from '.';
35
35
 
36
- generateTests({
37
- path: [__dirname, '..', '..', 'test-fixtures', 'match-detection', 'index'],
38
- useMetadataFile: true,
39
- recurse: false,
40
- fixura: {
41
- reader: READERS.JSON
42
- },
43
- callback: ({getFixture, options, expectedResults}) => {
44
- const detect = createDetectionInterface(formatOptions());
45
- const recordA = new MarcRecord(getFixture('recordA.json'));
46
- const recordB = new MarcRecord(getFixture('recordB.json'));
47
- const results = detect(recordA, recordB);
36
+ describe('match-detection', () => {
37
+ generateTests({
38
+ path: [__dirname, '..', '..', 'test-fixtures', 'match-detection', 'index'],
39
+ useMetadataFile: true,
40
+ recurse: false,
41
+ fixura: {
42
+ reader: READERS.JSON
43
+ },
44
+ callback: ({getFixture, options, expectedResults, enabled = true}) => {
48
45
 
49
- expect(results).to.eql(expectedResults);
46
+ if (!enabled) {
47
+ return;
48
+ }
50
49
 
51
- function formatOptions() {
52
- const contextFeatures = features[options.strategy.type];
50
+ const detect = createDetectionInterface(formatOptions());
51
+ const recordA = new MarcRecord(getFixture('recordA.json'));
52
+ const recordB = new MarcRecord(getFixture('recordB.json'));
53
+ const results = detect(recordA, recordB);
53
54
 
54
- return {
55
- ...options,
56
- strategy: options.strategy.features.map(v => contextFeatures[v]())
57
- };
55
+ expect(results).to.eql(expectedResults);
56
+
57
+ function formatOptions() {
58
+ const contextFeatures = features[options.strategy.type];
59
+
60
+ return {
61
+ ...options,
62
+ strategy: options.strategy.features.map(v => contextFeatures[v]())
63
+ };
64
+ }
58
65
  }
59
- }
66
+ });
60
67
  });
@@ -0,0 +1,86 @@
1
+ /**
2
+ *
3
+ * @licstart The following is the entire license notice for the JavaScript code in this file.
4
+ *
5
+ * Melinda record matching modules for Javascript
6
+ *
7
+ * Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)
8
+ *
9
+ * This file is part of melinda-record-matching-js
10
+ *
11
+ * melinda-record-matching-js program is free software: you can redistribute it and/or modify
12
+ * it under the terms of the GNU Lesser General Public License as
13
+ * published by the Free Software Foundation, either version 3 of the
14
+ * License, or (at your option) any later version.
15
+ *
16
+ * melinda-record-matching-js is distributed in the hope that it will be useful,
17
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ * GNU Lesser General Public License for more details.
20
+ *
21
+ * You should have received a copy of the GNU Affero General Public License
22
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
+ *
24
+ * @licend The above is the entire license notice
25
+ * for the JavaScript code in this file.
26
+ *
27
+ */
28
+
29
+ import createDebugLogger from 'debug';
30
+
31
+ const debug = createDebugLogger('@natlibfi/melinda-record-matching:utils');
32
+ const debugData = debug.extend('data');
33
+
34
+ // eslint-disable-next-line max-statements
35
+ export function getMelindaIdsF035(record) {
36
+
37
+ const debug = createDebugLogger('@natlibfi/melinda-record-matching:melinda-id');
38
+ const debugData = debug.extend('data');
39
+
40
+ const f035s = record.getFields('035');
41
+
42
+ if (f035s.length < 1) {
43
+ debug(`No f035s found.`);
44
+ return [];
45
+ }
46
+
47
+ const allF035MelindaIds = [].concat(...f035s.map(toMelindaIds));
48
+ const f035MelindaIds = [...new Set(allF035MelindaIds)];
49
+
50
+ debugData(`Fields (${f035s.length}): ${JSON.stringify(f035s)}`);
51
+ debugData(`Ids (${allF035MelindaIds.length}): ${JSON.stringify(allF035MelindaIds)}`);
52
+ debugData(`Unique ids (${f035MelindaIds.length}): ${JSON.stringify(f035MelindaIds)}`);
53
+
54
+ return f035MelindaIds;
55
+
56
+ function toMelindaIds({subfields}) {
57
+ const melindaIdRegExp = /^(?<prefix>\(FI-MELINDA\)|FCC)(?<id>\d{9})$/u;
58
+
59
+ return subfields
60
+ .filter(sub => ['a', 'z'].includes(sub.code))
61
+ .filter(sub => melindaIdRegExp.test(sub.value))
62
+ .map(({value}) => value.replace(melindaIdRegExp, '$<id>'));
63
+
64
+ }
65
+ }
66
+
67
+ export function validateSidFieldSubfieldCounts(field) {
68
+ // Valid SID-fields have just one $c and one $b
69
+ debugData(`Validating SID field ${JSON.stringify(field)}`);
70
+ const countC = countSubfields(field, 'c');
71
+ const countB = countSubfields(field, 'b');
72
+ debug(`Found ${countC} sf $cs and ${countB} sf $bs. IsValid: ${countC === 1 && countB === 1}`);
73
+
74
+ return countC === 1 && countB === 1;
75
+ }
76
+
77
+ function countSubfields(field, subfieldCode) {
78
+ // debug(`Counting subfields ${subfieldCode}`);
79
+ return field.subfields.filter(({code}) => code === subfieldCode).length;
80
+ }
81
+
82
+ export function getSubfieldValues(field, subfieldCode) {
83
+ debugData(`Get subfield(s) $${subfieldCode} from ${JSON.stringify(field)}`);
84
+ return field.subfields.filter(({code}) => code === subfieldCode).map(({value}) => value);
85
+ }
86
+