forked from feature23/StringSimilarity.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJaroWinkler.cs
More file actions
232 lines (216 loc) · 9.04 KB
/
JaroWinkler.cs
File metadata and controls
232 lines (216 loc) · 9.04 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
/*
* The MIT License
*
* Copyright 2016 feature[23]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Data.SqlTypes;
using System.Linq;
using F23.StringSimilarity.Interfaces;
// ReSharper disable SuggestVarOrType_Elsewhere
// ReSharper disable LoopCanBeConvertedToQuery
namespace F23.StringSimilarity
{
/// <summary>
/// The Jaro–Winkler distance metric is designed and best suited for short
/// strings such as person names, and to detect typos; it is (roughly) a
/// variation of Damerau-Levenshtein, where the substitution of 2 close
/// characters is considered less important then the substitution of 2 characters
/// that a far from each other.
/// Jaro-Winkler was developed in the area of record linkage (duplicate
/// detection) (Winkler, 1990). It returns a value in the interval [0.0, 1.0].
/// The distance is computed as 1 - Jaro-Winkler similarity.
/// </summary>
public class JaroWinkler : INormalizedStringSimilarity, INormalizedStringDistance, INormalizedSpanSimilarity, INormalizedSpanDistance
{
private const double DEFAULT_THRESHOLD = 0.7;
private const int THREE = 3;
private const double JW_COEF = 0.1;
/// <summary>
/// The current value of the threshold used for adding the Winkler bonus. The default value is 0.7.
/// </summary>
private double Threshold { get; }
/// <summary>
/// Creates a new instance with default threshold (0.7)
/// </summary>
public JaroWinkler()
{
Threshold = DEFAULT_THRESHOLD;
}
/// <summary>
/// Creates a new instance with given threshold to determine when Winkler bonus should
/// be used. Set threshold to a negative value to get the Jaro distance.
/// </summary>
/// <param name="threshold"></param>
public JaroWinkler(double threshold)
{
Threshold = threshold;
}
/// <summary>
/// Compute Jaro-Winkler similarity.
/// </summary>
/// <param name="s1">The first string to compare.</param>
/// <param name="s2">The second string to compare.</param>
/// <returns>The Jaro-Winkler similarity in the range [0, 1]</returns>
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
public double Similarity(string s1, string s2)
=> Similarity(s1.AsSpan(), s2.AsSpan());
/// <summary>
/// Calculates the similarity between two sequences using the Jaro-Winkler distance metric.
/// </summary>
/// <remarks>The similarity is calculated using the Jaro-Winkler distance, which is a measure of
/// similarity between two sequences. The result is adjusted based on common prefixes to give higher scores to
/// sequences that share a common prefix.</remarks>
/// <typeparam name="T">The type of elements in the sequences. Must implement <see cref="IEquatable{T}"/>.</typeparam>
/// <param name="s1">The first sequence to compare. Cannot be null.</param>
/// <param name="s2">The second sequence to compare. Cannot be null.</param>
/// <returns>A value between 0 and 1 representing the similarity between the two sequences, where 1 indicates identical
/// sequences and 0 indicates no similarity.</returns>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="s1"/> or <paramref name="s2"/> is null.</exception>
public double Similarity<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
if (s1 == null)
{
throw new ArgumentNullException(nameof(s1));
}
if (s2 == null)
{
throw new ArgumentNullException(nameof(s2));
}
if (s1.SequenceEqual(s2))
{
return 1f;
}
int[] mtp = Matches(s1, s2);
float m = mtp[0];
if (m == 0)
{
return 0f;
}
double j = ((m / s1.Length + m / s2.Length + (m - mtp[1]) / m))
/ THREE;
double jw = j;
if (j > Threshold)
{
jw = j + Math.Min(JW_COEF, 1.0 / mtp[THREE]) * mtp[2] * (1 - j);
}
return jw;
}
/// <summary>
/// Return 1 - similarity.
/// </summary>
/// <param name="s1">The first string to compare.</param>
/// <param name="s2">The second string to compare.</param>
/// <returns>1 - similarity</returns>
/// <exception cref="ArgumentNullException">If s1 or s2 is null.</exception>
public double Distance(string s1, string s2)
=> 1.0 - Similarity(s1, s2);
/// <summary>
/// Calculates the distance between two sequences based on their similarity.
/// </summary>
/// <remarks>The distance is calculated as the complement of the similarity between the two
/// sequences.</remarks>
/// <typeparam name="T">The type of elements in the sequences. Must implement <see cref="IEquatable{T}"/>.</typeparam>
/// <param name="s1">The first sequence to compare.</param>
/// <param name="s2">The second sequence to compare.</param>
/// <returns>A double value representing the distance between the two sequences. The value ranges from 0.0 to 1.0, where
/// 0.0 indicates identical sequences and 1.0 indicates completely dissimilar sequences.</returns>
public double Distance<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
=> 1.0 - Similarity(s1, s2);
private static int[] Matches<T>(ReadOnlySpan<T> s1, ReadOnlySpan<T> s2)
where T : IEquatable<T>
{
ReadOnlySpan<T> max, min;
if (s1.Length > s2.Length)
{
max = s1;
min = s2;
}
else
{
max = s2;
min = s1;
}
int range = Math.Max(max.Length / 2 - 1, 0);
//int[] matchIndexes = new int[min.Length];
//Arrays.fill(matchIndexes, -1);
int[] match_indexes = Enumerable.Repeat(-1, min.Length).ToArray();
bool[] match_flags = new bool[max.Length];
int matches = 0;
for (int mi = 0; mi < min.Length; mi++)
{
var c1 = min[mi];
for (int xi = Math.Max(mi - range, 0),
xn = Math.Min(mi + range + 1, max.Length); xi < xn; xi++)
{
if (!match_flags[xi] && c1.Equals(max[xi]))
{
match_indexes[mi] = xi;
match_flags[xi] = true;
matches++;
break;
}
}
}
T[] ms1 = new T[matches];
T[] ms2 = new T[matches];
for (int i = 0, si = 0; i < min.Length; i++)
{
if (match_indexes[i] != -1)
{
ms1[si] = min[i];
si++;
}
}
for (int i = 0, si = 0; i < max.Length; i++)
{
if (match_flags[i])
{
ms2[si] = max[i];
si++;
}
}
int transpositions = 0;
for (int mi = 0; mi < ms1.Length; mi++)
{
if (!ms1[mi].Equals(ms2[mi]))
{
transpositions++;
}
}
int prefix = 0;
for (int mi = 0; mi < min.Length; mi++)
{
if (s1[mi].Equals(s2[mi]))
{
prefix++;
}
else
{
break;
}
}
return new[] { matches, transpositions / 2, prefix, max.Length };
}
}
}