A heart adds to what the listening says of an artist: a favourite artist counts as much as three complete listens, a favourite album 1.5 for its album artist and a favourite song 1 for each of its main artists, at most 6 for one artist, without fading with time. An artist hearted but never played is liked, with no listens. That feeds Discover, the mixes and the smart playlists that use the taste, and GET /me/taste. In the mixes of what the listener likes (genre mixes, Your favourites, Rediscover) a hearted song is wanted 1.5 times as much and a song on a hearted album 1.25 times. New to you is not changed.
185 lines
5.9 KiB
Go
185 lines
5.9 KiB
Go
// Package taste learns what a listener likes from what they listen to.
|
|
//
|
|
// Everything here is computed from the listener's own history and from tags that
|
|
// are already stored. Nothing is sent to any provider, and nothing is kept: the
|
|
// profile is worked out again on every request, so a change to the listening
|
|
// rule or to the half-life applies to the whole past at once.
|
|
//
|
|
// Each listening event has a value (see EventValue). Older events weigh less
|
|
// (the weight halves every Options.HalfLife). The values of one artist add up
|
|
// and are squeezed into (-1, 1) by tanh, so a few listens already say something
|
|
// but they only approach 1, and one skip does not sink an artist. tanh is for
|
|
// showing: it rounds to exactly 1 for a very large sum, so artists are ordered by
|
|
// the sum itself (Score).
|
|
package taste
|
|
|
|
import (
|
|
"math"
|
|
"sort"
|
|
"time"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/listening"
|
|
)
|
|
|
|
// sumScale is the sum of weighted values that gives tanh(1), about 0.76: three
|
|
// full recent listens.
|
|
const sumScale = 3.0
|
|
|
|
// What a heart is worth, in the same units as a complete listen (1). A heart is a statement the listener made on
|
|
// purpose and it does not fade with time, so these are plain sums, not weighed by age.
|
|
const (
|
|
FavoriteArtistValue = 3.0 // a favourite artist: three complete listens
|
|
FavoriteAlbumValue = 1.5 // a favourite album, for its album artist
|
|
FavoriteTrackValue = 1.0 // a favourite song, for each of its main artists
|
|
// MaxFavoriteValue is the most the hearts of one artist can add, so that a thousand hearted songs do not put one
|
|
// artist above everything else.
|
|
MaxFavoriteValue = 6.0
|
|
)
|
|
|
|
// Favorites are the hearts that point at one artist: the artist itself, the albums it made and the songs it is a main
|
|
// artist of.
|
|
type Favorites struct {
|
|
Artist bool
|
|
Albums int
|
|
Tracks int
|
|
}
|
|
|
|
// FavoriteValue is what the hearts of one artist add to what the listening says of it.
|
|
func FavoriteValue(f Favorites) float64 {
|
|
v := float64(f.Albums)*FavoriteAlbumValue + float64(f.Tracks)*FavoriteTrackValue
|
|
if f.Artist {
|
|
v += FavoriteArtistValue
|
|
}
|
|
return math.Min(v, MaxFavoriteValue)
|
|
}
|
|
|
|
// WithFavorites adds what the hearts say to the preferences, in place, and returns them. An artist the listener
|
|
// never played but hearted becomes liked; its listens stay as they are, because a heart is not a listen.
|
|
func WithFavorites(prefs map[int64]ArtistPref, favs map[int64]Favorites) map[int64]ArtistPref {
|
|
for id, f := range favs {
|
|
v := FavoriteValue(f)
|
|
if v == 0 {
|
|
continue
|
|
}
|
|
p := prefs[id]
|
|
p.ArtistID = id
|
|
p.Score += v
|
|
p.Preference = math.Tanh(p.Score / sumScale)
|
|
prefs[id] = p
|
|
}
|
|
return prefs
|
|
}
|
|
|
|
// Options are the knobs of the model.
|
|
type Options struct {
|
|
MinListen time.Duration // how long a play must last to count as a listen
|
|
HalfLife time.Duration // after this long an event weighs half as much
|
|
}
|
|
|
|
// Event is one thing the listener did with a track, credited to one of its artists.
|
|
type Event struct {
|
|
ArtistID int64
|
|
Type listening.EventType
|
|
PlayedMs int64
|
|
At time.Time
|
|
}
|
|
|
|
// ArtistPref is what the history says about one artist.
|
|
type ArtistPref struct {
|
|
ArtistID int64
|
|
Preference float64 // from -1 (avoided) to 1 (loved); 0 is neutral
|
|
Score float64 // the weighted sum behind Preference; it keeps order where tanh rounds to 1
|
|
Listens int // events that count as a listen, with no decay
|
|
LastListened time.Time // the latest counted event; zero when there is none
|
|
}
|
|
|
|
// TagWeight is how much a tag matters to the listener, from 0 to 1.
|
|
type TagWeight struct {
|
|
Tag string
|
|
Weight float64
|
|
}
|
|
|
|
// EventValue says how much one event tells in favour (positive) or against
|
|
// (negative) the artist: a complete listen +1, any other listen +0.5, a skip
|
|
// before the listener had heard enough -1, and a play too short to mean anything 0.
|
|
func EventValue(t listening.EventType, playedMs int64, minListen time.Duration) float64 {
|
|
switch {
|
|
case t == listening.Complete:
|
|
return 1
|
|
case playedMs >= minListen.Milliseconds():
|
|
return 0.5
|
|
case t == listening.Skip:
|
|
return -1
|
|
default:
|
|
return 0
|
|
}
|
|
}
|
|
|
|
func counted(t listening.EventType, playedMs int64, minListen time.Duration) bool {
|
|
return t == listening.Complete || playedMs >= minListen.Milliseconds()
|
|
}
|
|
|
|
// Preferences works out the preference of every artist that has events.
|
|
func Preferences(events []Event, now time.Time, o Options) map[int64]ArtistPref {
|
|
sums := map[int64]float64{}
|
|
out := map[int64]ArtistPref{}
|
|
for _, e := range events {
|
|
if e.At.After(now) {
|
|
e.At = now // a wrong clock does not make an event newer than now
|
|
}
|
|
age := now.Sub(e.At)
|
|
weight := math.Exp2(-float64(age) / float64(o.HalfLife))
|
|
sums[e.ArtistID] += weight * EventValue(e.Type, e.PlayedMs, o.MinListen)
|
|
|
|
p := out[e.ArtistID]
|
|
p.ArtistID = e.ArtistID
|
|
if counted(e.Type, e.PlayedMs, o.MinListen) {
|
|
p.Listens++
|
|
if e.At.After(p.LastListened) {
|
|
p.LastListened = e.At
|
|
}
|
|
}
|
|
out[e.ArtistID] = p
|
|
}
|
|
for id, p := range out {
|
|
p.Score = sums[id]
|
|
p.Preference = math.Tanh(sums[id] / sumScale)
|
|
out[id] = p
|
|
}
|
|
return out
|
|
}
|
|
|
|
// TagProfile adds up the tags of the artists the listener likes, each weighed by
|
|
// how much, and scales the result so the best tag is 1. Artists with a preference
|
|
// of zero or less add nothing. tags holds, per artist, tag weights from 0 to 1.
|
|
// The result is best first, ties in name order.
|
|
func TagProfile(prefs map[int64]ArtistPref, tags map[int64]map[string]float64) []TagWeight {
|
|
sums := map[string]float64{}
|
|
for id, p := range prefs {
|
|
if p.Preference <= 0 {
|
|
continue
|
|
}
|
|
for tag, w := range tags[id] {
|
|
sums[tag] += p.Preference * w
|
|
}
|
|
}
|
|
out := make([]TagWeight, 0, len(sums))
|
|
best := 0.0
|
|
for tag, w := range sums {
|
|
if w <= 0 {
|
|
continue
|
|
}
|
|
out = append(out, TagWeight{Tag: tag, Weight: w})
|
|
best = math.Max(best, w)
|
|
}
|
|
for i := range out {
|
|
out[i].Weight /= best
|
|
}
|
|
sort.Slice(out, func(i, j int) bool {
|
|
if out[i].Weight != out[j].Weight {
|
|
return out[i].Weight > out[j].Weight
|
|
}
|
|
return out[i].Tag < out[j].Tag
|
|
})
|
|
return out
|
|
}
|