-
Notifications
You must be signed in to change notification settings - Fork 0
/
metaphone_test.go
99 lines (93 loc) · 2.03 KB
/
metaphone_test.go
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
package Metaphone
import "testing"
func TestPack(t *testing.T) {
cases := []struct {
in, want string
}{
{"gopher", "GFR"},
{"olá", "Ul"},
{"mundo", "MD"},
{"testando", "TSTD"},
{"Metaphone", "MTF"},
}
for _, c := range cases {
got := Pack(c.in)
if got != c.want {
t.Errorf("Pack(%q) == %q, want %q", c.in, got, c.want)
}
}
}
func TestParse(t *testing.T) {
cases := []struct {
in string
want *WordType
}{
{"Olá Mundo", &WordType{Words: []string{"OLÁ", "MUNDO"}, MTFs: []string{"Ul", "MD"}}},
{"", nil},
{"foo bar baz", &WordType{Words: []string{"FOO", "BAR", "BAZ"}, MTFs: []string{"F", "BR", "BS"}}},
}
for _, c := range cases {
got := Parse(c.in)
if got == nil && c.want == nil {
continue
}
if got == nil || c.want == nil || !got.Equals(c.want) {
t.Errorf("Parse(%q) == %v, want %v", c.in, got, c.want)
}
}
}
func (wt *WordType) Equals(other *WordType) bool {
if len(wt.Words) != len(other.Words) {
return false
}
if len(wt.MTFs) != len(other.MTFs) {
return false
}
for i, w := range wt.Words {
if w != other.Words[i] {
return false
}
}
for i, m := range wt.MTFs {
if m != other.MTFs[i] {
return false
}
}
return true
}
func TestIsMetaphoneSimilar(t *testing.T) {
cases := []struct {
a, b string
want bool
}{
{"daniel", "danilo", true},
{"testando", "testandi", true},
{"mundo", "mundi", true},
{"cara", "barra", false},
}
for _, c := range cases {
got := IsMetaphoneSimilar(Pack(c.a), Pack(c.b))
if got != c.want {
t.Errorf("IsMetaphoneSimilar(%q, %q) == %v, want %v", c.a, c.b, got, c.want)
}
}
}
func TestSimilarityBetweenWords(t *testing.T) {
cases := []struct {
a, b string
want float32
}{
{"daniel", "danilo", 0.6666666},
{"testando", "testandi", 0.875},
{"olá", "ola", 0.75},
{"mundo", "mundi", 0.8},
{"foo", "bar", 0.0},
{"dia", "DIA", 0.0},
}
for _, c := range cases {
got := SimilarityBetweenWords(c.a, c.b)
if got != c.want {
t.Errorf("SimilarityBetweenWords(%q, %q) == %v, want %v", c.a, c.b, got, c.want)
}
}
}