-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vladimir Yarotsky
committed
Dec 17, 2021
1 parent
cb488dc
commit 6afac93
Showing
5 changed files
with
159 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package dawg | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"math" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
func FilterChoices(choices []string, query string) []string { | ||
scoredChoices := NewScoredChoices(choices, query) | ||
sort.Sort(sort.Reverse(scoredChoices)) | ||
var filtered []string | ||
|
||
for i, m := range scoredChoices.Choices { | ||
if scoredChoices.Scores[i] > 0.0 { | ||
filtered = append(filtered, m) | ||
} | ||
} | ||
|
||
return filtered | ||
} | ||
|
||
type ScoredChoices struct { | ||
Choices []string | ||
Scores []float64 | ||
Query string | ||
} | ||
|
||
func NewScoredChoices(choices []string, query string) ScoredChoices { | ||
scores := make([]float64, len(choices)) | ||
|
||
for i, c := range choices { | ||
scores[i] = score(c, query) | ||
} | ||
|
||
return ScoredChoices{Choices: choices, Scores: scores, Query: query} | ||
} | ||
|
||
func (a ScoredChoices) Len() int { return len(a.Choices) } | ||
func (a ScoredChoices) Less(i, j int) bool { return a.Scores[i] < a.Scores[j] } | ||
func (a ScoredChoices) Swap(i, j int) { | ||
a.Choices[i], a.Choices[j] = a.Choices[j], a.Choices[i] | ||
a.Scores[i], a.Scores[j] = a.Scores[j], a.Scores[i] | ||
} | ||
|
||
func score(choice, query string) float64 { | ||
if len(query) == 0 { | ||
return 1.0 | ||
} | ||
|
||
if len(choice) == 0 { | ||
return 0.0 | ||
} | ||
|
||
choice = strings.ToLower(choice) | ||
query = strings.ToLower(query) | ||
|
||
matchLength, err := computeMatchLength(choice, query) | ||
if err != nil { | ||
return 0.0 | ||
} | ||
|
||
score := float64(len(query)) / float64(matchLength) | ||
normalizationFactor := float64(len(query)) / float64(len(choice)) | ||
normalizedScore := score * normalizationFactor | ||
return normalizedScore | ||
} | ||
|
||
func computeMatchLength(str, chars string) (int, error) { | ||
runes := []rune(chars) | ||
firstChar := runes[0] | ||
restChars := string(runes[1:]) | ||
|
||
firstIndexes := findCharInString(firstChar, str) | ||
|
||
matchLength := math.MaxInt32 | ||
|
||
for _, i := range firstIndexes { | ||
lastIndex := findEndOfMatch(str, restChars, i) | ||
if lastIndex != -1 { | ||
newMatchLength := lastIndex - i + 1 | ||
if matchLength > newMatchLength { | ||
matchLength = newMatchLength | ||
} | ||
} | ||
} | ||
|
||
if matchLength == math.MaxInt32 { | ||
return -1, errors.New("did not match") | ||
} | ||
return matchLength, nil | ||
} | ||
|
||
func findCharInString(chr rune, str string) []int { | ||
indexes := []int{} | ||
|
||
for i, cur := range []rune(str) { | ||
if chr == cur { | ||
indexes = append(indexes, i) | ||
} | ||
} | ||
|
||
return indexes | ||
} | ||
|
||
func findEndOfMatch(str, chars string, firstIndex int) int { | ||
lastIndex := firstIndex | ||
byteStr := []byte(str) | ||
for _, chr := range chars { | ||
i := bytes.IndexRune(byteStr[(lastIndex+1):], chr) | ||
if i == -1 { | ||
return -1 | ||
} | ||
lastIndex += i | ||
} | ||
|
||
return lastIndex + 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dawg | ||
|
||
import "testing" | ||
|
||
var epsilon = 0.001 | ||
|
||
func TestFuzz(t *testing.T) { | ||
tests := []struct { | ||
query string | ||
choices []string | ||
}{ | ||
{"api", []string{"api", "my-api", "a-phrase-that-includes-all-letters-in-order", "angular-tooltips"}}, | ||
{"map", []string{"my-api", "api"}}, | ||
{"open", []string{"Open", "my-openthingy"}}, | ||
} | ||
|
||
for _, tt := range tests { | ||
for i := 0; i < len(tt.choices)-1; i++ { | ||
choice0, choice1 := tt.choices[i], tt.choices[i+1] | ||
score0 := score(choice0, tt.query) | ||
score1 := score(choice1, tt.query) | ||
if score0 < score1 { | ||
t.Errorf("expected score for choice %s (%f) to be greater than score for choice %s (%f) for query %s", choice0, score0, choice1, score1, tt.query) | ||
} | ||
} | ||
} | ||
} |