-
Notifications
You must be signed in to change notification settings - Fork 0
/
FuzzyLineMatcher.cs
330 lines (269 loc) · 9.21 KB
/
FuzzyLineMatcher.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace DiffPatch
{
public class MatchMatrix
{
public const int DefaultMaxOffset = 5;
private class MatchNodes
{
//score of this match (1.0 = perfect, 0.0 = no match)
public float score;
//sum of the match scores in the best path up to this node
public float sum;
//offset index of the next node in the path
public int next;
}
//contains match entries for consecutive characters of a pattern and the search text starting at line offset loc
private class StraightMatch
{
public readonly MatchNodes[] nodes;
public StraightMatch(int patternLength) {
nodes = new MatchNodes[patternLength];
for (int i = 0; i < patternLength; i++)
nodes[i] = new MatchNodes();
}
public void Update(int loc, IReadOnlyList<string> pattern, IReadOnlyList<string> search) {
for (int i = 0; i < pattern.Count; i++) {
int l = i + loc;
if (l < 0 || l >= search.Count)
nodes[i].score = 0;
else
nodes[i].score = FuzzyLineMatcher.MatchLines(pattern[i], search[l]);
}
}
}
private readonly IReadOnlyList<string> pattern;
private readonly IReadOnlyList<string> search;
//maximum offset between line matches in a run
private readonly int maxOffset;
//center line offset for this match matrix
private int loc;
//consecutive matches for pattern offset from loc by up to maxOffset
//first entry is for pattern starting at loc in text, last entry is offset +maxOffset
private readonly StraightMatch[] matches;
//offset index of first node in best path
private int firstNode;
public MatchMatrix(IReadOnlyList<string> pattern, IReadOnlyList<string> search, int maxOffset = DefaultMaxOffset) {
this.pattern = pattern;
this.search = search;
this.maxOffset = maxOffset;
matches = new StraightMatch[maxOffset + 1];
for (int i = 0; i <= maxOffset; i++)
matches[i] = new StraightMatch(pattern.Count);
}
public float Initialize(int loc) {
this.loc = loc;
for (int i = 0; i <= maxOffset; i++)
matches[i].Update(loc + i, pattern, search);
return Recalculate();
}
public bool CanStepForward => loc < search.Count - pattern.Count + maxOffset;
public bool CanStepBackward => loc > -maxOffset;
public float StepForward() {
if (!CanStepForward)
return 0;
loc++;
var reuse = matches[0];
for (int i = 1; i <= maxOffset; i++)
matches[i - 1] = matches[i];
matches[maxOffset] = reuse;
reuse.Update(loc + maxOffset, pattern, search);
return Recalculate();
}
public float StepBackward() {
if (!CanStepBackward)
return 0;
loc--;
var reuse = matches[maxOffset];
for (int i = maxOffset; i > 0; i--)
matches[i] = matches[i - 1];
matches[0] = reuse;
reuse.Update(loc, pattern, search);
return Recalculate();
}
//calculates the best path through the match matrix
//all paths must start with the first line of pattern matched to the line at loc (0 offset)
private float Recalculate() {
//tail nodes have sum = score
for (int j = 0; j <= maxOffset; j++) {
var node = matches[j].nodes[pattern.Count - 1];
node.sum = node.score;
node.next = -1;//no next
}
//calculate best paths for all nodes excluding head
for (int i = pattern.Count - 2; i >= 0; i--)
for (int j = 0; j <= maxOffset; j++) {
//for each node
var node = matches[j].nodes[i];
int maxk = -1;
float maxsum = 0;
for (int k = 0; k <= maxOffset; k++) {
int l = i + OffsetsToPatternDistance(j, k);
if (l >= pattern.Count) continue;
float sum = matches[k].nodes[l].sum;
if (k > j) sum -= 0.5f * (k - j); //penalty for skipping lines in search text
if (sum > maxsum) {
maxk = k;
maxsum = sum;
}
}
node.sum = maxsum + node.score;
node.next = maxk;
}
//find starting node
{
firstNode = 0;
float maxsum = matches[0].nodes[0].sum;
for (int k = 1; k <= maxOffset; k++) {
float sum = matches[k].nodes[0].sum;
if (sum > maxsum) {
firstNode = k;
maxsum = sum;
}
}
}
//return best path value
return matches[firstNode].nodes[0].sum / pattern.Count;
}
/// <summary>
/// Get the path of the current best match
/// </summary>
/// <returns>An array of corresponding line numbers in search text for each line in pattern</returns>
public int[] Path() {
var path = new int[pattern.Count];
int offset = firstNode; //offset of current node
var node = matches[firstNode].nodes[0];
path[0] = loc + offset;
int i = 0; //index in pattern of current node
while (node.next >= 0) {
int delta = OffsetsToPatternDistance(offset, node.next);
while (delta-- > 1) //skipped pattern lines
path[++i] = -1;
offset = node.next;
node = matches[offset].nodes[++i];
path[i] = loc + i + offset;
}
while (++i < path.Length)//trailing lines with no match
path[i] = -1;
return path;
}
public string Visualise() {
var path = Path();
var sb = new StringBuilder();
for (int j = 0; j <= maxOffset; j++) {
sb.Append(j).Append(':');
var line = matches[j];
for (int i = 0; i < pattern.Count; i++) {
bool inPath = path[i] > 0 && path[i] == loc + i + j;
sb.Append(inPath ? '[' : ' ');
int score = (int)Math.Round(line.nodes[i].score * 100);
sb.Append(score == 100 ? "%%" : score.ToString("D2"));
sb.Append(inPath ? ']' : ' ');
}
sb.AppendLine();
}
return sb.ToString();
}
//returns the pattern distance between two successive nodes in a path with offsets i and j
//if i == j, then the line offsets are the same and the pattern distance is 1 line
//if j > i, then the offset increased by j-i in successive pattern lines and the pattern distance is 1 line
//if j < i, then i-j patch lines were skipped between nodes, and the distance is 1+i-j
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int OffsetsToPatternDistance(int i, int j) => j >= i ? 1 : 1 + i - j;
}
public class FuzzyLineMatcher
{
public int maxOffset;
public FuzzyLineMatcher(int maxOffset = MatchMatrix.DefaultMaxOffset) {
this.maxOffset = maxOffset;
}
public void MatchLinesByWords(int[] matches, IReadOnlyList<string> wmLines1, IReadOnlyList<string> wmLines2) {
foreach (var (range1, range2) in LineMatching.UnmatchedRanges(matches, wmLines2.Count)) {
if (range1.length == 0 || range2.length == 0)
continue;
int[] match = Match(wmLines1.Slice(range1), wmLines2.Slice(range2));
for (int i = 0; i < match.Length; i++)
if (match[i] >= 0)
matches[range1.start + i] = range2.start + match[i];
}
}
public int[] Match(IReadOnlyList<string> pattern, IReadOnlyList<string> search) {
if (search.Count < pattern.Count) {
var rMatch = Match(search, pattern);
var nMatch = new int[pattern.Count];
for (int i = 0; i < nMatch.Length; i++)
nMatch[i] = -1;
for (int i = 0; i < rMatch.Length; i++)
if (rMatch[i] >= 0)
nMatch[rMatch[i]] = i;
return nMatch;
}
if (pattern.Count == 0)
return new int[0];
if (pattern.Count == search.Count)
return Enumerable.Range(0, pattern.Count).ToArray();
var mm = new MatchMatrix(pattern, search, maxOffset);
float bestScore = mm.Initialize(-maxOffset);
int[] bestMatch = mm.Path();
while (mm.CanStepForward) {
float score = mm.StepForward();
if (score > bestScore) {
bestScore = score;
bestMatch = mm.Path();
}
}
if (bestScore == 0)
return Enumerable.Range(0, pattern.Count).ToArray();
return bestMatch;
}
//assumes the lines are in word to char mode
//return 0.0 poor match to 1.0 perfect match
//uses LevenshtienDistance. A distance with half the maximum number of errors is considered a 0.0 scored match
public static float MatchLines(string s, string t) {
int d = LevenshteinDistance(s, t);
if (d == 0)
return 1f;//perfect match
float max = Math.Max(s.Length, t.Length) / 2f;
return Math.Max(0f, 1f - d / max);
}
//https://en.wikipedia.org/wiki/Levenshtein_distance
public static int LevenshteinDistance(string s, string t) {
// degenerate cases
if (s == t) return 0;
if (s.Length == 0) return t.Length;
if (t.Length == 0) return s.Length;
// create two work vectors of integer distances
//previous
int[] v0 = new int[t.Length + 1];
//current
int[] v1 = new int[t.Length + 1];
// initialize v1 (the current row of distances)
// this row is A[0][i]: edit distance for an empty s
// the distance is just the number of characters to delete from t
for (int i = 0; i < v1.Length; i++)
v1[i] = i;
for (int i = 0; i < s.Length; i++) {
// swap v1 to v0, reuse old v0 as new v1
var tmp = v0;
v0 = v1;
v1 = tmp;
// calculate v1 (current row distances) from the previous row v0
// first element of v1 is A[i+1][0]
// edit distance is delete (i+1) chars from s to match empty t
v1[0] = i + 1;
// use formula to fill in the rest of the row
for (int j = 0; j < t.Length; j++) {
int del = v0[j + 1] + 1;
int ins = v1[j] + 1;
int subs = v0[j] + (s[i] == t[j] ? 0 : 1);
v1[j + 1] = Math.Min(del, Math.Min(ins, subs));
}
}
return v1[t.Length];
}
}
}