@natlibfi/melinda-record-matching 2.0.1-alpha.1 → 2.1.0

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.
@@ -10,6 +10,8 @@ var _marcRecord = require("@natlibfi/marc-record");
10
10
 
11
11
  var _ = _interopRequireWildcard(require("."));
12
12
 
13
+ var _debug = _interopRequireDefault(require("debug"));
14
+
13
15
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
14
16
 
15
17
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
@@ -22,7 +24,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
22
24
  *
23
25
  * Melinda record matching modules for Javascript
24
26
  *
25
- * Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)
27
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
26
28
  *
27
29
  * This file is part of melinda-record-matching-js
28
30
  *
@@ -43,6 +45,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
43
45
  * for the JavaScript code in this file.
44
46
  *
45
47
  */
48
+ const debug = (0, _debug.default)('@natlibfi/melinda-record-matching:index:test');
49
+ const debugData = debug.extend('data');
46
50
  describe('INDEX', () => {
47
51
  (0, _fixugenHttpClient.default)({
48
52
  callback,
@@ -56,9 +60,12 @@ describe('INDEX', () => {
56
60
  async function callback({
57
61
  getFixture,
58
62
  options,
59
- enabled = true
63
+ enabled = true,
64
+ expectedMatchStatus,
65
+ expectedStopReason
60
66
  }) {
61
67
  if (!enabled) {
68
+ debug(`Disabled test!`);
62
69
  return;
63
70
  }
64
71
 
@@ -66,35 +73,54 @@ describe('INDEX', () => {
66
73
  subfieldValues: false
67
74
  });
68
75
  const expectedMatches = getFixture('expectedMatches.json');
76
+ const expectedNonMatches = getFixture('expectedNonMatches.json') || [];
69
77
  const match = (0, _.default)(formatOptions());
70
- const matches = await match(record);
71
- (0, _chai.expect)(formatResults()).to.eql(expectedMatches);
78
+ const {
79
+ matches,
80
+ matchStatus,
81
+ nonMatches
82
+ } = await match(record);
83
+ debugData(`${matches.length}, ${matchStatus.status}/${matchStatus.stopReason}, ${nonMatches ? nonMatches.length : nonMatches}`);
84
+ const formattedMatchResult = formatRecordResults(matches);
85
+ (0, _chai.expect)(formattedMatchResult).to.eql(expectedMatches);
86
+ const formattedNonMatchResult = formatRecordResults(nonMatches);
87
+ (0, _chai.expect)(formattedNonMatchResult).to.eql(expectedNonMatches);
88
+ (0, _chai.expect)(matchStatus.status).to.eql(expectedMatchStatus);
89
+ (0, _chai.expect)(matchStatus.stopReason).to.eql(expectedStopReason);
72
90
 
73
91
  function formatOptions() {
74
92
  const contextFeatures = _.matchDetection.features[options.detection.strategy.type];
75
93
  return { ...options,
76
- search: { ...options.search,
77
- maxRecordsPerRequest: 1
94
+ search: { ...options.search
78
95
  },
79
96
  detection: { ...options.detect,
80
97
  strategy: options.detection.strategy.features.map(v => contextFeatures[v]())
81
- },
82
- maxMatches: 2,
83
- maxCandidates: 1
98
+ }
84
99
  };
85
100
  }
86
101
 
87
- function formatResults() {
88
- return matches.map(({
89
- candidate,
90
- probability
91
- }) => ({
92
- probability,
93
- candidate: {
94
- id: candidate.id,
95
- record: candidate.record.toObject()
96
- }
97
- }));
102
+ function formatRecordResults(matches) {
103
+ if (matches) {
104
+ debugData(JSON.stringify(matches));
105
+ return matches.map(match => ({ ...match,
106
+ candidate: formatCandidate(match.candidate)
107
+ }));
108
+ }
109
+
110
+ return [];
111
+ } // Format candidate to remove validationOptions from record
112
+
113
+
114
+ function formatCandidate({
115
+ id,
116
+ record
117
+ }) {
118
+ const newId = id;
119
+ const newRecord = record;
120
+ return {
121
+ id: newId,
122
+ record: newRecord.toObject()
123
+ };
98
124
  }
99
125
  }
100
126
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.spec.js"],"names":["describe","callback","path","__dirname","recurse","fixura","reader","READERS","JSON","getFixture","options","enabled","record","MarcRecord","subfieldValues","expectedMatches","match","formatOptions","matches","formatResults","to","eql","contextFeatures","matchDetection","features","detection","strategy","type","search","maxRecordsPerRequest","detect","map","v","maxMatches","maxCandidates","candidate","probability","id","toObject"],"mappings":";;AA4BA;;AACA;;AACA;;AACA;;AACA;;;;;;;;AAhCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAQAA,QAAQ,CAAC,OAAD,EAAU,MAAM;AACtB,kCAAc;AACZC,IAAAA,QADY;AAEZC,IAAAA,IAAI,EAAE,CAACC,SAAD,EAAY,IAAZ,EAAkB,eAAlB,EAAmC,OAAnC,CAFM;AAGZC,IAAAA,OAAO,EAAE,KAHG;AAIZC,IAAAA,MAAM,EAAE;AACNC,MAAAA,MAAM,EAAEC,gBAAQC;AADV;AAJI,GAAd;;AASA,iBAAeP,QAAf,CAAwB;AAACQ,IAAAA,UAAD;AAAaC,IAAAA,OAAb;AAAsBC,IAAAA,OAAO,GAAG;AAAhC,GAAxB,EAA+D;AAE7D,QAAI,CAACA,OAAL,EAAc;A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,CAAP;AAOD;AACF;AACF,CApDO,CAAR","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {expect} from 'chai';\nimport {READERS} from '@natlibfi/fixura';\nimport generateTests from '@natlibfi/fixugen-http-client';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport createMatchInterface, {matchDetection} from '.';\n\ndescribe('INDEX', () => {\n generateTests({\n callback,\n path: [__dirname, '..', 'test-fixtures', 'index'],\n recurse: false,\n fixura: {\n reader: READERS.JSON\n }\n });\n\n async function callback({getFixture, options, enabled = true}) {\n\n if (!enabled) {\n return;\n }\n\n const record = new MarcRecord(getFixture('inputRecord.json'), {subfieldValues: false});\n const expectedMatches = getFixture('expectedMatches.json');\n\n const match = createMatchInterface(formatOptions());\n const matches = await match(record);\n\n expect(formatResults()).to.eql(expectedMatches);\n\n function formatOptions() {\n const contextFeatures = matchDetection.features[options.detection.strategy.type];\n\n return {\n ...options,\n search: {\n ...options.search,\n maxRecordsPerRequest: 1\n },\n detection: {\n ...options.detect,\n strategy: options.detection.strategy.features.map(v => contextFeatures[v]())\n },\n maxMatches: 2,\n maxCandidates: 1\n };\n }\n\n function formatResults() {\n return matches.map(({candidate, probability}) => ({\n probability,\n candidate: {\n id: candidate.id,\n record: candidate.record.toObject()\n }\n }));\n }\n }\n});\n"],"file":"index.spec.js"}
1
+ {"version":3,"sources":["../src/index.spec.js"],"names":["debug","debugData","extend","describe","callback","path","__dirname","recurse","fixura","reader","READERS","JSON","getFixture","options","enabled","expectedMatchStatus","expectedStopReason","record","MarcRecord","subfieldValues","expectedMatches","expectedNonMatches","match","formatOptions","matches","matchStatus","nonMatches","length","status","stopReason","formattedMatchResult","formatRecordResults","to","eql","formattedNonMatchResult","contextFeatures","matchDetection","features","detection","strategy","type","search","detect","map","v","stringify","candidate","formatCandidate","id","newId","newRecord","toObject"],"mappings":";;AA4BA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;;;AAjCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AASA,MAAMA,KAAK,GAAG,oBAAkB,8CAAlB,CAAd;AACA,MAAMC,SAAS,GAAGD,KAAK,CAACE,MAAN,CAAa,MAAb,CAAlB;AAEAC,QAAQ,CAAC,OAAD,EAAU,MAAM;AACtB,kCAAc;AACZC,IAAAA,QADY;AAEZC,IAAAA,IAAI,EAAE,CAACC,SAAD,EAAY,IAAZ,EAAkB,eAAlB,EAAmC,OAAnC,CAFM;AAGZC,IAAAA,OAAO,EAAE,KAHG;AAIZC,IAAAA,MAAM,EAAE;AACNC,MAAAA,MAAM,EAAEC,gBAAQC;AADV;AAJI,GAAd;;AASA,iBAAeP,QAAf,CAAwB;AAACQ,IAAAA,UAAD;AAAaC,IAAAA,OAAb;AAAsBC,IAAAA,OAAO,GAAG,IAAhC;AAAsCC,IAAAA,mBAAtC;AAA2DC,IAAAA;AAA3D,GAAxB,EAAwG;AAEtG,QAAI,CAACF,OAAL,EAAc;AACZd,MAAAA,KAAK,CAAE,gBAAF,CAAL;AACA;AACD;;AAED,UAAMiB,MAAM,GAAG,IAAIC,sBAAJ,CAAe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B,MADF;AAETH,UAAAA,QAAQ,EAAE1B,OAAO,CAACyB,SAAR,CAAkBC,QAAlB,CAA2BF,QAA3B,CAAoCM,GAApC,CAAwCC,CAAC,IAAIT,eAAe,CAACS,CAAD,CAAf,EAA7C;AAFD;AALN,OAAP;AAUD;;AAED,aAASb,mBAAT,CAA6BP,OAA7B,EAAsC;AACpC,UAAIA,OAAJ,EAAa;AACXvB,QAAAA,SAAS,CAACU,IAAI,CAACkC,SAAL,CAAerB,OAAf,CAAD,CAAT;AACA,eAAOA,OAAO,CAACmB,GAAR,CAAarB,KAAD,KAAY,EAC7B,GAAGA,KAD0B;AAE7BwB,UAAAA,SAAS,EAAEC,eAAe,CAACzB,KAAK,CAACwB,SAAP;AAFG,SAAZ,CAAZ,CAAP;AAID;;AACD,aAAO,EAAP;AACD,KAlDqG,CAoDtG;;;AACA,aAASC,eAAT,CAAyB;AAACC,MAAAA,EAAD;AAAK/B,MAAAA;AAAL,KAAzB,EAAuC;AACrC,YAAMgC,KAAK,GAAGD,EAAd;AACA,YAAME,SAAS,GAAGjC,MAAlB;AACA,aAAO;AACL+B,QAAAA,EAAE,EAAEC,KADC;AAELhC,QAAAA,MAAM,EAAEiC,SAAS,CAACC,QAAV;AAFH,OAAP;AAID;AAGF;AAEF,CA3EO,CAAR","sourcesContent":["/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport {expect} from 'chai';\nimport {READERS} from '@natlibfi/fixura';\nimport generateTests from '@natlibfi/fixugen-http-client';\nimport {MarcRecord} from '@natlibfi/marc-record';\nimport createMatchInterface, {matchDetection} from '.';\nimport createDebugLogger from 'debug';\n\nconst debug = createDebugLogger('@natlibfi/melinda-record-matching:index:test');\nconst debugData = debug.extend('data');\n\ndescribe('INDEX', () => {\n generateTests({\n callback,\n path: [__dirname, '..', 'test-fixtures', 'index'],\n recurse: false,\n fixura: {\n reader: READERS.JSON\n }\n });\n\n async function callback({getFixture, options, enabled = true, expectedMatchStatus, expectedStopReason}) {\n\n if (!enabled) {\n debug(`Disabled test!`);\n return;\n }\n\n const record = new MarcRecord(getFixture('inputRecord.json'), {subfieldValues: false});\n const expectedMatches = getFixture('expectedMatches.json');\n const expectedNonMatches = getFixture('expectedNonMatches.json') || [];\n\n\n const match = createMatchInterface(formatOptions());\n const {matches, matchStatus, nonMatches} = await match(record);\n debugData(`${matches.length}, ${matchStatus.status}/${matchStatus.stopReason}, ${nonMatches ? nonMatches.length : nonMatches}`);\n\n const formattedMatchResult = formatRecordResults(matches);\n expect(formattedMatchResult).to.eql(expectedMatches);\n\n const formattedNonMatchResult = formatRecordResults(nonMatches);\n expect(formattedNonMatchResult).to.eql(expectedNonMatches);\n\n expect(matchStatus.status).to.eql(expectedMatchStatus);\n expect(matchStatus.stopReason).to.eql(expectedStopReason);\n\n\n function formatOptions() {\n const contextFeatures = matchDetection.features[options.detection.strategy.type];\n\n return {\n ...options,\n search: {\n ...options.search\n },\n detection: {\n ...options.detect,\n strategy: options.detection.strategy.features.map(v => contextFeatures[v]())\n }\n };\n }\n\n function formatRecordResults(matches) {\n if (matches) {\n debugData(JSON.stringify(matches));\n return matches.map((match) => ({\n ...match,\n candidate: formatCandidate(match.candidate)\n }));\n }\n return [];\n }\n\n // Format candidate to remove validationOptions from record\n function formatCandidate({id, record}) {\n const newId = id;\n const newRecord = record;\n return {\n id: newId,\n record: newRecord.toObject()\n };\n }\n\n\n }\n\n});\n"],"file":"index.spec.js"}
@@ -50,10 +50,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
50
50
  var _default = ({
51
51
  strategy,
52
52
  treshold = 0.9
53
- }) => (recordA, recordB) => {
53
+ }, returnStrategy = false) => (recordA, recordB) => {
54
54
  const minProbabilityQuantifier = 0.5;
55
55
  const debug = (0, _debug.default)('@natlibfi/melinda-record-matching:match-detection');
56
56
  const debugData = debug.extend('data');
57
+ debugData(`Strategy: ${JSON.stringify(strategy)}`);
58
+ debugData(`Treshold: ${JSON.stringify(treshold)}`);
59
+ debugData(`ReturnStrategy: ${JSON.stringify(returnStrategy)}`);
57
60
  const featuresA = extractFeatures(recordA);
58
61
  const featuresB = extractFeatures(recordB);
59
62
  debugData(`Features (a): ${JSON.stringify(featuresA)}`);
@@ -64,17 +67,38 @@ var _default = ({
64
67
  if (similarityVector.some(v => v >= minProbabilityQuantifier)) {
65
68
  const probability = calculateprobability();
66
69
  debug(`probability: ${probability} (Treshold: ${treshold})`);
67
- return {
70
+ return returnResult({
68
71
  match: probability >= treshold,
69
72
  probability
70
- };
73
+ });
71
74
  }
72
75
 
73
76
  debugData(`No feature yielded minimum probability amount of points (${minProbabilityQuantifier})`);
74
- return {
77
+ return returnResult({
75
78
  match: false,
76
79
  probability: 0.0
77
- };
80
+ });
81
+
82
+ function returnResult(result) {
83
+ if (returnStrategy) {
84
+ debug(`Returning detection strategy with the result`);
85
+ const resultWithStrategy = {
86
+ match: result.match,
87
+ probability: result.probability,
88
+ strategy: formatStrategy(strategy),
89
+ treshold
90
+ };
91
+ debugData(`${JSON.stringify(resultWithStrategy)}`);
92
+ return resultWithStrategy;
93
+ }
94
+
95
+ return result;
96
+ }
97
+
98
+ function formatStrategy(strategy) {
99
+ const strategyNames = strategy.map(element => element.name);
100
+ return strategyNames || [];
101
+ }
78
102
 
79
103
  function calculateprobability() {
80
104
  const probability = similarityVector.reduce((acc, v) => acc + v, 0.0);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/match-detection/index.js"],"names":["strategy","treshold","recordA","recordB","minProbabilityQuantifier","debug","debugData","extend","featuresA","extractFeatures","featuresB","JSON","stringify","featurePairs","generateFeaturePairs","similarityVector","generateSimilarityVector","some","v","probability","calculateprobability","match","reduce","acc","record","name","extract","concat","value","compared","map","a","b","compare","find","featureName","points","pairs","generatePairs","missingFeatures","findMissing","index","filter","length"],"mappings":";;;;;;;AA6BA;;AACA;;;;;;;;;;AA9BA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAOA;eACe,CAAC;AAACA,EAAAA,QAAD;AAAWC,EAAAA,QAAQ,GAAG;AAAtB,CAAD,KAAgC,CAACC,OAAD,EAAUC,OAAV,KAAsB;AACnE,QAAMC,wBAAwB,GAAG,GAAjC;AAEA,QAAMC,KAAK,GAAG,oBAAkB,mDAAlB,CAAd;AACA,QAAMC,SAAS,GAAGD,KAAK,CAACE,MAAN,CAAa,MAAb,CAAlB;AACA,QAAMC,SAAS,GAAGC,eAAe,CAACP,OAAD,CAAjC;AACA,QAAMQ,SAAS,GAAGD,eAAe,CAACN,OAAD,CAAjC;AAEAG,EAAAA,SAAS,CAAE,iBAAgBK,IAAI,CAACC,SAAL,CAAeJ,SAAf,CAA0B,EAA5C,CAAT;AACAF,EAAAA,SAAS,CAAE,iBAAgBK,IAAI,CAACC,SAAL,CAAeF,SAAf,CAA0B,EAA5C,CAAT;AAEA,QAAMG,YAAY,GAAGC,oBAAoB,EAAzC;AACA,QAAMC,gBAAgB,GAAGC,wBAAwB,EAAjD;;AAEA,MAAID,gBAAgB,CAACE,IAAjB,CAAsBC,CAAC,IAAIA,CAAC,IAAId,wBAAhC,CAAJ,EAA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,KAAYA,IAAI,KAAKP,CAAhC,MAAuC,KAF/C,CAAP;AAGD;AACF;AACF,C","sourcesContent":["/* eslint-disable no-console */\n/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport createDebugLogger from 'debug';\nimport * as features from './features';\n\nexport {features};\n\n// eslint-disable-next-line max-statements\nexport default ({strategy, treshold = 0.9}) => (recordA, recordB) => {\n const minProbabilityQuantifier = 0.5;\n\n const debug = createDebugLogger('@natlibfi/melinda-record-matching:match-detection');\n const debugData = debug.extend('data');\n const featuresA = extractFeatures(recordA);\n const featuresB = extractFeatures(recordB);\n\n debugData(`Features (a): ${JSON.stringify(featuresA)}`);\n debugData(`Features (b): ${JSON.stringify(featuresB)}`);\n\n const featurePairs = generateFeaturePairs();\n const similarityVector = generateSimilarityVector();\n\n if (similarityVector.some(v => v >= minProbabilityQuantifier)) {\n const probability = calculateprobability();\n debug(`probability: ${probability} (Treshold: ${treshold})`);\n return {match: probability >= treshold, probability};\n }\n\n debugData(`No feature yielded minimum probability amount of points (${minProbabilityQuantifier})`);\n return {match: false, probability: 0.0};\n\n function calculateprobability() {\n const probability = similarityVector.reduce((acc, v) => acc + v, 0.0);\n return probability > 1.0 ? 1.0 : probability;\n }\n\n function extractFeatures(record) {\n return strategy.reduce((acc, {name, extract}) => acc.concat({name, value: extract(record)}), []);\n }\n\n function generateSimilarityVector() {\n const compared = featurePairs.map(({name, a, b}) => {\n const {compare} = strategy.find(({name: featureName}) => name === featureName);\n const points = compare(a, b);\n return {name, points};\n });\n\n debugData(`Points: ${JSON.stringify(compared)}`);\n return compared.map(({points}) => points);\n }\n\n function generateFeaturePairs() {\n const pairs = generatePairs();\n const missingFeatures = findMissing();\n\n debug(`Not comparing the following features because one, or both records are missing features: ${JSON.stringify(missingFeatures)}`);\n return pairs;\n\n function generatePairs() {\n return featuresA\n .reduce((acc, {name, value}, index) => acc.concat({\n name,\n a: value,\n b: featuresB[index].value\n }), [])\n .filter(({a, b}) => {\n if (a.length === 0 || b.length === 0) {\n return false;\n }\n\n return true;\n });\n }\n\n function findMissing() {\n return featuresA\n .map(({name}) => name)\n .filter(v => pairs.some(({name}) => name === v) === false);\n }\n }\n};\n"],"file":"index.js"}
1
+ {"version":3,"sources":["../../src/match-detection/index.js"],"names":["strategy","treshold","returnStrategy","recordA","recordB","minProbabilityQuantifier","debug","debugData","extend","JSON","stringify","featuresA","extractFeatures","featuresB","featurePairs","generateFeaturePairs","similarityVector","generateSimilarityVector","some","v","probability","calculateprobability","returnResult","match","result","resultWithStrategy","formatStrategy","strategyNames","map","element","name","reduce","acc","record","extract","concat","value","compared","a","b","compare","find","featureName","points","pairs","generatePairs","missingFeatures","findMissing","index","filter","length"],"mappings":";;;;;;;AA6BA;;AACA;;;;;;;;;;AA9BA;;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAOA;eACe,CAAC;AAACA,EAAAA,QAAD;AAAWC,EAAAA,QAAQ,GAAG;AAAtB,CAAD,EAA6BC,cAAc,GAAG,KAA9C,KAAwD,CAACC,OAAD,EAAUC,OAAV,KAAsB;AAC3F,QAAMC,wBA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Aa,CAAnC,EAAsC;AACpC,iBAAO,KAAP;AACD;;AAED,eAAO,IAAP;AACD,OAZI,CAAP;AAaD;;AAED,aAASH,WAAT,GAAuB;AACrB,aAAOpC,SAAS,CACbiB,GADI,CACA,CAAC;AAACE,QAAAA;AAAD,OAAD,KAAYA,IADZ,EAEJmB,MAFI,CAEG9B,CAAC,IAAIyB,KAAK,CAAC1B,IAAN,CAAW,CAAC;AAACY,QAAAA;AAAD,OAAD,KAAYA,IAAI,KAAKX,CAAhC,MAAuC,KAF/C,CAAP;AAGD;AACF;AACF,C","sourcesContent":["/* eslint-disable no-console */\n/**\n*\n* @licstart The following is the entire license notice for the JavaScript code in this file.\n*\n* Melinda record matching modules for Javascript\n*\n* Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)\n*\n* This file is part of melinda-record-matching-js\n*\n* melinda-record-matching-js program is free software: you can redistribute it and/or modify\n* it under the terms of the GNU Lesser General Public License as\n* published by the Free Software Foundation, either version 3 of the\n* License, or (at your option) any later version.\n*\n* melinda-record-matching-js is distributed in the hope that it will be useful,\n* but WITHOUT ANY WARRANTY; without even the implied warranty of\n* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n* GNU Lesser General Public License for more details.\n*\n* You should have received a copy of the GNU Affero General Public License\n* along with this program. If not, see <http://www.gnu.org/licenses/>.\n*\n* @licend The above is the entire license notice\n* for the JavaScript code in this file.\n*\n*/\n\nimport createDebugLogger from 'debug';\nimport * as features from './features';\n\nexport {features};\n\n// eslint-disable-next-line max-statements\nexport default ({strategy, treshold = 0.9}, returnStrategy = false) => (recordA, recordB) => {\n const minProbabilityQuantifier = 0.5;\n\n const debug = createDebugLogger('@natlibfi/melinda-record-matching:match-detection');\n const debugData = debug.extend('data');\n\n debugData(`Strategy: ${JSON.stringify(strategy)}`);\n debugData(`Treshold: ${JSON.stringify(treshold)}`);\n debugData(`ReturnStrategy: ${JSON.stringify(returnStrategy)}`);\n\n const featuresA = extractFeatures(recordA);\n const featuresB = extractFeatures(recordB);\n\n debugData(`Features (a): ${JSON.stringify(featuresA)}`);\n debugData(`Features (b): ${JSON.stringify(featuresB)}`);\n\n const featurePairs = generateFeaturePairs();\n const similarityVector = generateSimilarityVector();\n\n if (similarityVector.some(v => v >= minProbabilityQuantifier)) {\n const probability = calculateprobability();\n debug(`probability: ${probability} (Treshold: ${treshold})`);\n return returnResult({match: probability >= treshold, probability});\n }\n\n debugData(`No feature yielded minimum probability amount of points (${minProbabilityQuantifier})`);\n return returnResult({match: false, probability: 0.0});\n\n function returnResult(result) {\n if (returnStrategy) {\n debug(`Returning detection strategy with the result`);\n const resultWithStrategy = {match: result.match, probability: result.probability, strategy: formatStrategy(strategy), treshold};\n debugData(`${JSON.stringify(resultWithStrategy)}`);\n return resultWithStrategy;\n }\n return result;\n }\n\n function formatStrategy(strategy) {\n const strategyNames = strategy.map(element => element.name);\n return strategyNames || [];\n }\n\n function calculateprobability() {\n const probability = similarityVector.reduce((acc, v) => acc + v, 0.0);\n return probability > 1.0 ? 1.0 : probability;\n }\n\n function extractFeatures(record) {\n return strategy.reduce((acc, {name, extract}) => acc.concat({name, value: extract(record)}), []);\n }\n\n function generateSimilarityVector() {\n const compared = featurePairs.map(({name, a, b}) => {\n const {compare} = strategy.find(({name: featureName}) => name === featureName);\n const points = compare(a, b);\n return {name, points};\n });\n\n debugData(`Points: ${JSON.stringify(compared)}`);\n return compared.map(({points}) => points);\n }\n\n function generateFeaturePairs() {\n const pairs = generatePairs();\n const missingFeatures = findMissing();\n\n debug(`Not comparing the following features because one, or both records are missing features: ${JSON.stringify(missingFeatures)}`);\n return pairs;\n\n function generatePairs() {\n return featuresA\n .reduce((acc, {name, value}, index) => acc.concat({\n name,\n a: value,\n b: featuresB[index].value\n }), [])\n .filter(({a, b}) => {\n if (a.length === 0 || b.length === 0) {\n return false;\n }\n\n return true;\n });\n }\n\n function findMissing() {\n return featuresA\n .map(({name}) => name)\n .filter(v => pairs.some(({name}) => name === v) === false);\n }\n }\n};\n"],"file":"index.js"}
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "url": "git@github.com:natlibfi/melinda-record-matching-js.git"
15
15
  },
16
16
  "license": "LGPL-3.0+",
17
- "version": "2.0.1-alpha.1",
17
+ "version": "2.1.0",
18
18
  "main": "./dist/index.js",
19
19
  "engines": {
20
20
  "node": ">=14"
@@ -36,22 +36,22 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@natlibfi/marc-record": "^7.0.0",
39
- "@natlibfi/marc-record-serializers": "^8.0.4",
40
- "@natlibfi/sru-client": "^5.0.0-alpha.1",
39
+ "@natlibfi/marc-record-serializers": "^8.1.0",
40
+ "@natlibfi/sru-client": "^5.0.0",
41
41
  "debug": "^4.3.3",
42
42
  "moment": "^2.29.1",
43
- "natural": "^5.1.11",
43
+ "natural": "^5.1.13",
44
44
  "uuid": "^8.3.2",
45
- "winston": "^3.3.3"
45
+ "winston": "^3.3.4"
46
46
  },
47
47
  "devDependencies": {
48
- "@babel/cli": "^7.16.0",
49
- "@babel/core": "^7.16.0",
50
- "@babel/node": "^7.16.0",
51
- "@babel/preset-env": "^7.16.4",
52
- "@babel/register": "^7.16.0",
53
- "@babel/eslint-parser": "^7.16.3",
54
- "@natlibfi/eslint-config-melinda-backend": "^1.1.1",
48
+ "@babel/cli": "^7.16.7",
49
+ "@babel/core": "^7.16.7",
50
+ "@babel/eslint-parser": "^7.16.5",
51
+ "@babel/node": "^7.16.7",
52
+ "@babel/preset-env": "^7.16.7",
53
+ "@babel/register": "^7.16.7",
54
+ "@natlibfi/eslint-config-melinda-backend": "^2.0.0",
55
55
  "@natlibfi/fixugen": "^1.0.2",
56
56
  "@natlibfi/fixugen-http-client": "^1.1.3",
57
57
  "@natlibfi/fixura": "^2.2.1",
@@ -60,7 +60,7 @@
60
60
  "chai": "^4.3.4",
61
61
  "chai-as-promised": "^7.1.1",
62
62
  "cross-env": "^7.0.3",
63
- "eslint": "^7.32.0",
63
+ "eslint": "^8.6.0",
64
64
  "mocha": "^9.1.3",
65
65
  "nodemon": "^2.0.15",
66
66
  "nyc": "^15.1.0"
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * Melinda record matching modules for Javascript
6
6
  *
7
- * Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)
7
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
8
8
  *
9
9
  * This file is part of melinda-record-matching-js
10
10
  *
@@ -36,14 +36,29 @@ export {searchTypes} from './query-list';
36
36
 
37
37
  export class CandidateSearchError extends Error {}
38
38
 
39
- export default ({record, searchSpec, url, maxRecordsPerRequest = 50}) => {
39
+ // serverMaxResults : maximum size of total search result available from the server, defaults to Aleph's 20000
40
+
41
+ // eslint-disable-next-line max-statements
42
+ export default ({record, searchSpec, url, maxCandidates, maxRecordsPerRequest = 50, serverMaxResult = 20000}) => {
40
43
  MarcRecord.setValidationOptions({subfieldValues: false});
41
44
 
42
45
  const debug = createDebugLogger('@natlibfi/melinda-record-matching:candidate-search');
46
+ const debugData = debug.extend('data');
47
+
48
+ debugData(`SearchSpec: ${JSON.stringify(searchSpec)}`);
49
+ debugData(`Url: ${url}`);
50
+ debugData(`MaxRecordsPerRequest ${maxRecordsPerRequest}`);
51
+ debugData(`ServerMaxResult: ${serverMaxResult}`);
52
+ debugData(`MaxCandidates: ${maxCandidates}`);
53
+
54
+ // Do not retrieve more candidates than defined in maxCandidates
55
+ const adjustedMaxRecordsPerRequest = maxRecordsPerRequest >= maxCandidates ? maxCandidates : maxRecordsPerRequest;
56
+
43
57
  const inputRecordId = getRecordId(record);
44
58
  const queryList = generateQueryList(record, searchSpec);
45
59
  const client = createClient({
46
- url, maxRecordsPerRequest,
60
+ url,
61
+ maxRecordsPerRequest: adjustedMaxRecordsPerRequest,
47
62
  version: '2.0',
48
63
  retrieveAll: false
49
64
  });
@@ -54,27 +69,49 @@ export default ({record, searchSpec, url, maxRecordsPerRequest = 50}) => {
54
69
  throw new CandidateSearchError(`Generated query list contains no queries`);
55
70
  }
56
71
 
72
+ // state.totalRecords : amount of candidate records available to the current query (undefined, if there was no queries left)
73
+ // state.query : current query (undefined if there was no queries left)
74
+ // state.searchCounter : sequence for current search for current query (undefined, if there we no queries left)
75
+ // state.queryCandidateCounter: amount of candidate records retrieved from SRU for matching for current query, including the current record set (undefined if there were no queries left)
76
+ // state.queriesLeft : amount of queries left
77
+ // state.queryCounter : sequence for current query
78
+ // state.maxedQueries : queries that resulted in more than serverMaxResults hits
79
+
80
+
57
81
  // eslint-disable-next-line max-statements
58
- return async ({queryOffset = 0, resultSetOffset = 1}) => {
82
+ return async ({queryOffset = 0, resultSetOffset = 1, totalRecords = 0, searchCounter = 0, queryCandidateCounter = 0, queryCounter = 0, maxedQueries = []}) => {
59
83
  const query = queryList[queryOffset];
60
84
 
61
85
  if (query) {
62
- const {records, nextOffset} = await retrieveRecords();
86
+ const {records, nextOffset, total} = await retrieveRecords();
87
+
88
+ // If resultSetOffset === 1 this is the first search for the current query
89
+ debugData(`ResultSetOffset: ${resultSetOffset}`);
90
+ const newTotalRecords = resultSetOffset === 1 ? total : totalRecords;
91
+ const newQueryCounter = resultSetOffset === 1 ? queryCounter + 1 : queryCounter;
92
+ const newSearchCounter = resultSetOffset === 1 ? 1 : searchCounter + 1;
93
+ const newQueryCandidateCounter = resultSetOffset === 1 ? records.length : queryCandidateCounter + records.length;
94
+
95
+ const maxedQuery = resultSetOffset === 1 ? checkMaxedQuery(query, total, serverMaxResult) : undefined;
96
+ const newMaxedQueries = maxedQuery ? maxedQueries.concat(maxedQuery) : maxedQueries;
63
97
 
64
98
  if (typeof nextOffset === 'number') {
65
- debug(`Running next search for query ${queryOffset} ${query}`);
66
- return {records, queryOffset, resultSetOffset: nextOffset, queriesLeft: queryList.length - (queryOffset + 1)};
99
+ debug(`Next search will be for query ${queryOffset} ${query}, starting from record ${nextOffset}`);
100
+ return {records, queryOffset, resultSetOffset: nextOffset, queriesLeft: queryList.length - (queryOffset + 1), totalRecords: newTotalRecords, query, searchCounter: newSearchCounter, queryCandidateCounter: newQueryCandidateCounter, queryCounter: newQueryCounter, maxedQueries: newMaxedQueries};
67
101
  }
68
- debug(`Query ${queryOffset} ${query} done, moving to next query. (${queryList.length - (queryOffset + 1)} queries left)`);
69
- return {records, queryOffset: queryOffset + 1, queriesLeft: queryList.length - (queryOffset + 1)};
102
+ debug(`Query ${queryOffset} ${query} done.`);
103
+ debug(`There are (${queryList.length - (queryOffset + 1)} queries left)`);
104
+ return {records, queryOffset: queryOffset + 1, queriesLeft: queryList.length - (queryOffset + 1), totalRecords: newTotalRecords, query, searchCounter: newSearchCounter, queryCandidateCounter: newQueryCandidateCounter, queryCounter: newQueryCounter, maxedQueries: newMaxedQueries};
70
105
  }
71
106
 
72
107
  debug(`All ${queryList.length} queries done, there's no query for ${queryOffset}`);
73
- return {records: []};
108
+ return {records: [], queriesLeft: 0, queryCounter, maxedQueries};
74
109
 
75
110
  function retrieveRecords() {
76
111
  return new Promise((resolve, reject) => {
77
112
  const promises = [];
113
+ // eslint-disable-next-line functional/no-let
114
+ let totalRecords = 0;
78
115
 
79
116
  debug(`Searching for candidates with query: ${query} (Offset ${resultSetOffset})`);
80
117
 
@@ -88,6 +125,10 @@ export default ({record, searchSpec, url, maxRecordsPerRequest = 50}) => {
88
125
  debug(`SRU error for query: ${query}: ${err}`);
89
126
  reject(new CandidateSearchError(`SRU error for query: ${query}: ${err}`));
90
127
  })
128
+ .on('total', total => {
129
+ debug(`Got total: ${total}`);
130
+ totalRecords += total;
131
+ })
91
132
  .on('end', async nextOffset => {
92
133
  try {
93
134
  const records = await Promise.all(promises);
@@ -95,7 +136,7 @@ export default ({record, searchSpec, url, maxRecordsPerRequest = 50}) => {
95
136
 
96
137
  debug(`Found ${filtered.length} candidates`);
97
138
 
98
- resolve({nextOffset, records: filtered});
139
+ resolve({nextOffset, records: filtered, total: totalRecords});
99
140
  } catch (err) {
100
141
  reject(err);
101
142
  }
@@ -108,14 +149,6 @@ export default ({record, searchSpec, url, maxRecordsPerRequest = 50}) => {
108
149
  const foundRecordMarc = await MARCXML.from(foundRecord, {subfieldValues: false});
109
150
  const foundRecordId = getRecordId(foundRecordMarc);
110
151
 
111
- // This does not work and might cause problems:
112
- // Record *should* match itself AND in REST the input record is given id 000000001 always
113
- debug(`Checking ${inputRecordId} vs ${foundRecordId}`);
114
- if (inputRecordId === foundRecordId) {
115
- debug(`Input and candidate are the same record per 001. Discarding candidate`);
116
- return;
117
- }
118
-
119
152
  return {record: foundRecordMarc, id: foundRecordId};
120
153
  } catch (err) {
121
154
  throw new Error(`Failed converting record: ${err}, record: ${foundRecord}`);
@@ -126,6 +159,15 @@ export default ({record, searchSpec, url, maxRecordsPerRequest = 50}) => {
126
159
  }
127
160
  };
128
161
 
162
+ function checkMaxedQuery(query, total, serverMaxResult) {
163
+ // eslint-disable-next-line functional/no-conditional-statement
164
+ if (total >= serverMaxResult) {
165
+ debug(`WARNING: Query ${query} resulted in ${total} hits which meets the serverMaxResult (${serverMaxResult}) `);
166
+ return query;
167
+ }
168
+ }
169
+
170
+
129
171
  function getRecordId(record) {
130
172
  const [field] = record.get(/^001$/u);
131
173
  return field ? field.value : '';
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * Melinda record matching modules for Javascript
6
6
  *
7
- * Copyright (C) 2020 University Of Helsinki (The National Library Of Finland)
7
+ * Copyright (C) 2020-2022 University Of Helsinki (The National Library Of Finland)
8
8
  *
9
9
  * This file is part of melinda-record-matching-js
10
10
  *
@@ -31,6 +31,9 @@ import {READERS} from '@natlibfi/fixura';
31
31
  import generateTests from '@natlibfi/fixugen-http-client';
32
32
  import {MarcRecord} from '@natlibfi/marc-record';
33
33
  import createSearchInterface, {CandidateSearchError} from '.';
34
+ import createDebugLogger from 'debug';
35
+
36
+ const debug = createDebugLogger('@natlibfi/melinda-record-matching:candidate-search:test');
34
37
 
35
38
  describe('candidate-search', () => {
36
39
  generateTests({
@@ -43,7 +46,7 @@ describe('candidate-search', () => {
43
46
  });
44
47
 
45
48
  // eslint-disable-next-line max-statements
46
- async function callback({getFixture, factoryOptions, searchOptions, expectedFactoryError, expectedSearchError, enabled = true}) {
49
+ async function callback({getFixture, factoryOptions, searchOptions, expectedFactoryError = false, expectedSearchError = false, enabled = true}) {
47
50
  const url = 'http://foo.bar';
48
51
 
49
52
  if (!enabled) {
@@ -61,18 +64,20 @@ describe('candidate-search', () => {
61
64
  }
62
65
 
63
66
  const search = createSearchInterface({...formatFactoryOptions(), url});
64
- await iterate({searchOptions});
67
+ await iterate({searchOptions, expectedSearchError});
65
68
 
66
69
  function formatFactoryOptions() {
70
+ debug(`Using factoryOptions: ${JSON.stringify(factoryOptions)}`);
67
71
  return {
68
72
  ...factoryOptions,
69
- maxRecordsPerRequest: 1,
73
+ maxRecordsPerRequest: factoryOptions.maxRecordsPerRequest || 1,
74
+ maxServerResults: factoryOptions.maxServerResults || undefined,
70
75
  record: new MarcRecord(factoryOptions.record, {subfieldValues: false})
71
76
  };
72
77
  }
73
78
 
74
79
  // eslint-disable-next-line max-statements
75
- async function iterate({searchOptions, count = 1}) {
80
+ async function iterate({searchOptions, expectedSearchError, count = 1}) {
76
81
  const expectedResults = getFixture(`expectedResults${count}.json`);
77
82
 
78
83
  if (expectedSearchError) { // eslint-disable-line functional/no-conditional-statement
@@ -86,15 +91,10 @@ describe('candidate-search', () => {
86
91
  }
87
92
  }
88
93
 
89
- const results = await search(searchOptions);
90
-
91
- expect(formatResults(results)).to.eql(expectedResults);
92
-
93
- if (results.records.length > 0) {
94
- return iterate({
95
- searchOptions: resultsToOptions(results),
96
- count: count + 1
97
- });
94
+ // eslint-disable-next-line functional/no-conditional-statement
95
+ if (!expectedSearchError) {
96
+ const results = await search(searchOptions);
97
+ expect(formatResults(results)).to.eql(expectedResults);
98
98
  }
99
99
 
100
100
  function formatResults(results) {
@@ -105,11 +105,6 @@ describe('candidate-search', () => {
105
105
  };
106
106
  }
107
107
 
108
- function resultsToOptions(results) {
109
- return Object.entries(results)
110
- .filter(([k]) => k === 'records' === false) // If key is 'records' return false
111
- .reduce((acc, [k, v]) => ({...acc, [k]: v}), {});
112
- }
113
108
  }
114
109
  }
115
110
  });
@@ -175,12 +175,15 @@ export function bibHostComponents(record) {
175
175
 
176
176
  // SRU search dc.title with a search phrase starting with ^ maps currently in Melinda to
177
177
  // (probably) to *headings* index TIT
178
+ // - Aleph cannot currently handle headings searches starting with a boolean - in these cases use word search
179
+
178
180
  // Headings index TIT drops articles etc. from the start of the title according to the filing indicator
179
181
  // Currently filing indicator is not implemented - if the title starts with an article and the Melinda
180
182
  // record is correctly catalogued using a filing indicator -> dc.title search won't match
181
183
 
182
184
  export function bibTitle(record) {
183
185
  const title = getTitle();
186
+ const booleanStartWords = ['and', 'or', 'nor', 'not'];
184
187
 
185
188
  if (title) {
186
189
  const formatted = title
@@ -191,8 +194,10 @@ export function bibTitle(record) {
191
194
  .slice(0, 30)
192
195
  .trim();
193
196
 
197
+ // use word search for titles starting with a boolean
198
+ const useWordSearch = booleanStartWords.some(word => formatted.toLowerCase().startsWith(word));
194
199
  // Prevent too many matches by having a minimum length requirement
195
- return formatted.length >= 5 ? [`dc.title="^${formatted}*"`] : [];
200
+ return formatted.length >= 5 ? [`dc.title="${useWordSearch ? '' : '^'}${formatted}*"`] : [];
196
201
  }
197
202
 
198
203
  return [];