Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve ties in scoring best term suggestions #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,20 @@ func (model *Model) corpusCount(input string) int {
func best(input string, potential map[string]*Potential) string {
var best string
var bestcalc, bonus int

// Create a sorted slice of strings to range over as `potential`
// is an unordered map (map[string]*Potential). Thus, for ties
// (terms with identical scores) the best term will be the last
// tied term when sorted in increasing order (note ge operator)
keys := make([]string, 0, len(potential))
for k := range potential {
keys = append(keys, k)
}
sort.Strings(keys)

for i := 0; i < 4; i++ {
for _, pot := range potential {
for _, k := range keys {
pot := potential[k]
if pot.Leven == 0 {
return pot.Term
} else if pot.Leven == i {
Expand All @@ -416,7 +428,7 @@ func best(input string, potential map[string]*Potential) string {
if pot.Term[0] == input[0] {
bonus += 100
}
if pot.Score+bonus > bestcalc {
if pot.Score+bonus >= bestcalc {
bestcalc = pot.Score + bonus
best = pot.Term
}
Expand Down