package recommendation import ( "math" "testing" ) func approxEq(a, b float64) bool { return math.Abs(a-b) < 1e-9 } // Similarity is the set overlap (Jaccard) of tags weighted 0.7 plus that of // artists weighted 0.3. Tag counts collapse to a set, an axis empty on either // side contributes nothing, empty vectors give 0 rather than NaN, and a seed // vector (too short a session to say anything) matches nothing. func TestSimilarity(t *testing.T) { v := SessionVector{Artists: []string{"a1", "a2"}, Tags: map[string]int{"rock": 2, "indie": 1}} for _, tc := range []struct { name string a, b SessionVector want float64 }{ {"identical", v, v, 1.0}, {"disjoint", SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}}, SessionVector{Artists: []string{"a2"}, Tags: map[string]int{"jazz": 1}}, 0}, {"tags only shared", SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}}, SessionVector{Artists: []string{"a2"}, Tags: map[string]int{"rock": 5}}, 0.7}, {"artists only shared", SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}}, SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"jazz": 1}}, 0.3}, {"partial tag overlap", SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1, "indie": 1}}, SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1, "jazz": 1}}, 0.7*(1.0/3.0) + 0.3}, {"counts collapse to a set", SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 2, "indie": 1}}, SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 5, "indie": 3}}, 1.0}, {"each side has a different axis", SessionVector{Tags: map[string]int{"rock": 1}}, SessionVector{Artists: []string{"a1"}}, 0}, {"both empty", SessionVector{}, SessionVector{}, 0}, {"a seed on the right", v, SessionVector{Seed: true, Artists: v.Artists, Tags: v.Tags}, 0}, {"a seed on the left", SessionVector{Seed: true, Artists: v.Artists, Tags: v.Tags}, v, 0}, } { t.Run(tc.name, func(t *testing.T) { got := Similarity(tc.a, tc.b, DefaultSimilarityWeights) if math.IsNaN(got) || !approxEq(got, tc.want) { t.Errorf("Similarity = %v, want %v", got, tc.want) } }) } } // A track's contextual match is its best similarity to the sessions in which // it was liked, ignoring likes made in a seed session; a current seed session // matches nothing. func TestContextualMatchScore(t *testing.T) { current := SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}} full := SessionVector{Artists: []string{"a1"}, Tags: map[string]int{"rock": 1}} tagsOnly := SessionVector{Artists: []string{"a2"}, Tags: map[string]int{"rock": 1}} none := SessionVector{Artists: []string{"a99"}, Tags: map[string]int{"jazz": 1}} seedFull := SessionVector{Seed: true, Artists: full.Artists, Tags: full.Tags} for _, tc := range []struct { name string current SessionVector likes []SessionVector want float64 }{ {"no likes", current, nil, 0}, {"the best like counts", current, []SessionVector{full, tagsOnly, none}, 1.0}, {"likes in seed sessions are ignored", current, []SessionVector{seedFull, seedFull}, 0}, {"seed likes are dropped before the max", current, []SessionVector{seedFull, tagsOnly}, 0.7}, {"a seed session matches nothing", SessionVector{Seed: true}, []SessionVector{full}, 0}, } { t.Run(tc.name, func(t *testing.T) { if got := ContextualMatchScore(tc.current, tc.likes, DefaultSimilarityWeights); !approxEq(got, tc.want) { t.Errorf("ContextualMatchScore = %v, want %v", got, tc.want) } }) } }