-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathStringDistance.cs
712 lines (561 loc) · 22.7 KB
/
StringDistance.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
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
using System.Text.RegularExpressions;
using static Signum.Utilities.StringDistance;
namespace Signum.Utilities;
public class StringDistance
{
int[,]? _cachedNum;
public int LevenshteinDistance(string strOld, string strNew, IEqualityComparer<char>? comparer = null, Func<Choice<char>, int>? weight = null, bool allowTransposition = false)
{
return LevenshteinDistance<char>(strOld.ToCharArray(), strNew.ToCharArray(), comparer, weight, allowTransposition);
}
public int LevenshteinDistance<T>(T[] strOld, T[] strNew, IEqualityComparer<T>? comparer = null, Func<Choice<T>, int>? weight = null, bool allowTransposition = false)
{
int M1 = strOld.Length + 1;
int M2 = strNew.Length + 1;
if (comparer == null)
comparer = EqualityComparer<T>.Default;
if (weight == null)
weight = c => 1;
var num = ResizeArray(M1, M2);
num[0, 0] = 0;
for (int i = 1; i < M1; i++)
num[i, 0] = num[i - 1, 0] + weight(Choice<T>.Remove(strOld[i - 1]));
for (int j = 1; j < M2; j++)
num[0, j] = num[0, j - 1] + weight(Choice<T>.Add(strNew[j - 1]));
for (int i = 1; i < M1; i++)
{
for (int j = 1; j < M2; j++)
{
if (comparer.Equals(strOld[i - 1], strNew[j - 1]))
num[i, j] = num[i - 1, j - 1];
else
{
num[i, j] = Math.Min(Math.Min(
num[i - 1, j] + weight(Choice<T>.Remove(strOld[i - 1])),
num[i, j - 1] + weight(Choice<T>.Add(strNew[j - 1]))),
num[i - 1, j - 1] + weight(Choice<T>.Substitute(strOld[i - 1], strNew[j - 1])));
if (allowTransposition && i > 1 && j > 1 && comparer.Equals(strOld[i - 1], strNew[j - 2]) && comparer.Equals(strOld[i - 2], strNew[j - 1]))
num[i, j] = Math.Min(num[i, j], num[i - 2, j - 2] + weight(Choice<T>.Transpose(strOld[i - 1], strOld[i - 2])));
}
}
}
return num[M1 - 1, M2 - 1];
}
public enum ChoiceType
{
Equal,
Substitute,
Remove,
Add,
Transpose,
}
public struct Choice<T>
{
public readonly ChoiceType Type;
public readonly T Removed;
public readonly T Added;
public bool HasRemoved { get { return Type != ChoiceType.Add; } }
public bool HasAdded { get { return Type != ChoiceType.Remove; } }
internal Choice( ChoiceType type, T removed, T added)
{
this.Type = type;
this.Removed = removed;
this.Added = added;
}
public static Choice<T> Add(T value)
{
return new Choice<T>(ChoiceType.Add, default!, value);
}
public static Choice<T> Remove(T value)
{
return new Choice<T>(ChoiceType.Remove, value, default!);
}
public static Choice<T> Equal(T value)
{
return new Choice<T>(ChoiceType.Equal, value, value);
}
public static Choice<T> Substitute(T remove, T add)
{
return new Choice<T>(ChoiceType.Substitute, remove, add);
}
internal static Choice<T> Transpose(T remove, T add)
{
return new Choice<T>(ChoiceType.Transpose, remove, add);
}
public override string? ToString()
{
switch (Type)
{
case ChoiceType.Equal: return "{0}".FormatWith(Added);
case ChoiceType.Substitute: return "[-{0}+{1}]".FormatWith(Removed, Added);
case ChoiceType.Remove: return "-{0}".FormatWith(Removed);
case ChoiceType.Add: return "+{0}".FormatWith(Added);
default: return null;
}
}
}
public List<Choice<char>> LevenshteinChoices(string strOld, string strNew, IEqualityComparer<char>? comparer = null, Func<Choice<char>, int>? weight = null)
{
return LevenshteinChoices<char>(strOld.ToCharArray(), strNew.ToCharArray(), comparer, weight);
}
public List<Choice<T>> LevenshteinChoices<T>(T[] strOld, T[] strNew, IEqualityComparer<T>? comparer = null, Func<Choice<T>, int>? weight = null)
{
if (comparer == null)
comparer = EqualityComparer<T>.Default;
if (weight == null)
weight = (c) => 1;
this.LevenshteinDistance<T>(strOld, strNew, comparer, weight);
int i = strOld.Length;
int j = strNew.Length;
List<Choice<T>> result = new List<Choice<T>>();
while (i > 0 && j > 0)
{
if (comparer.Equals(strOld[i - 1], strNew[j - 1]))
{
result.Add(Choice<T>.Equal(strOld[i - 1]));
i = i - 1;
j = j - 1;
}
else
{
var cRemove = Choice<T>.Remove(strOld[i - 1]);
var cAdd = Choice<T>.Add(strNew[j - 1]);
var cSubstitute = Choice<T>.Substitute(strOld[i - 1], strNew[j - 1]);
var num = _cachedNum!;
var remove = num[i - 1, j] + weight(cRemove);
var add = num[i, j - 1] + weight(cAdd);
var substitute = num[i - 1, j - 1] + weight(cSubstitute);
var min = Math.Min(remove, Math.Min(add, substitute));
if (substitute == min)
{
result.Add(cSubstitute);
i = i - 1;
j = j - 1;
}
else if (remove == min)
{
result.Add(cRemove);
i = i - 1;
}
else if (add == min)
{
result.Add(cAdd);
j = j - 1;
}
}
}
while (i > 0)
{
result.Add(Choice<T>.Remove(strOld[i - 1]));
i = i - 1;
}
while (j > 0)
{
result.Add(Choice<T>.Add(strNew[j - 1]));
j = j - 1;
}
result.Reverse();
return result;
}
public int LongestCommonSubstring(string str1, string str2)
{
if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
return 0;
return LongestCommonSubstring<char>(str1.ToCharArray(), str2.ToCharArray(), out int pos1, out int pos2);
}
public int LongestCommonSubstring(string str1, string str2, out int startPos1, out int startPos2)
{
startPos1 = 0;
startPos2 = 0;
if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
return 0;
return LongestCommonSubstring<char>(str1.ToCharArray(), str2.ToCharArray(), out startPos1, out startPos2);
}
public int LongestCommonSubstring<T>(T[] str1, T[] str2, out int startPos1, out int startPos2, IEqualityComparer<T>? comparer = null)
{
if (str1 == null)
throw new ArgumentNullException("str1");
if (str2 == null)
throw new ArgumentNullException("str2");
return LongestCommonSubstring(new Slice<T>(str1), new Slice<T>(str2), out startPos1, out startPos2, comparer);
}
//http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring
public int LongestCommonSubstring<T>(Slice<T> str1, Slice<T> str2, out int startPos1, out int startPos2, IEqualityComparer<T>? comparer = null)
{
startPos1 = 0;
startPos2 = 0;
if (str1.Length == 0 || str2.Length == 0)
return 0;
if (comparer == null)
comparer = EqualityComparer<T>.Default;
var num = ResizeArray(str1.Length, str2.Length);
int maxlen = 0;
for (int i = 0; i < str1.Length; i++)
{
for (int j = 0; j < str2.Length; j++)
{
if (!comparer.Equals(str1[i], str2[j]))
num[i, j] = 0;
else
{
if ((i == 0) || (j == 0))
num[i, j] = 1;
else
num[i, j] = 1 + num[i - 1, j - 1];
if (num[i, j] > maxlen)
{
maxlen = num[i, j];
startPos1 = i - num[i, j] + 1;
startPos2 = j - num[i, j] + 1;
}
}
}
}
return maxlen;
}
string DebugTable()
{
if (_cachedNum == null)
throw new InvalidOperationException("Not initialized");
return _cachedNum.SelectArray(a => a.ToString()).FormatTable();
}
public int LongestCommonSubsequence(string str1, string str2)
{
if (string.IsNullOrEmpty(str1) || string.IsNullOrEmpty(str2))
return 0;
return LongestCommonSubsequence<char>(str1.ToCharArray(), str2.ToCharArray());
}
/// <summary>
/// ACE is a subsequence of ABCDE
/// </summary>
public int LongestCommonSubsequence<T>(T[] str1, T[] str2, IEqualityComparer<T>? comparer = null)
{
if (str1 == null)
throw new ArgumentNullException("str1");
if (str2 == null)
throw new ArgumentNullException("str2");
if(str1.Length == 0 || str2.Length == 0)
return 0;
if (comparer == null)
comparer = EqualityComparer<T>.Default;
int M1 = str1.Length + 1;
int M2 = str2.Length + 1;
var num = ResizeArray(M1, M2);
for (int i = 0; i < M1; i++)
num[i, 0] = 0;
for (int j = 0; j < M2; j++)
num[0, j] = 0;
for (int i = 1; i < M1; i++)
{
for (int j = 1; j < M2; j++)
{
if (comparer.Equals(str1[i - 1], str2[j - 1]))
num[i, j] = num[i - 1, j - 1] + 1;
else
{
if (num[i, j - 1] > num[i - 1, j])
num[i, j] = num[i, j - 1];
else
num[i, j] = num[i - 1, j];
}
}
}
return num[str1.Length, str2.Length];
}
public (int, string? result) SmithWatermanScoreWithResult(string text, string pattern, IEqualityComparer<char>? comparer = null)
{
var result = SmithWatermanScoreWithResult<char>(text.ToArray(), pattern.ToArray(), comparer);
return (result.distance, new string(result.result));
}
public (int distance, T[]? result) SmithWatermanScoreWithResult<T>(T[] text, T[] pattern, IEqualityComparer<T>? comparer = null)
{
if (comparer == null)
comparer = EqualityComparer<T>.Default;
var dist = SmithWatermanScore(text, pattern, comparer);
var result = FindBestMatchingSubstringSmithWaterman(text, pattern, comparer);
return (dist, result);
}
public int SmithWatermanScore(string text, string pattern, IEqualityComparer<char>? comparer = null)
{
return SmithWatermanScore(text.ToArray(), pattern.ToArray(), comparer);
}
public int SmithWatermanScore<T>(T[] text, T[] pattern, IEqualityComparer<T>? comparer = null)
{
if (text == null)
throw new ArgumentNullException("text");
if (pattern == null)
throw new ArgumentNullException("pattern");
if (text.Length == 0 || pattern.Length == 0)
return 0;
if (comparer == null)
comparer = EqualityComparer<T>.Default;
int M1 = text.Length + 1;
int M2 = pattern.Length + 1;
var num = ResizeArray(M1, M2);
int rows = text.Length + 1;
int cols = pattern.Length + 1;
_cachedNum = new int[rows, cols];
int maxScore = 0;
// Scoring parameters
int match = 2; // Score for a match
int mismatch = -1; // Penalty for a mismatch
int gap = -1; // Penalty for a gap
for (int i = 1; i < rows; i++)
{
for (int j = 1; j < cols; j++)
{
// Determine if characters match
int scoreMatch = comparer.Equals(text[i - 1], pattern[j - 1]) ? match : mismatch;
// Calculate scores based on possible options
int diag = _cachedNum[i - 1, j - 1] + scoreMatch; // Diagonal (match/mismatch)
int up = _cachedNum[i - 1, j] + gap; // Up (gap in text)
int left = _cachedNum[i, j - 1] + gap; // Left (gap in pattern)
int maxCellScore = Math.Max(0, Math.Max(diag, Math.Max(up, left)));
_cachedNum[i, j] = maxCellScore;
// Track the highest score for alignment
maxScore = Math.Max(maxScore, maxCellScore);
}
}
return maxScore;
}
T[]? FindBestMatchingSubstringSmithWaterman<T>(T[] text, T[] pattern, IEqualityComparer<T>? comparer = null)
{
if (text.Length == 0 || pattern.Length == 0 || _cachedNum == null)
return null;
if (text == null)
throw new ArgumentNullException("text");
if (pattern == null)
throw new ArgumentNullException("pattern");
if (comparer == null)
comparer = EqualityComparer<T>.Default;
// Find the cell with the maximum score
int maxRow = 0, maxCol = 0, maxScore = 0;
for (int i = 1; i < _cachedNum.GetLength(0); i++)
{
for (int j = 1; j < _cachedNum.GetLength(1); j++)
{
if (_cachedNum[i, j] > maxScore)
{
maxScore = _cachedNum[i, j];
maxRow = i;
maxCol = j;
}
}
}
// Backtrack to find the best alignment
List<T> result = new List<T>();
int row = maxRow, col = maxCol;
while (row > 0 && col > 0 && _cachedNum[row, col] > 0)
{
if (_cachedNum[row, col] == _cachedNum[row - 1, col - 1] +
(comparer.Equals(text[row - 1], pattern[col - 1]) ? 2 : -1))
{
// Diagonal (match/mismatch)
result.Insert(0, text[row - 1]);
row--;
col--;
}
else if (_cachedNum[row, col] == _cachedNum[row - 1, col] - 1)
{
// Gap in pattern
row--;
}
else
{
// Gap in text
col--;
}
}
return result.ToArray();
}
private int [,] ResizeArray(int M1, int M2)
{
if (_cachedNum == null || M1 > _cachedNum.GetLength(0) || M2 > _cachedNum.GetLength(1))
{
_cachedNum = new int[M1, M2];
}
return _cachedNum;
}
public enum DiffAction
{
Equal,
Added,
Removed
}
public struct DiffPair<T>
{
public DiffPair(DiffAction action, T value)
{
this.Action = action;
this.Value = value;
}
public readonly DiffAction Action;
public readonly T Value;
public override string ToString()
{
var str = Action == DiffAction.Added ? "+" :
Action == DiffAction.Removed ? "-" : "";
return str + Value;
}
}
public List<DiffPair<List<DiffPair<string>>>> DiffText(string? textOld, string? textNew, bool lineEndingDifferences = true)
{
textOld = textOld ?? "";
textNew = textNew ?? "";
var linesOld = lineEndingDifferences ? textOld.Split('\n') : textOld.Lines();
var linesNew = lineEndingDifferences ? textNew.Split('\n') : textNew.Lines();
List<DiffPair<string>> diff = this.Diff(linesOld, linesNew);
List<IGrouping<bool, DiffPair<string>>> groups = diff.GroupWhenChange(a => a.Action == DiffAction.Equal).ToList();
StringDistance sd = new StringDistance();
return groups.SelectMany(g=>
{
if (g.Key)
return g.Select(dp => new DiffPair<List<DiffPair<string>>>(DiffAction.Equal, new List<DiffPair<string>> { dp }));
var removed = g.Where(a=>a.Action == DiffAction.Removed).Select(a=>a.Value).ToArray();
var added = g.Where(a=>a.Action == DiffAction.Added).Select(a=>a.Value).ToArray();
var choices = this.LevenshteinChoices<string>(removed, added, weight: c =>
{
if (c.Type == ChoiceType.Add)
return c.Added.Length;
if (c.Type == ChoiceType.Remove)
return c.Removed.Length;
var distance = sd.LevenshteinDistance(c.Added, c.Removed);
return distance * 2;
});
return choices.Select(c =>
{
if (c.Type == ChoiceType.Add)
return new DiffPair<List<DiffPair<string>>>(DiffAction.Added, new List<DiffPair<string>> { new DiffPair<string>(DiffAction.Added, c.Added) });
if (c.Type == ChoiceType.Remove)
return new DiffPair<List<DiffPair<string>>>(DiffAction.Removed, new List<DiffPair<string>> { new DiffPair<string>(DiffAction.Removed, c.Removed) });
var diffWords = sd.DiffWords(c.Removed, c.Added);
return new DiffPair<List<DiffPair<string>>>(DiffAction.Equal, diffWords);
});
}).ToList();
}
static readonly Regex WordsRegex = new Regex(@"([\w\d]+|\s+|.)");
public List<DiffPair<string>> DiffWords(string strOld, string strNew)
{
var wordsOld = WordsRegex.Matches(strOld).Cast<Match>().Select(m => m.Value).ToArray();
var wordsNew = WordsRegex.Matches(strNew).Cast<Match>().Select(m => m.Value).ToArray();
return Diff(wordsOld, wordsNew);
}
public List<DiffPair<T>> Diff<T>(T[] strOld, T[] strNew, IEqualityComparer<T>? comparer = null)
{
var result = new List<DiffPair<T>>();
DiffPrivate<T>(new Slice<T>(strOld), new Slice<T>(strNew), comparer, result);
return result;
}
void DiffPrivate<T>(Slice<T> sliceOld, Slice<T> sliceNew, IEqualityComparer<T>? comparer, List<DiffPair<T>> result)
{
int length = LongestCommonSubstring<T>(sliceOld, sliceNew, out int posOld, out int posNew, comparer);
if (length == 0)
{
AddResults(result, sliceOld, DiffAction.Removed);
AddResults(result, sliceNew, DiffAction.Added);
}
else
{
TryDiff(sliceOld.SubSliceStart(posOld), sliceNew.SubSliceStart(posNew), comparer, result);
AddResults(result, sliceOld.SubSlice(posOld, length), DiffAction.Equal);
TryDiff(sliceOld.SubSliceEnd(posOld + length), sliceNew.SubSliceEnd(posNew + length), comparer, result);
}
}
static void AddResults<T>(List<DiffPair<T>> list, Slice<T> slice, DiffAction action)
{
for (int i = 0; i < slice.Length; i++)
list.Add(new DiffPair<T>(action, slice[i]));
}
void TryDiff<T>(Slice<T> sliceOld, Slice<T> sliceNew, IEqualityComparer<T>? comparer, List<DiffPair<T>> result)
{
if (sliceOld.Length > 0 && sliceOld.Length > 0)
{
DiffPrivate(sliceOld, sliceNew, comparer, result);
}
else if (sliceOld.Length > 0)
{
AddResults(result, sliceOld, DiffAction.Removed);
}
else if (sliceNew.Length > 0)
{
AddResults(result, sliceNew, DiffAction.Added);
}
}
}
public static class StringDistanceExtensions
{
public static string NiceToString(this List<DiffPair<List<DiffPair<string>>>> diff)
{
return diff.ToString(b =>
{
if (b.Action == DiffAction.Equal)
{
if (b.Value.Count == 1)
return " " + b.Value.Single().Value;
return
"-- " + b.Value.Where(a => a.Action == DiffAction.Removed || a.Action == DiffAction.Equal).ToString(a => a.Value, "") + "\n" +
"++ " + b.Value.Where(a => a.Action == DiffAction.Added || a.Action == DiffAction.Equal).ToString(a => a.Value, "");
}
else if (b.Action == DiffAction.Removed)
return "-- " + b.Value.Single().Value;
else if (b.Action == DiffAction.Added)
return "++ " + b.Value.Single().Value;
else
throw new UnexpectedValueException(b.Action);
}, "\n");
}
}
public struct Slice<T> :IEnumerable<T>
{
public Slice(T[] array) : this(array, 0, array.Length) { }
public Slice(T[] array, int offset, int length)
{
if (offset + length > array.Length)
throw new ArgumentException("Invalid slice");
this.Array = array;
this.Offset = offset;
this.Length = length;
}
public readonly T[] Array;
public readonly int Offset;
public readonly int Length;
public T this[int index]
{
get
{
if (index > Length)
throw new IndexOutOfRangeException();
return Array[Offset + index];
}
set
{
if (index > Length)
throw new IndexOutOfRangeException();
Array[Offset + index] = value;
}
}
public Slice<T> SubSlice(int relativeIndex, int length)
{
return new Slice<T>(this.Array, this.Offset + relativeIndex, length);
}
public Slice<T> SubSliceStart(int relativeIndex)
{
return new Slice<T>(this.Array, this.Offset, relativeIndex);
}
public Slice<T> SubSliceEnd(int relativeIndex)
{
return new Slice<T>(this.Array, this.Offset + relativeIndex, this.Length - relativeIndex);
}
public override string ToString()
{
return this.Array.Skip(Offset).Take(Length).ToString("");
}
public IEnumerator<T> GetEnumerator()
{
return this.Array.Skip(Offset).Take(Length).GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}