Skip to content

Commit

Permalink
Merge pull request #97 from tminaorg/ranker
Browse files Browse the repository at this point in the history
added shallow ranking algo
  • Loading branch information
k4lizen authored Oct 3, 2023
2 parents f021198 + c23d94b commit b2808f7
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 23 deletions.
8 changes: 4 additions & 4 deletions src/bucket/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func AddSEResult(seResult *engines.RetrievedResult, seName engines.Name, relay *
engineRanks[0] = seResult.Rank
result := result.Result{
URL: seResult.URL,
Rank: -1,
Rank: 0,
Title: seResult.Title,
Description: seResult.Description,
EngineRanks: engineRanks,
Expand Down Expand Up @@ -84,9 +84,9 @@ func SetResultResponse(link string, response *colly.Response, relay *Relay, seNa
func MakeSEResult(urll string, title string, description string, searchEngineName engines.Name, sePage int, seOnPageRank int) *engines.RetrievedResult {
ser := engines.RetrievedRank{
SearchEngine: searchEngineName,
Rank: -1,
Page: sePage,
OnPageRank: seOnPageRank,
Rank: 0,
Page: uint(sePage),
OnPageRank: uint(seOnPageRank),
}
res := engines.RetrievedResult{
URL: urll,
Expand Down
5 changes: 3 additions & 2 deletions src/bucket/result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
// The URL is the primary key
type Result struct {
URL string
Rank int
Rank uint
Score float64
Title string
Description string
EngineRanks []engines.RetrievedRank
TimesReturned int
TimesReturned uint8
Response *colly.Response
}
65 changes: 65 additions & 0 deletions src/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,70 @@ func New() *Config {
},
},
},
Ranking: Ranking{
REXP: 0.5,
A: 1,
B: 0,
C: 1,
D: 0,
TRA: 1,
TRB: 0,
TRC: 1,
TRD: 0,
Engines: map[string]EngineRanking{
engines.Bing.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Brave.ToLower(): {
Mul: 1,
Const: 0,
},
engines.DuckDuckGo.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Etools.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Google.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Mojeek.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Presearch.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Qwant.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Startpage.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Swisscows.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Yahoo.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Yandex.ToLower(): {
Mul: 1,
Const: 0,
},
engines.Yep.ToLower(): {
Mul: 1,
Const: 0,
},
},
},
}
}
19 changes: 19 additions & 0 deletions src/config/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,27 @@ type Server struct {
RedisUrl string `koanf:"redisUrl"`
}

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

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"`
}

// Config struct for Koanf
type Config struct {
Server Server `koanf:"server"`
Engines map[string]Engine `koanf:"engines"`
Ranking Ranking `koanf:"ranking"`
}
6 changes: 3 additions & 3 deletions src/engines/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package engines
// Information about what Rank a result was on some Search Engine
type RetrievedRank struct {
SearchEngine Name
Rank int
Page int
OnPageRank int
Rank uint
Page uint
OnPageRank uint
}

