Skip to content

Commit

Permalink
refactor(rank): renamed vars for fn and removed hardcoded *10 (affect…
Browse files Browse the repository at this point in the history
…ing TRC, now timesreturnedscoremul)
  • Loading branch information
aleksasiriski committed Nov 15, 2024
1 parent e4804c0 commit 229e7a1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 33 deletions.
24 changes: 12 additions & 12 deletions docs/example_category.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@
}
},
"ranking": {
"rexp": 0.5,
"a": 1,
"b": 0,
"c": 1,
"d": 0,
"tra": 1,
"trb": 0,
"trc": 1,
"trd": 0,
"rankexp": 0.5,
"rankmul": 1,
"rankconst": 0,
"rankscoremul": 1,
"rankscoreadd": 0,
"timesreturnedmul": 1,
"timesreturnedadd": 0,
"timesreturnedscoremul": 1,
"timesreturnedscoreadd": 0,
"engines": {
"google": {
"mul": 1,
"const": 0
"add": 0
},
"bing": {
"mul": 1,
"const": 0
"add": 0
},
"brave": {
"mul": 1,
"const": 0
"add": 0
}
}
},
Expand Down
28 changes: 27 additions & 1 deletion src/search/category/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,32 @@ func (cj CategoryJSON) ToCategoryType() (Category, error) {
}
}

// Ranking config.
ranking := Ranking{
RankExp: cj.Ranking.RankExp,
RankMul: cj.Ranking.RankMul,
RankAdd: cj.Ranking.RankAdd,
RankScoreMul: cj.Ranking.RankScoreMul,
RankScoreAdd: cj.Ranking.RankScoreAdd,
TimesReturnedMul: cj.Ranking.TimesReturnedMul,
TimesReturnedAdd: cj.Ranking.TimesReturnedAdd,
TimesReturnedScoreMul: cj.Ranking.TimesReturnedScoreMul,
TimesReturnedScoreAdd: cj.Ranking.TimesReturnedScoreAdd,
Engines: make(map[engines.Name]EngineRanking),
}

// Set the engine ranking config.
for nameS, er := range cj.Ranking.Engines {
name, err := engines.NameString(nameS)
if err != nil {
return Category{}, fmt.Errorf("failed converting string to engine name: %w", err)
}
ranking.Engines[name] = EngineRanking{
Mul: er.Mul,
Add: er.Add,
}
}

// Timings config.
timings := Timings{
PreferredTimeout: moretime.ConvertFromFancyTime(cj.Timings.PreferredTimeout),
Expand All @@ -77,7 +103,7 @@ func (cj CategoryJSON) ToCategoryType() (Category, error) {
RequiredByOriginEngines: engRequiredByOrigin,
PreferredEngines: engPreferred,
PreferredByOriginEngines: engPreferredByOrigin,
Ranking: cj.Ranking, // Stays the same.
Ranking: ranking,
Timings: timings,
}, nil
}
26 changes: 25 additions & 1 deletion src/search/category/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package category
// CategoryJSON is format in which the config is passed from the user.
type CategoryJSON struct {
Engines map[string]EngineJSON `koanf:"engines"`
Ranking Ranking `koanf:"ranking"`
Ranking RankingJSON `koanf:"ranking"`
Timings TimingsJSON `koanf:"timings"`
}

Expand All @@ -28,6 +28,30 @@ type EngineJSON struct {
PreferredByOrigin bool `koanf:"preferredbyorigin"`
}

// RankingJSON is format in which the config is passed from the user.
type RankingJSON struct {
// The exponent, multiplier and addition used on the rank itself.
RankExp float64 `koanf:"rankexp"`
RankMul float64 `koanf:"rankmul"`
RankAdd float64 `koanf:"rankconst"`
// The multiplier and addition used on the rank score (number calculated from dividing 100 with the rank + above variables applied).
RankScoreMul float64 `koanf:"rankscoremul"`
RankScoreAdd float64 `koanf:"rankscoreadd"`
// The multiplier and addition used on the number of times the result was returned.
TimesReturnedMul float64 `koanf:"timesreturnedmul"`
TimesReturnedAdd float64 `koanf:"timesreturnedadd"`
// The multiplier and addition used on the times returned score (number calculated from doing log(timesReturnedNum + above variables applied)).
TimesReturnedScoreMul float64 `koanf:"timesreturnedscoremul"`
TimesReturnedScoreAdd float64 `koanf:"timesreturnedscoreadd"`
// Multipliers and additions for each engine, applied to the rank score.
Engines map[string]EngineRankingJSON `koanf:"engines"`
}

