Skip to content

Commit

Permalink
Merge pull request #57 from josharian/transform-once
Browse files Browse the repository at this point in the history
transform once
  • Loading branch information
lithammer authored May 9, 2023
2 parents 6053418 + 86187d3 commit 1701d62
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
17 changes: 13 additions & 4 deletions fuzzy/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,12 @@ func MatchNormalizedFold(source, target string) bool {
}

func match(source, target string, transformer transform.Transformer) bool {
source = stringTransform(source, transformer)
target = stringTransform(target, transformer)
sourceT := stringTransform(source, transformer)
targetT := stringTransform(target, transformer)
return matchTransformed(sourceT, targetT)
}

func matchTransformed(source, target string) bool {
lenDiff := len(target) - len(source)

if lenDiff < 0 {
Expand Down Expand Up @@ -100,10 +103,13 @@ func FindNormalizedFold(source string, targets []string) []string {
}

func find(source string, targets []string, transformer transform.Transformer) []string {
sourceT := stringTransform(source, transformer)

var matches []string

for _, target := range targets {
if match(source, target, transformer) {
targetT := stringTransform(target, transformer)
if matchTransformed(sourceT, targetT) {
matches = append(matches, target)
}
}
Expand Down Expand Up @@ -193,10 +199,13 @@ func RankFindNormalizedFold(source string, targets []string) Ranks {
}

func rankFind(source string, targets []string, transformer transform.Transformer) Ranks {
sourceT := stringTransform(source, transformer)

var r Ranks

for index, target := range targets {
if match(source, target, transformer) {
targetT := stringTransform(target, transformer)
if matchTransformed(sourceT, targetT) {
distance := LevenshteinDistance(source, target)
r = append(r, Rank{source, target, distance, index})
}
Expand Down
24 changes: 22 additions & 2 deletions fuzzy/fuzzy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ Rhine; and look toward the north and the rising sun. Aquitania extends from the
the Pyrenaean mountains and to that part of the ocean which is near Spain: it looks between the
setting of the sun, and the north star.`

var fuzzyTests = []struct {
type fuzzyTest struct {
source string
target string
wanted bool
rank int
}{
}

var fuzzyTests = []fuzzyTest{
{"zazz", deBelloGallico + " zazz", true, 1544},
{"zazz", "zazz " + deBelloGallico, true, 1544},
{"twl", "cartwheel", true, 6},
Expand Down Expand Up @@ -353,6 +355,24 @@ func BenchmarkMatchFoldBigEarly(b *testing.B) {
}
}

func BenchmarkFindFold(b *testing.B) {
b.Run("Plain", func(b *testing.B) { benchmarkFindFold(b, fuzzyTests[2]) })
b.Run("BigLate", func(b *testing.B) { benchmarkFindFold(b, fuzzyTests[0]) })
b.Run("BigEarly", func(b *testing.B) { benchmarkFindFold(b, fuzzyTests[1]) })
}

func benchmarkFindFold(b *testing.B, ft fuzzyTest) {
src := ft.source
var tgts []string
for i := 0; i < 128; i++ {
tgts = append(tgts, ft.target)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
FindFold(src, tgts)
}
}

func BenchmarkRankMatch(b *testing.B) {
ft := fuzzyTests[2]
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 1701d62

Please sign in to comment.