// The info a Search Engine returned about some Result
Expand Down
4 changes: 2 additions & 2 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
func printResults(results []result.Result) {
fmt.Print("\n\tThe Search Results:\n\n")
for _, r := range results {
fmt.Printf("%v -----\n\t\"%v\"\n\t\"%v\"\n\t\"%v\"\n\t-", r.Rank, r.Title, r.URL, r.Description)
for seInd := 0; seInd < r.TimesReturned; seInd++ {
fmt.Printf("%v (%.2f) -----\n\t\"%v\"\n\t\"%v\"\n\t\"%v\"\n\t-", r.Rank, r.Score, r.Title, r.URL, r.Description)
for seInd := uint8(0); seInd < r.TimesReturned; seInd++ {
fmt.Printf("%v", r.EngineRanks[seInd].SearchEngine)
if seInd != r.TimesReturned-1 {
fmt.Print(", ")
Expand Down
19 changes: 19 additions & 0 deletions src/rank/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package rank

import (
"math"
)

const magic64 = 0x5FE6EB50C7B537A9

func fastInvSqrt64(n float64) float64 {
if n < 0 {
return math.NaN()
}
n2, th := n*0.5, float64(1.5)
b := math.Float64bits(n)
b = magic64 - (b >> 1)
f := math.Float64frombits(b)
f *= th - (n2 * f * f)
return f
}
29 changes: 22 additions & 7 deletions src/rank/rank.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package rank

import (
"math"
"sort"

"github.com/tminaorg/brzaguza/src/bucket/result"
"github.com/tminaorg/brzaguza/src/config"
"github.com/tminaorg/brzaguza/src/engines"
)

func SetRank(result *result.Result) {
result.Rank = result.EngineRanks[0].Rank
func GetScore(result *result.Result, rconf *config.Ranking) float64 {
retRankScore := float64(0)
for _, er := range result.EngineRanks {
seMul := rconf.Engines[er.SearchEngine.ToLower()].Mul
seConst := rconf.Engines[er.SearchEngine.ToLower()].Const //these 2 could be preproced into array
retRankScore += (100.0/math.Pow(float64(er.Rank)*rconf.A+rconf.B, rconf.REXP)*rconf.C+rconf.D)*seMul + seConst
}
retRankScore /= float64(result.TimesReturned)

timesReturnedScore := math.Log(float64(result.TimesReturned)*rconf.TRA+rconf.TRB)*10*rconf.TRC + rconf.TRD

score := retRankScore + timesReturnedScore
return score
}

func Rank(resMap map[string]*result.Result) []result.Result {
func Rank(resMap map[string]*result.Result, rconf *config.Ranking) []result.Result {
results := make([]result.Result, 0, len(resMap))
for _, res := range resMap {
res.EngineRanks = res.EngineRanks[0:res.TimesReturned:res.TimesReturned]
Expand All @@ -21,10 +34,12 @@ func Rank(resMap map[string]*result.Result) []result.Result {
FillRetrievedRank(results)

for ind := range results {
SetRank(&(results[ind]))
results[ind].Score = GetScore(&(results[ind]), rconf)
}
sort.Sort(ByScore(results))
for ind := range results {
results[ind].Rank = uint(ind + 1)
}

sort.Sort(ByRank(results))

return results
}
Expand Down Expand Up @@ -52,7 +67,7 @@ func FillRetrievedRank(results []result.Result) {
sort.Sort(ByRetrievedRank(engRes))

for rnk, el := range engRes {
results[el.ArrInd].EngineRanks[el.RRInd].Rank = rnk + 1
results[el.ArrInd].EngineRanks[el.RRInd].Rank = uint(rnk + 1)
}
}
}
8 changes: 4 additions & 4 deletions src/rank/byrank.go → src/rank/sorting.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"github.com/tminaorg/brzaguza/src/bucket/result"
)

type ByRank []result.Result
type ByScore []result.Result

func (r ByRank) Len() int { return len(r) }
func (r ByRank) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ByRank) Less(i, j int) bool { return r[i].Rank < r[j].Rank }
func (r ByScore) Len() int { return len(r) }
func (r ByScore) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r ByScore) Less(i, j int) bool { return r[i].Score > r[j].Score }

type ByRetrievedRank []RankFiller

Expand Down
2 changes: 1 addition & 1 deletion src/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func PerformSearch(query string, options engines.Options, config *config.Config)

rankTiming := time.Now()
log.Debug().Msg("Ranking...")
results := rank.Rank(relay.ResultMap)
results := rank.Rank(relay.ResultMap, &(config.Ranking))
log.Debug().Msgf("Finished ranking in %v", time.Since(rankTiming).Milliseconds())

log.Debug().Msg("Search done!")
Expand Down

0 comments on commit b2808f7

Please sign in to comment.