Made search case-insensitive
This commit is contained in:
parent
88ea431a27
commit
2e3d11ca42
3 changed files with 13 additions and 2 deletions
1
assets/additional_cards.json
Normal file
1
assets/additional_cards.json
Normal file
File diff suppressed because one or more lines are too long
|
|
@ -1,6 +1,9 @@
|
||||||
package dicttranslate
|
package dicttranslate
|
||||||
|
|
||||||
import "github.com/texttheater/golang-levenshtein/levenshtein"
|
import (
|
||||||
|
"github.com/texttheater/golang-levenshtein/levenshtein"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
func match(query string, opts []string, maxDist int) (string, bool) {
|
func match(query string, opts []string, maxDist int) (string, bool) {
|
||||||
bestInd := -1
|
bestInd := -1
|
||||||
|
|
@ -8,7 +11,7 @@ func match(query string, opts []string, maxDist int) (string, bool) {
|
||||||
for i, s := range opts {
|
for i, s := range opts {
|
||||||
cfg := levenshtein.DefaultOptions
|
cfg := levenshtein.DefaultOptions
|
||||||
cfg.SubCost = 1
|
cfg.SubCost = 1
|
||||||
dist := levenshtein.DistanceForStrings([]rune(s), []rune(query), cfg)
|
dist := levenshtein.DistanceForStrings([]rune(strings.ToLower(s)), []rune(strings.ToLower(query)), cfg)
|
||||||
if dist <= maxDist && (bestInd == -1 || dist < bestDist) {
|
if dist <= maxDist && (bestInd == -1 || dist < bestDist) {
|
||||||
bestInd = i
|
bestInd = i
|
||||||
bestDist = dist
|
bestDist = dist
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,13 @@ func TestMatch(t *testing.T) {
|
||||||
opts: []string{"option", "opt1on"},
|
opts: []string{"option", "opt1on"},
|
||||||
shouldFind: false,
|
shouldFind: false,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Match case insensitive",
|
||||||
|
query: "option",
|
||||||
|
opts: []string{"OPTION", "opt1on"},
|
||||||
|
shouldFind: true,
|
||||||
|
match: "OPTION",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue