github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/teambot/scoring.go (about)

     1  package teambot
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/keybase/client/go/protocol/keybase1"
     8  	"github.com/keybase/client/go/teams/opensearch"
     9  )
    10  
    11  const (
    12  	minRank    = 0
    13  	maxRank    = 10000
    14  	rankWeight = 3
    15  )
    16  
    17  type rankedSearchItem struct {
    18  	item  keybase1.FeaturedBot
    19  	score float64
    20  }
    21  
    22  func (i rankedSearchItem) Score(query string) (score float64) {
    23  	if !i.item.IsPromoted {
    24  		return 0
    25  	}
    26  	for _, qtok := range strings.Split(query, " ") {
    27  		score += opensearch.ScoreName(i.item.BotAlias, qtok)
    28  		score += opensearch.ScoreName(i.item.BotUsername, qtok)
    29  		score += opensearch.ScoreDescription(i.item.Description, qtok)
    30  		score += opensearch.ScoreDescription(i.item.ExtendedDescription, qtok)
    31  	}
    32  	if opensearch.FilterScore(score) {
    33  		return score
    34  	}
    35  	return score + normalizeRank(i.item.Rank)*rankWeight
    36  }
    37  
    38  func (i rankedSearchItem) String() string {
    39  	return fmt.Sprintf(
    40  		"Bot Alias: %s Rank: %d IsPromoted: %v Score: %.2f Description: %s ",
    41  		i.item.BotAlias, i.item.Rank, i.item.IsPromoted,
    42  		i.score, i.item.Description)
    43  }
    44  
    45  func normalizeRank(rank int) float64 {
    46  	if rank < minRank {
    47  		return 0
    48  	} else if rank > maxRank {
    49  		return 1
    50  	}
    51  	return float64(rank) / float64(minRank-maxRank)
    52  }