-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase.cs
More file actions
400 lines (330 loc) · 11.4 KB
/
Base.cs
File metadata and controls
400 lines (330 loc) · 11.4 KB
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
/*
This code is provided "as is" and for educational or experimental purposes only. The author makes no warranties regarding the functionality, reliability, or safety of the code. It is not guaranteed to be production-ready, secure, or free from defects or bugs. Users should use this code at their own risk. The author disclaims any liability for damages resulting from using, modifying, or distributing this code. Before using in a production environment, thorough testing and validation are recommended.
*/
using System;
namespace ikatic.StringMetrics
{
public enum Metrics
{
Unknown = 0,
Hamming = 10,
Dice = 20,
Jaro = 30,
JaroWinkler = 40,
Soundex = 50,
Levenshtein = 60,
DamerauLevenshtein = 70
}
public interface IMetric
{
double Compare(string left, string right);
string Left { get; }
string Right { get; }
double Score { get; }
double Distance { get; }
IDiagnostics Diagnostics { get; }
}
public class IDiagnostics
{
/// <summary>
/// Elapsed milliseconds
/// </summary>
public double Milliseconds { get; set; }
}
public abstract class Metric : IMetric
{
private System.Diagnostics.Stopwatch _sw = null;
protected int lLength;
protected int rLength;
protected bool Init(string left, string right)
{
//start timing:
Start();
if (left == right)
{
Distance = MinScore;
Score = MaxScore;
return false;
}
if (left == null && right == null)
{
Distance = MinScore;
Score = MaxScore;
return false;
}
if (left == null)
{
Distance = right.Length;
Score = MinScore;
return false;
}
if (right == null)
{
Distance = left.Length;
Score = MinScore;
return false;
}
if (left == string.Empty && right == string.Empty)
{
Distance = MinScore;
Score = MinScore;
return false;
}
if (left == string.Empty)
{
Distance = right.Length;
Score = MinScore;
return false;
}
if (right == string.Empty)
{
Distance = left.Length;
Score = MinScore;
return false;
}
_left = left;
lLength = left.Length;
_right = right;
rLength = right.Length;
if (left.Equals(right))
{
Distance = MinScore;
Score = MaxScore; //exact match - no distance and perfect score:
return false;
}
if (lLength == 0)
{
//Missing pattern ; set distance to target's length and score to min score:
Distance = (double)rLength;
Score = MinScore;
return false;
}
if (rLength == 0)
{
//Missing target ; set distance to pattern's length and score to min score:
Distance = (double)lLength;
Score = MinScore;
return false;
}
//both strings are valid, set distance and score to min
//and return true so the strings get compared
_distance = MinScore;
_score = MinScore;
return true;
}
/// <summary>
/// (Void) Stops the timing and sets the private variable for ElapsedMilliseconds property;
/// </summary>
protected void Stop()
{
_sw.Stop();
_diagnostics.Milliseconds = _sw.Elapsed.TotalMilliseconds;
}
/// <summary>
/// (Void) Starts or resets the stopwatch and ElapsedMilliseconds property
/// </summary>
protected void Start()
{
_diagnostics.Milliseconds = double.MinValue;
_sw = System.Diagnostics.Stopwatch.StartNew();
}
#region "IMetric implementation:"
private string _left = null;
public string Left
{
get { return _left; }
internal set { _left = value; }
}
private string _right = null;
public string Right
{
get { return _right; }
protected set { _left = value; }
}
private double _distance = double.MinValue;
public double Distance
{
get { return _distance; }
internal set { _distance = value; }
}
private double _score = double.MinValue;
public double Score
{
get { return _score; }
internal set { _score = value; }
}
/// <summary>
///
/// </summary>
private IDiagnostics _diagnostics = new IDiagnostics();
public IDiagnostics Diagnostics
{
get { return _diagnostics; }
internal set { _diagnostics = value; }
}
/// <summary>
/// returns basic compare of two strings and returns 0d or 1d for no match and match respectivley.
/// derived classes must implement this method with their specific compare logic:
///</summary>
public virtual double Compare(string pattern, string target)
{
return (pattern == target ? 1d : 0d);
}
#endregion
#region "Min and Max scores:"
protected double MaxScore
{
get { return _maxScore; }
set { _maxScore = value; }
}
private double _maxScore = 1.0d;
protected double MinScore
{
get { return _minScore; }
set { _minScore = value; }
}
private double _minScore = 0.0d;
#endregion
#region "Helper and common functions/methods"
protected List<string> ToBigrams(string text, out List<string> bigrams)
{
//List<string> bigrams = new List<string>();
bigrams = new List<string>();
if (text != null && text.Length > 0)
{
for (int index = 0; index <= text.Length; index++)
{
if (index == 0)
bigrams.Add(String.Format("${0}", text[0]));
else if (index == text.Length)
bigrams.Add(String.Format("{0}^", text[index - 1]));
else
bigrams.Add(String.Format("{0}{1}", text[index - 1], text[index]));
}
}
return bigrams;
}
protected List<string> ToBigrams(string text)
{
List<string> bigrams = new List<string>();
if (text != null && text.Length > 0)
{
for (int index = 0; index <= text.Length; index++)
{
if (index == 0)
bigrams.Add(String.Format("${0}", text[0]));
else if (index == text.Length)
bigrams.Add(String.Format("{0}^", text[index - 1]));
else
bigrams.Add(String.Format("{0}{1}", text[index - 1], text[index]));
}
}
return bigrams;
}
protected List<string> ToTrigrams(string text)
{
List<string> trigrams = new List<string>();
if (text.Length == 0)
{
}
else if (text.Length == 1)
trigrams.Add(String.Format("${0}", text[0]));
else if (text.Length == 2)
trigrams.Add(String.Format("${0}{1}", text[0], text[1]));
else
{
for (int index = 1; index < text.Length - 1; index += 2)
{
if (index == 1)
{
trigrams.Add(String.Format("${0}{1}", text[index - 1], text[index]));
}
else if (index == text.Length - 2)
{
trigrams.Add(String.Format("{0}{1}^", text[index], text[index + 1]));
}
else
{
trigrams.Add(String.Format("{0}{1}{2}", text[index], text[index + 1], text[index + 2]));
}
}
}
return trigrams;
}
protected int MatchGrams(List<string> patterns, List<string> targets)
{
//return patterns.Intersect(targets).Count();
int matches = 0;
for (int p = 0; p < patterns.Count; p++)
{
for (int t = 0; t < targets.Count; t++)
{
if (patterns[p].Equals(targets[t]))
{
matches++;
break;
}
}
}
return matches;
//// Parallelize the outer loop to partion each comparison to targets:
//Parallel.For(0, patterns.Count, p =>
//{
// for (int t = 0; t < targets.Count; t++)
// {
// if (patterns[p].Equals(targets[t]))
// {
// matches++;
// break;
// }
// }
//}); // Parallel.For
//return matches;
}
protected int GetCommonPrefix(string pattern, string target, int max)
{
if (pattern == null || pattern.Length == 0)
return 0;
if (target == null || target.Length == 0)
return 0;
int returnValue = 0;
for (int index = 0; index < max; index++)
{
if (index < pattern.Length && index < target.Length)
{
if (pattern[index] == target[index])
{
returnValue++;
}
else
{
break;
}
}
}
return returnValue;
}
public static bool Equal(string a, string b)
{
if (a.Length != b.Length)
return false;
for (int i = 0; i < a.Length; i++)
{
if (!a[i].Equals(b[i]))
return false;
}
return true;
}
protected void Swap(ref string pattern, ref string target)
{
string temp = pattern;
pattern = target;
target = temp;
_left = pattern;
_right = target;
lLength = pattern.Length;
rLength = target.Length;
}
#endregion
}
}