Skip to content

Commit

Permalink
Fix Jaro for cases when len == 1
Browse files Browse the repository at this point in the history
  • Loading branch information
xrash committed Jul 30, 2020
1 parent f06e43c commit 89a2a8a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 4 deletions.
4 changes: 2 additions & 2 deletions jaro.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
func Jaro(a, b string) float64 {
// If both strings are zero-length, they are completely equal,
// therefore return 1.
if len(a) == 0 && len(a) == 0 {
if len(a) == 0 && len(b) == 0 {
return 1
}

Expand All @@ -20,7 +20,7 @@ func Jaro(a, b string) float64 {
// Define the necessary variables for the algorithm.
la := float64(len(a))
lb := float64(len(b))
matchRange := int(math.Floor(math.Max(la, lb)/2.0)) - 1
matchRange := int(math.Max(0, math.Floor(math.Max(la, lb)/2.0)-1))
matchesA := make([]bool, len(a))
matchesB := make([]bool, len(b))
var matches float64 = 0
Expand Down
2 changes: 1 addition & 1 deletion tests/jaro-winkler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestJaroWinkler(t *testing.T) {
result := fmt.Sprintf("%.3f", r)
expected := fmt.Sprintf("%.3f", c.r)
if result != expected {
fmt.Println(result, "instead of", expected, c.a, c.b)
fmt.Println(c.a, c.b, result, "instead of", expected)
t.Fail()
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/jaro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestJaro(t *testing.T) {
result := fmt.Sprintf("%.3f", r)
expected := fmt.Sprintf("%.3f", c.r)
if result != expected {
fmt.Println(result, "instead of", expected)
fmt.Println(c.a, c.b, result, "instead of", expected)
t.Fail()
}
}
Expand Down
93 changes: 93 additions & 0 deletions tests/testcases.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,52 @@ var __jaro_cases = []*jarocase{
{a: "LACURA", b: "LOCURA", r: 0.889},
{a: "IOWA", b: "IONA", r: 0.833},
// {a: "1ST", b: "IST", r: 0.000},

// Equal strings.
{a: "", b: "", r: 1.000},
{a: "A", b: "A", r: 1.000},
{a: "AA", b: "AA", r: 1.000},
{a: "AAA", b: "AAA", r: 1.000},
{a: "AAAA", b: "AAAA", r: 1.000},
{a: "AAAAA", b: "AAAAA", r: 1.000},
{a: "AAAAAA", b: "AAAAAA", r: 1.000},
{
a: "Legend of the Galactic Heroes",
b: "Legend of the Galactic Heroes",
r: 1.000,
},
{
a: "Home is the place where, when you have to go there, they have to take you in.",
b: "Home is the place where, when you have to go there, they have to take you in.",
r: 1.000,
},
{
a: "Pedro de Alcântara João Carlos Leopoldo Salvador Bibiano Francisco Xavier de Paula Leocádio Miguel Gabriel Rafael Gonzaga de Habsburgo-Lorena e Bragança",
b: "Pedro de Alcântara João Carlos Leopoldo Salvador Bibiano Francisco Xavier de Paula Leocádio Miguel Gabriel Rafael Gonzaga de Habsburgo-Lorena e Bragança",
r: 1.000,
},
{
a: "Et tu, Brute",
b: "Et tu, Brute",
r: 1.000,
},

// Completely different strings.
{a: "", b: "A", r: 0.000},
{a: "", b: "AA", r: 0.000},
{a: "", b: "AAA", r: 0.000},
{a: "", b: "AAAA", r: 0.000},
{a: "", b: "AAAAA", r: 0.000},
{a: "A", b: "", r: 0.000},
{a: "AA", b: "", r: 0.000},
{a: "AAA", b: "", r: 0.000},
{a: "AAAA", b: "", r: 0.000},
{a: "AAAAA", b: "", r: 0.000},
{a: "A", b: "B", r: 0.000},
{a: "AA", b: "BB", r: 0.000},
{a: "AAA", b: "BBB", r: 0.000},
{a: "AAAA", b: "BBBB", r: 0.000},
{a: "AAAAa", b: "BBBBB", r: 0.000},
}

var __jaro_winkler_cases = []*jarocase{
Expand Down Expand Up @@ -82,4 +128,51 @@ var __jaro_winkler_cases = []*jarocase{
{a: "LACURA", b: "LOCURA", r: 0.900},
{a: "IOWA", b: "IONA", r: 0.867},
// {a: "1ST", b: "IST", r: 0.000},
{a: "w", b: "w", r: 1.000},

// Equal strings.
{a: "", b: "", r: 1.000},
{a: "A", b: "A", r: 1.000},
{a: "AA", b: "AA", r: 1.000},
{a: "AAA", b: "AAA", r: 1.000},
{a: "AAAA", b: "AAAA", r: 1.000},
{a: "AAAAA", b: "AAAAA", r: 1.000},
{a: "AAAAAA", b: "AAAAAA", r: 1.000},
{
a: "Legend of the Galactic Heroes",
b: "Legend of the Galactic Heroes",
r: 1.000,
},
{
a: "Home is the place where, when you have to go there, they have to take you in.",
b: "Home is the place where, when you have to go there, they have to take you in.",
r: 1.000,
},
{
a: "Pedro de Alcântara João Carlos Leopoldo Salvador Bibiano Francisco Xavier de Paula Leocádio Miguel Gabriel Rafael Gonzaga de Habsburgo-Lorena e Bragança",
b: "Pedro de Alcântara João Carlos Leopoldo Salvador Bibiano Francisco Xavier de Paula Leocádio Miguel Gabriel Rafael Gonzaga de Habsburgo-Lorena e Bragança",
r: 1.000,
},
{
a: "Et tu, Brute",
b: "Et tu, Brute",
r: 1.000,
},

// Completely different strings.
{a: "", b: "A", r: 0.000},
{a: "", b: "AA", r: 0.000},
{a: "", b: "AAA", r: 0.000},
{a: "", b: "AAAA", r: 0.000},
{a: "", b: "AAAAA", r: 0.000},
{a: "A", b: "", r: 0.000},
{a: "AA", b: "", r: 0.000},
{a: "AAA", b: "", r: 0.000},
{a: "AAAA", b: "", r: 0.000},
{a: "AAAAA", b: "", r: 0.000},
{a: "A", b: "B", r: 0.000},
{a: "AA", b: "BB", r: 0.000},
{a: "AAA", b: "BBB", r: 0.000},
{a: "AAAA", b: "BBBB", r: 0.000},
{a: "AAAAa", b: "BBBBB", r: 0.000},
}

0 comments on commit 89a2a8a

Please sign in to comment.