scoring

package
v0.55.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2025 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateExponentialDecayTo

func CalculateExponentialDecayTo(score, decayTo float64, halfLife time.Duration) time.Duration

CalculateExponentialDecayTo calculates the time it takes for the score to decay to value equal to or less than decayTo. If score and decayTo have different signs, it returns 0. If decayTo is 0, it returns 0. If halfLife is 0 or negative, it returns 0.

func CalculateExponentialHalfLife

func CalculateExponentialHalfLife(score, decayTo float64, duration time.Duration) time.Duration

CalculateExponentialHalfLife calculates the half-life for an ExponentialDecay function to decay from score to decayTo in duration. If decayTo is 0, it returns 0. If duration is 0 or negative, it returns 0.

Types

type DecayFunc

type DecayFunc func(last, now time.Time, score float64) float64

func ExponentialDecay

func ExponentialDecay(halfLife time.Duration, decayToZero float64) DecayFunc

ExponentialDecay returns a DecayFunc that decays to zero exponentially over time. Returns nil if halfLife is negative.

func LinearDecay

func LinearDecay(decayPerSecond float64) DecayFunc

LinearDecay returns a DecayFunc that decays to zero linearly over time. Returns nil if decayPerSecond is negative.

type Scoring

type Scoring[T comparable] struct {
	// contains filtered or unexported fields
}

Scoring is a simple scoring system that allows to track scores for a set of items.

Scoring is thread-safe.

func New

func New[T comparable](decayFunc DecayFunc, minScore, maxScore float64) *Scoring[T]

New creates a new Scoring instance.

func (*Scoring[T]) Add

func (s *Scoring[T]) Add(id T, score float64)

Add adds score to the given ID. All consecutive score calculations will be based the time of the call. If the ID already exists, it does nothing.

func (*Scoring[T]) AddAt

func (s *Scoring[T]) AddAt(id T, score float64, at time.Time)

AddAt adds score to the given ID at the given time. All consecutive score calculations will be based on the given time. If the ID already exists, it does nothing.

func (*Scoring[T]) Decrease

func (s *Scoring[T]) Decrease(id T, score float64)

Decrease decreases the score for the given ID by the given amount. All consecutive score calculations will be based on the time of the call. If the ID does not exist, it does nothing.

func (*Scoring[T]) DecreaseAt

func (s *Scoring[T]) DecreaseAt(id T, score float64, at time.Time)

DecreaseAt decreases the score for the given ID by the given amount at the given time. All consecutive score calculations will be based on the given time. If the ID does not exist, it does nothing.

func (*Scoring[T]) Increase

func (s *Scoring[T]) Increase(id T, score float64)

Increase increases the score for the given ID by the given amount. All consecutive score calculations will be based on the time of the call. If the ID does not exist, it does nothing.

func (*Scoring[T]) IncreaseAt

func (s *Scoring[T]) IncreaseAt(id T, score float64, at time.Time)

IncreaseAt increases the score for the given ID by the given amount at the given time. All consecutive score calculations will be based on the given time. If the ID does not exist, it does nothing.

func (*Scoring[T]) Map

func (s *Scoring[T]) Map() map[T]float64

Map returns a map of IDs to their scores.

func (*Scoring[T]) MapAt

func (s *Scoring[T]) MapAt(at time.Time) map[T]float64

MapAt returns a map of IDs to their scores at the given time.

func (*Scoring[T]) Ranked

func (s *Scoring[T]) Ranked(granularity float64) []T

Ranked returns the IDs sorted by their scores in descending order.

All scores are rounded to the nearest multiple of the granularity, and the ranking is based on the rounded scores. This guarantee that items with similar scores will be returned in random order.

func (*Scoring[T]) RankedAt

func (s *Scoring[T]) RankedAt(at time.Time, granularity float64) []T

RankedAt returns the IDs sorted by their scores at the given time in descending order.

All scores are rounded to the nearest multiple of the granularity, and the ranking is based on the rounded scores. This guarantee that items with similar scores will be returned in random order.

func (*Scoring[T]) Remove

func (s *Scoring[T]) Remove(id T)

Remove removes the given ID. If the ID does not exist, it does nothing.

func (*Scoring[T]) Score

func (s *Scoring[T]) Score(id T) (float64, bool)

Score returns the score for the given ID. The boolean return value indicates whether the ID exists.

func (*Scoring[T]) ScoreAt

func (s *Scoring[T]) ScoreAt(id T, at time.Time) (float64, bool)

ScoreAt returns the score for the given ID at the given time. The boolean return value indicates whether the ID exists.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL