Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

Commit

Permalink
Increased fuzzy accuracy.
Browse files Browse the repository at this point in the history
Added test for fuzzy.go
  • Loading branch information
avahe-kellenberger authored and Bios-Marcel committed Oct 6, 2019
1 parent 5f8662a commit d2c8529
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
23 changes: 10 additions & 13 deletions util/fuzzy/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ func Score(needle, haystack string) float64 {
needleIndex := 0
for haystackIndex := 0; haystackIndex < haystackLength && needleIndex < needleLength; haystackIndex++ {

letterScore, foundAtIndex := scoreLetter(needle[needleIndex], haystack, haystackIndex)
if letterScore < 0 {
return letterScore
letterIndex := findLetterIndex(needle[needleIndex], haystack, haystackIndex)
if letterIndex < 0 {
return -1
}

if foundAtIndex == haystackIndex {
if letterIndex == haystackIndex {
// Letter was consecutive
score += letterScore * 2
score += 8
} else {
score += letterScore
score++
// Move haystackIndex up to the next found letter.
haystackIndex = foundAtIndex
haystackIndex = letterIndex
}

needleIndex++
Expand All @@ -81,15 +81,12 @@ func Score(needle, haystack string) float64 {
// Scores a letter from inside a string based on its distance from the start of the string.
// The index at which the letter was found will be returned.
// The score and index will be -1 if the character is not found.
func scoreLetter(c byte, haystack string, startIndex int) (float64, int) {
haystackLength := len(haystack)
func findLetterIndex(c byte, haystack string, startIndex int) int {
for i := startIndex; i < len(haystack); i++ {
if c == haystack[i] {
var displacement float64 = float64(haystackLength - i)
score := displacement / float64(haystackLength) * (1.0 / float64(haystackLength))
return score, i
return i
}
}
// Letter not found.
return -1, -1
return -1
}
36 changes: 36 additions & 0 deletions util/fuzzy/fuzzy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package fuzzy

import (
"testing"
)

func TestFuzzyScore(t *testing.T) {
scoreA := Score("Marc", "Marcel#4759")
scoreB := Score("Marc", "Maurice")
if scoreA <= scoreB {
t.Errorf("Incorrect score rating")
}

scoreC := Score("Marc", "Marchesi#4377")
if scoreC <= scoreB {
t.Errorf("Incorrect score rating")
}

arr := []string{"tests", "test", "testosterone", "atesta", "bob"}
sorted := SortSearchResults(ScoreSearch("te", arr))
expected := [4]string{"test", "testosterone", "tests", "atesta"}

if len(sorted) != len(expected) {
t.Errorf("Expected length of %d, but received %d.\n", len(expected), len(sorted))
}

var results [4]string
for i, result := range sorted {
results[i] = result.Key
}

if results != expected {
t.Errorf("Expected\n%v\nbut received\n%v\n", expected, results)
}

}

0 comments on commit d2c8529

Please sign in to comment.