stillbox/internal/trending/score.go

43 lines
683 B
Go
Raw Normal View History

2024-10-22 23:44:40 -04:00
package trending
type Score[K comparable] struct {
ID K
2024-10-22 23:44:40 -04:00
Score float64
Probability float64
Expectation float64
Maximum float64
KLScore float64
Count float64
RecentCount float64
2024-10-22 23:44:40 -04:00
}
type Scores[K comparable] []Score[K]
2024-10-22 23:44:40 -04:00
func (s Scores[K]) Len() int {
2024-10-22 23:44:40 -04:00
return len(s)
}
func (s Scores[K]) Swap(i, j int) {
2024-10-22 23:44:40 -04:00
s[i], s[j] = s[j], s[i]
}
func (s Scores[K]) Less(i, j int) bool {
2024-10-22 23:44:40 -04:00
return s[i].Score > s[j].Score
}
func (s Scores[K]) take(count int) Scores[K] {
2024-10-22 23:44:40 -04:00
if count >= len(s) {
return s
}
return s[0 : count-1]
}
func (s Scores[K]) threshold(t float64) Scores[K] {
2024-10-22 23:44:40 -04:00
for i := range s {
if s[i].Score < t {
return s[0:i]
}
}
return s
}