type EngineRankingJSON struct {
Mul float64 `koanf:"mul"`
Add float64 `koanf:"add"`
}

// TimingsJSON is format in which the config is passed from the user.
// In <number><unit> format.
// Example: 1s, 1m, 1h, 1d, 1w, 1M, 1y.
Expand Down
29 changes: 17 additions & 12 deletions src/search/category/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,26 @@ type Category struct {
}

type Ranking struct {
REXP float64 `koanf:"rexp"`
A float64 `koanf:"a"`
B float64 `koanf:"b"`
C float64 `koanf:"c"`
D float64 `koanf:"d"`
TRA float64 `koanf:"tra"`
TRB float64 `koanf:"trb"`
TRC float64 `koanf:"trc"`
TRD float64 `koanf:"trd"`
Engines map[string]EngineRanking `koanf:"engines"`
// The exponent, multiplier and addition used on the rank itself.
RankExp float64
RankMul float64
RankAdd float64
// The multiplier and addition used on the rank score (number calculated from dividing 100 with the rank + above variables applied).
RankScoreMul float64
RankScoreAdd float64
// The multiplier and addition used on the number of times the result was returned.
TimesReturnedMul float64
TimesReturnedAdd float64
// The multiplier and addition used on the times returned score (number calculated from doing log(timesReturnedNum + above variables applied)).
TimesReturnedScoreMul float64
TimesReturnedScoreAdd float64
// Multipliers and additions for each engine, applied to the rank score.
Engines map[engines.Name]EngineRanking
}

type EngineRanking struct {
Mul float64 `koanf:"mul"`
Const float64 `koanf:"const"`
Mul float64
Add float64
}

type Timings struct {
Expand Down
20 changes: 13 additions & 7 deletions src/search/result/rank/score.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ func (s Suggestions) calculateScores(rconf category.Ranking) {

// Calculates the score for one result.
func calculateScore[T ranker](val scoreEngineRanker[T], rconf category.Ranking) float64 {
var retRankScore float64 = 0
var rankScoreSum float64 = 0

// Calculate the sum of the rank scores of all engines.
// The rank score is dividing 100 to invert the priority (the lower the rank, the higher the score).
for _, er := range val.EngineRanks() {
eng := rconf.Engines[er.SearchEngine().String()]
retRankScore += (100.0/math.Pow(float64(er.Rank())*rconf.A+rconf.B, rconf.REXP)*rconf.C+rconf.D)*eng.Mul + eng.Const
eng := rconf.Engines[er.SearchEngine()]
rankScoreSum += (100.0/math.Pow(float64(er.Rank())*rconf.RankMul+rconf.RankAdd, rconf.RankExp)*rconf.RankScoreMul+rconf.RankScoreAdd)*eng.Mul + eng.Add
}

retRankScore /= float64(len(val.EngineRanks()))
timesReturnedScore := math.Log(float64(len(val.EngineRanks()))*rconf.TRA+rconf.TRB)*10*rconf.TRC + rconf.TRD
score := retRankScore + timesReturnedScore
// Calculate the average rank score from the sum.
rankScoreAvg := rankScoreSum / float64(len(val.EngineRanks()))

// Calculate a second score based on the number of times the result was returned.
// Log is used to make the score less sensitive to the number of times returned.
timesReturnedScore := math.Log(float64(len(val.EngineRanks()))*rconf.TimesReturnedMul+rconf.TimesReturnedAdd)*rconf.TimesReturnedScoreMul + rconf.TimesReturnedScoreAdd

return score
return rankScoreAvg + timesReturnedScore
}

0 comments on commit 229e7a1

Please sign in to comment.