// Package affinity learns which artists a person plays together, from their own listening: the plays are split into // sittings, and two artists that often turn up in the same sitting are linked. It is a count over the person's own // data, with no provider and no training service; it can be built again in a moment. package affinity import ( "math" "sort" "time" ) // Options are the knobs of the model. type Options struct { // Gap is the longest pause inside one sitting: a play that follows the one before by more than this starts another. Gap time.Duration // MaxArtists is how many different artists a sitting may have to teach anything: a sitting of a whole library on // shuffle says nothing about what goes together. MaxArtists int // MinTogether is in how many sittings two artists must meet to be linked: once is chance. MinTogether int // SeedHalfLife is how long it takes a play to weigh half as much when the artists are ranked by how much they // are played. It is long, because this is the whole history and not only the last months. SeedHalfLife time.Duration } // DefaultOptions are what Sonda uses. func DefaultOptions() Options { return Options{Gap: 30 * time.Minute, MaxArtists: 60, MinTogether: 2, SeedHalfLife: 2 * 365 * 24 * time.Hour} } // Play is one play that counts as listened: the artist (as a key that is the same for the same artist wherever the // play comes from) and when it ended. type Play struct { Key string Name string At time.Time } // Artist is what the model knows of one artist. type Artist struct { Key string Name string Plays int Sessions int // the sittings it was in that were used for the links Last time.Time // its last play Weight float64 // its plays, each worth 1 and halving every SeedHalfLife } // Link says that two artists are played together. A is before B. Score is from 0 to 1: the sittings they share over // the square root of the product of the sittings of each (a cosine), so a pair that is always together scores 1 // and a pair of which one is everywhere scores low. type Link struct { A, B string Score float64 } // Model is what was learned. type Model struct { Artists []Artist // by key Links []Link // by A, then B Plays int // the plays read Sessions int // the sittings they made } type pair struct{ a, b string } // Build learns from the plays. now is the moment the age of a play is counted to. func Build(plays []Play, now time.Time, o Options) Model { sorted := make([]Play, 0, len(plays)) for _, p := range plays { if p.Key != "" { sorted = append(sorted, p) } } sort.SliceStable(sorted, func(i, j int) bool { return sorted[i].At.Before(sorted[j].At) }) artists := map[string]*Artist{} use := func(p Play) *Artist { a := artists[p.Key] if a == nil { a = &Artist{Key: p.Key, Name: p.Name} artists[p.Key] = a } return a } sittings := map[string]int{} together := map[pair]int{} m := Model{Plays: len(sorted)} var current map[string]bool closeSitting := func() { if len(current) == 0 { return } m.Sessions++ if len(current) > o.MaxArtists { return } keys := make([]string, 0, len(current)) for k := range current { keys = append(keys, k) sittings[k]++ } sort.Strings(keys) for i := range keys { for j := i + 1; j < len(keys); j++ { together[pair{keys[i], keys[j]}]++ } } } var previous time.Time for i, p := range sorted { if i == 0 || p.At.Sub(previous) > o.Gap { closeSitting() current = map[string]bool{} } previous = p.At current[p.Key] = true a := use(p) a.Plays++ if p.At.After(a.Last) { a.Last = p.At } age := now.Sub(p.At) if age < 0 { age = 0 // a play dated after now weighs as one that just happened } a.Weight += math.Exp2(-float64(age) / float64(o.SeedHalfLife)) } closeSitting() for k, n := range sittings { artists[k].Sessions = n } for pr, n := range together { if n < o.MinTogether { continue } m.Links = append(m.Links, Link{A: pr.a, B: pr.b, Score: float64(n) / math.Sqrt(float64(sittings[pr.a])*float64(sittings[pr.b]))}) } sort.Slice(m.Links, func(i, j int) bool { if m.Links[i].A != m.Links[j].A { return m.Links[i].A < m.Links[j].A } return m.Links[i].B < m.Links[j].B }) m.Artists = make([]Artist, 0, len(artists)) for _, a := range artists { m.Artists = append(m.Artists, *a) } sort.Slice(m.Artists, func(i, j int) bool { return m.Artists[i].Key < m.Artists[j].Key }) return m }