-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmatching.ts
272 lines (244 loc) · 12 KB
/
matching.ts
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
/*
* Portions of this software was developed by employees of the National Institute
* of Standards and Technology (NIST), an agency of the Federal Government and is
* being made available as a public service. Pursuant to title 17 United States
* Code Section 105, works of NIST employees are not subject to copyright
* protection in the United States. This software may be subject to foreign
* copyright. Permission in the United States and in foreign countries, to the
* extent that NIST may hold copyright, to use, copy, modify, create derivative
* works, and distribute this software and its documentation without fee is hereby
* granted on a non-exclusive basis, provided that this notice and disclaimer
* of warranty appears in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/
import { TrackedArray, TrackedElement, TrackedObject, TrackedPrimitive } from './utils/tracked';
import { JSONObject, countSubElements, getPropertyIntersection } from './utils/json';
import { ArraySubElement, ComparisonResult, LeftArrayItem, RightArrayItem } from './results';
import stringSimilarity from './utils/string-similarity';
import { computeWithUnmatchedElements } from './utils/hungarian';
type ComparatorFunc = (left: TrackedElement, right: TrackedElement, shallow?: boolean) => ComparisonResult;
export type MatcherResults = [LeftArrayItem[], RightArrayItem[], ArraySubElement[], number];
export type Matcher = (left: TrackedElement[], right: TrackedElement[], compareFunc: ComparatorFunc) => MatcherResults;
export type MatcherGenerator = (left: TrackedElement[], right: TrackedElement[]) => Matcher[];
export function scoringMatcherFactory(
scorers: ((left: TrackedElement, right: TrackedElement) => number)[],
minConfidence = 0.7,
): Matcher[] {
return scorers.map(
(scorer) =>
(left: TrackedElement[], right: TrackedElement[], compareFunc: ComparatorFunc): MatcherResults => {
const matchedIndices: [number, number][] = [];
const unmatchedLeftIndices: number[] = [];
const rightArrayIndices = [...right.keys()];
left.forEach((leftElement, leftIndex) => {
const [topScoreIndex] = rightArrayIndices.reduce(
(prev, rightIndex) => {
const score = scorer(leftElement, right[rightIndex]);
return score > prev[1] && score >= minConfidence ? [rightIndex, score] : prev;
},
[-1, 0],
);
if (topScoreIndex === -1) {
unmatchedLeftIndices.push(leftIndex);
} else {
rightArrayIndices.splice(rightArrayIndices.indexOf(topScoreIndex), 1);
matchedIndices.push([leftIndex, topScoreIndex]);
}
});
let totalScore = 0;
return [
unmatchedLeftIndices.map((leftIndex) => {
const item = left[leftIndex];
totalScore += countSubElements(item.raw);
return {
leftPointer: item.pointer,
leftElement: item.raw,
};
}),
rightArrayIndices.map((rightIndex) => {
const item = right[rightIndex];
totalScore += countSubElements(item.raw);
return {
rightPointer: item.pointer,
rightElement: item.raw,
};
}),
matchedIndices.flatMap(([leftIndex, rightIndex]) => {
const leftElement = left[leftIndex];
const rightElement = right[rightIndex];
const [changes, score] = compareFunc(leftElement, rightElement);
totalScore += score;
return {
leftPointer: leftElement.pointer,
rightPointer: rightElement.pointer,
changes,
score,
};
}),
totalScore,
];
},
);
}
export default abstract class MatcherContainer {
abstract generate(left: TrackedElement[], right: TrackedElement[]): Matcher[];
public static fromDict(dict: JSONObject): MatcherContainer {
if ('type' in dict && typeof dict.type === 'string') {
switch (dict.type) {
case ObjectPropertyMatcherContainer.name:
return ObjectPropertyMatcherContainer.fromDict(dict);
case OptimalMatcherContainer.name:
return OptimalMatcherContainer.fromDict(dict);
case HungarianMatcherContainer.name:
return HungarianMatcherContainer.fromDict(dict);
default:
throw new Error('Error deserializing matcher container: unknown type');
}
}
throw new Error('Error deserializing matcher container: type must be specified');
}
}
export class ObjectPropertyMatcherContainer implements MatcherContainer {
private property: string;
private method: string;
generate(_: TrackedElement[], __: TrackedElement[]): Matcher[] {
return scoringMatcherFactory([
(left: TrackedElement, right: TrackedElement) => {
if (
left instanceof TrackedObject &&
right instanceof TrackedObject &&
this.property in left.raw &&
this.property in right.raw
) {
return left.raw[this.property] === right.raw[this.property] ? 1 : 0;
}
return 0;
},
]);
}
constructor(property: string, method = 'absolute') {
this.property = property;
this.method = method;
}
static fromDict(dict: JSONObject): MatcherContainer {
if ('property' in dict && typeof dict.property === 'string') {
return new ObjectPropertyMatcherContainer(dict.property);
}
throw new Error('Error deserializing ObjectPropertyMatchContainer');
}
}
export class OptimalMatcherContainer implements MatcherContainer {
// TODO: add ignore options, options for string-similarity
generate(left: TrackedElement[], right: TrackedElement[]): Matcher[] {
if (left.length === 0 || right.length === 0) {
return [];
}
const lSample = left[0];
const rSample = right[0];
if (lSample instanceof TrackedArray && rSample instanceof TrackedArray) {
throw new Error('Array of array comparison is not supported');
} else if (lSample instanceof TrackedObject && rSample instanceof TrackedObject) {
return scoringMatcherFactory(
getPropertyIntersection(lSample.raw, rSample.raw)
.map((prop) =>
typeof lSample.raw === 'string'
? [
(left: TrackedElement, right: TrackedElement) => {
if (!(left instanceof TrackedObject && right instanceof TrackedObject)) {
throw new Error('Non-homogenous array of items cannot be compared');
}
if (typeof left.raw !== 'string' || typeof right.raw !== 'string') {
throw new Error('Cannot mix string and non string types in comparison');
}
return stringSimilarity(left.raw[prop], right.raw[prop], 'absolute', true);
},
]
: [
(left: TrackedElement, right: TrackedElement) => {
if (!(left instanceof TrackedObject && right instanceof TrackedObject)) {
throw new Error('Non-homogenous array of items cannot be compared');
}
return left.raw[prop] === right.raw[prop] ? 1 : 0;
},
],
)
.flat(),
);
} else if (lSample instanceof TrackedPrimitive && rSample instanceof TrackedPrimitive) {
return scoringMatcherFactory([(left, right) => (left.raw === right.raw ? 1 : 0)]);
} else {
throw new Error('Mismatched types are not supported');
}
}
static fromDict(_: JSONObject): MatcherContainer {
return new OptimalMatcherContainer();
}
}
export const hungarianMatcher: Matcher = (left, right, compareFunc) => {
// the most expensive part, score each possible element pair and throw it into a matrix
const cost = left.map((l) => {
return right.map((r) => {
// return compareFunc(l, r)[1];
return compareFunc(l, r)[1] / (countSubElements(l.raw) + countSubElements(r.raw));
});
});
const lUnmatchedCost = left.map((_) => 1);
const rUnmatchedCost = right.map((_) => 1);
// const lUnmatchedCost = left.map((l) => countSubElements(l.raw));
// const rUnmatchedCost = right.map((r) => countSubElements(r.raw));
const [matchedPairs, lUnmatched, rUnmatched] = computeWithUnmatchedElements(cost, lUnmatchedCost, rUnmatchedCost);
let totalCost = 0;
return [
lUnmatched.map((lIndex) => {
const element = left[lIndex];
// totalCost += lUnmatchedCost[lIndex];
totalCost += countSubElements(left[lIndex].raw);
return {
leftPointer: element.pointer,
leftElement: element.raw,
};
}),
rUnmatched.map((rIndex) => {
const element = right[rIndex];
// totalCost += rUnmatchedCost[rIndex];
totalCost += countSubElements(right[rIndex].raw);
return {
rightPointer: element.pointer,
rightElement: element.raw,
};
}),
matchedPairs.map(([lIndex, rIndex]) => {
const leftElement = left[lIndex];
const rightElement = right[rIndex];
const [changes, score] = compareFunc(leftElement, rightElement);
totalCost += score;
return {
leftPointer: leftElement.pointer,
rightPointer: rightElement.pointer,
changes,
score,
};
}),
totalCost,
];
};
export class HungarianMatcherContainer implements MatcherContainer {
generate(_: TrackedElement[], __: TrackedElement[]): Matcher[] {
return [hungarianMatcher];
}
static fromDict(_: JSONObject): MatcherContainer {
return new HungarianMatcherContainer();
}
}