Skip to content

Commit dbfe452

Browse files
authored
Merge pull request #4 from paulirwin/master
Handle null and empty values in Jaro-Winkler
2 parents e4d5fa0 + 22fcaf2 commit dbfe452

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/F23.StringSimilarity/JaroWinkler.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public JaroWinkler(double threshold)
7373
*/
7474
public double Similarity(string s1, string s2)
7575
{
76+
if (s1 == null) s1 = string.Empty;
77+
if (s2 == null) s2 = string.Empty;
78+
79+
if (string.Equals(s1, s2)) return 1f;
80+
7681
int[] mtp = Matches(s1, s2);
7782
float m = mtp[0];
7883
if (m == 0)

test/F23.StringSimilarity.Tests/JaroWinklerTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,17 @@ public void TestSimilarity()
4949
precision: 6 // 0.000001
5050
);
5151
}
52+
53+
[Fact]
54+
public void TestSimilarityBothEmpty()
55+
{
56+
var instance = new JaroWinkler();
57+
58+
Assert.Equal(
59+
expected: 1,
60+
actual: instance.Similarity(string.Empty, string.Empty),
61+
precision: 6 // 0.000001
62+
);
63+
}
5264
}
5365
}

0 commit comments

Comments
 (0)