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

score: experimental extension novelty in sorting #665

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Changes from 2 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
48 changes: 48 additions & 0 deletions contentprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ import (
"bytes"
"fmt"
"log"
"os"
"path"
"sort"
"strings"
"unicode/utf8"

"golang.org/x/exp/slices"
)

var _ = log.Println
Expand Down Expand Up @@ -913,9 +917,53 @@ func sortChunkMatchesByScore(ms []ChunkMatch) {
sort.Sort(chunkMatchScoreSlice(ms))
}

var doNovelty = os.Getenv("ZOEKT_NOVELTY_DISABLE") == ""

// SortFiles sorts files matches. The order depends on the match score, which includes both
// query-dependent signals like word overlap, and file-only signals like the file ranks (if
// file ranks are enabled).
func SortFiles(ms []FileMatch) {
sort.Sort(fileMatchesByScore(ms))

if doNovelty {
// Experimentally boost something into the third filematch
boostNovelExtension(ms, 2, 0.9)
}
}

func boostNovelExtension(ms []FileMatch, boostOffset int, minScoreRatio float64) {
if len(ms) <= boostOffset+1 {
return
}

top := ms[:boostOffset]
candidates := ms[boostOffset:]

// Don't bother boosting something which is significantly different to the
// result it replaces.
minScoreForNovelty := candidates[0].Score * minScoreRatio

// We want to look for an ext that isn't in the top exts
exts := make([]string, len(top))
for i := range top {
exts[i] = path.Ext(top[i].FileName)
}

for i := range candidates {
// Do not assume sorted due to boostNovelExtension being called on subsets
if candidates[i].Score < minScoreForNovelty {
continue
}

if slices.Contains(exts, path.Ext(candidates[i].FileName)) {
continue
}

// Found what we are looking for, now boost to front of candidates (which
// is ms[boostOffset])
for ; i > 0; i-- {
candidates[i], candidates[i-1] = candidates[i-1], candidates[i]
}
return
}
}
Loading