feat: base discovery on the whole mood collection
- seed from every artist (weighted by song count) and every album, not one track - walk Deezer's graph: artist top tracks, related artists, album deep cuts - score by artist frequency, collection album, catalog affinity and hint text - penalise repeated artists so a 30-slot mood spans artists, not one - refresh the pool when a song is added instead of waiting for refreshHours - skip discovery for a mood with no hint, genres or songs - extend tests and the smoke test with the artist graph
This commit is contained in:
1 parent
a78d359d0c
commit
62bcefad20
13 files changed
+911
-72
No files matched your search
@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory
|
||||
import win.taptap.musicmanager.db.MoodRecord
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.RecommendationUpsert
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
import win.taptap.musicmanager.util.IdGenerator
|
||||
|
||||
data class DiscoveryOutcome(
|
||||
@@ -39,24 +40,54 @@ class DiscoveryService(
|
||||
|
||||
val catalog = catalogs.forSource(mood.source)
|
||||
val collection = MoodRepository.songRecords(mood.id)
|
||||
val seedArtists = collection.map { it.artist }.filter { it.isNotBlank() }
|
||||
val queries = buildQueries(mood, seedArtists)
|
||||
if (queries.isEmpty()) {
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
return DiscoveryOutcome(0)
|
||||
val artists = artistsByFrequency(collection)
|
||||
val albums = albumsOf(collection)
|
||||
val seeds = DiscoverySeeds(
|
||||
artists = artists,
|
||||
albums = albums,
|
||||
hint = mood.query,
|
||||
genres = mood.genres,
|
||||
name = mood.name,
|
||||
)
|
||||
val queries = buildQueries(mood, artists.map { it.name })
|
||||
if (queries.isEmpty() && seeds.isEmpty()) {
|
||||
// A new mood with no hint, no genres and no songs has nothing to
|
||||
// base music on. Leave the slots empty instead of downloading
|
||||
// whatever the mood's name happens to match.
|
||||
log.debug("Skipping discovery for mood {}: no music signal yet", mood.name)
|
||||
return DiscoveryOutcome(0, skipped = true)
|
||||
}
|
||||
|
||||
// Queries are independent; running them together keeps a refresh within
|
||||
// the client's request timeout even when a catalog is slow.
|
||||
// Text queries and the catalog's similarity graph run together: the
|
||||
// graph knows about related artists and album deep cuts, which a title
|
||||
// search cannot express.
|
||||
val results = coroutineScope {
|
||||
queries.map { query ->
|
||||
async(Dispatchers.IO) {
|
||||
runCatching { catalog.search(query, QUERY_LIMIT) }
|
||||
val searches = queries.map { query ->
|
||||
async(Dispatchers.IO) { runCatching { catalog.search(query, QUERY_LIMIT) } }
|
||||
}
|
||||
val expansion = async(Dispatchers.IO) {
|
||||
if (seeds.isEmpty()) {
|
||||
emptyList()
|
||||
} else {
|
||||
runCatching { catalog.discover(seeds, DISCOVERY_LIMIT) }
|
||||
.onFailure {
|
||||
log.debug(
|
||||
"{} expansion failed for mood {}: {}",
|
||||
catalog.source,
|
||||
mood.name,
|
||||
it.message,
|
||||
)
|
||||
}
|
||||
.getOrDefault(emptyList())
|
||||
}
|
||||
}.awaitAll()
|
||||
}
|
||||
searches.awaitAll() to expansion.await()
|
||||
}
|
||||
val failures = results.filter { it.isFailure }
|
||||
if (failures.size == queries.size) {
|
||||
val searches = results.first
|
||||
val discovered = results.second
|
||||
val failures = searches.filter { it.isFailure }
|
||||
|
||||
if (searches.isNotEmpty() && failures.size == searches.size && discovered.isEmpty()) {
|
||||
val message = failures.firstNotNullOfOrNull { it.exceptionOrNull()?.message }
|
||||
?: "Search failed"
|
||||
log.warn("{} discovery failed for mood {}: {}", catalog.source, mood.name, message)
|
||||
@@ -65,9 +96,11 @@ class DiscoveryService(
|
||||
}
|
||||
|
||||
val candidates = LinkedHashMap<String, TrackCandidate>()
|
||||
results.mapNotNull { it.getOrNull() }.flatten().forEach { candidate ->
|
||||
if (isMusicLength(candidate.duration)) {
|
||||
candidates.putIfAbsent(candidate.externalId, candidate)
|
||||
(searches.mapNotNull { it.getOrNull() }.flatten() + discovered).forEach { candidate ->
|
||||
if (!isMusicLength(candidate.duration)) return@forEach
|
||||
val existing = candidates[candidate.externalId]
|
||||
if (existing == null || existing.affinity < candidate.affinity) {
|
||||
candidates[candidate.externalId] = candidate
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,28 +118,33 @@ class DiscoveryService(
|
||||
.toSet()
|
||||
|
||||
val moodText = listOfNotNull(mood.query, mood.description, mood.name).joinToString(" ")
|
||||
val artistWeights = artists
|
||||
.associate { matchKey(it.name) to it.songs.toDouble() }
|
||||
.filterKeys { it.isNotEmpty() }
|
||||
val albumKeys = albums
|
||||
.map { matchKey(it.title) }
|
||||
.filter { it.isNotEmpty() }
|
||||
.toSet()
|
||||
|
||||
val upserts = candidates.values
|
||||
.filterNot { it.externalId in excluded }
|
||||
.map { candidate ->
|
||||
candidate to scoreCandidate(candidate, seedArtists, moodText)
|
||||
}
|
||||
.sortedWith(
|
||||
compareByDescending<Pair<TrackCandidate, Double>> { it.second }
|
||||
.thenBy { it.first.title },
|
||||
val ranked = rankCandidates(
|
||||
candidates.values
|
||||
.filterNot { it.externalId in excluded }
|
||||
.map { candidate ->
|
||||
candidate to scoreCandidate(candidate, artistWeights, albumKeys, moodText)
|
||||
},
|
||||
CANDIDATE_POOL,
|
||||
)
|
||||
val upserts = ranked.map { candidate ->
|
||||
RecommendationUpsert(
|
||||
source = mood.source,
|
||||
externalId = candidate.externalId,
|
||||
title = candidate.title,
|
||||
artist = candidate.artist,
|
||||
album = candidate.album,
|
||||
duration = candidate.duration,
|
||||
thumbnail = candidate.thumbnail,
|
||||
)
|
||||
.take(CANDIDATE_POOL)
|
||||
.map { (candidate, _) ->
|
||||
RecommendationUpsert(
|
||||
source = mood.source,
|
||||
externalId = candidate.externalId,
|
||||
title = candidate.title,
|
||||
artist = candidate.artist,
|
||||
album = candidate.album,
|
||||
duration = candidate.duration,
|
||||
thumbnail = candidate.thumbnail,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (upserts.isEmpty()) {
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
@@ -121,12 +159,22 @@ class DiscoveryService(
|
||||
CANDIDATE_POOL,
|
||||
)
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
log.info("{} discovery for mood {} stored {} candidates", catalog.source, mood.name, stored)
|
||||
log.info(
|
||||
"{} discovery for mood {} stored {} candidates from {} searches and {} expanded hits",
|
||||
catalog.source,
|
||||
mood.name,
|
||||
stored,
|
||||
queries.size,
|
||||
discovered.size,
|
||||
)
|
||||
return DiscoveryOutcome(stored = stored)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val QUERY_LIMIT = 10
|
||||
|
||||
/** The graph expands far more than a single search page does. */
|
||||
const val DISCOVERY_LIMIT = 200
|
||||
const val CANDIDATE_POOL = 60
|
||||
}
|
||||
}
|
||||
@@ -144,28 +192,96 @@ internal fun isMusicLength(seconds: Int?): Boolean =
|
||||
internal fun buildQueries(mood: MoodRecord, seedArtists: List<String>): List<String> {
|
||||
val queries = LinkedHashSet<String>()
|
||||
mood.query?.trim()?.takeIf { it.isNotEmpty() }?.let(queries::add)
|
||||
seedArtists.map { it.trim() }.filter { it.isNotEmpty() }.distinct().take(2).forEach {
|
||||
// The collection is the strongest signal, so its artists fill the slots
|
||||
// before genres do. They arrive ordered by how many songs the mood holds.
|
||||
seedArtists.map { it.trim() }.filter { it.isNotEmpty() }.distinct().take(3).forEach {
|
||||
queries += it
|
||||
}
|
||||
mood.genres.map { it.trim() }.filter { it.isNotEmpty() }.take(2).forEach {
|
||||
queries += "$it mix"
|
||||
}
|
||||
if (queries.isEmpty()) queries += mood.name
|
||||
return queries.distinctBy { it.lowercase() }.take(4)
|
||||
}
|
||||
|
||||
/** Collection artists, most represented first. */
|
||||
internal fun artistsByFrequency(collection: List<SongRecord>): List<SeedArtist> =
|
||||
collection
|
||||
.map { it.artist.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
.groupingBy { it }
|
||||
.eachCount()
|
||||
.entries
|
||||
.sortedWith(
|
||||
compareByDescending<Map.Entry<String, Int>> { it.value }.thenBy { it.key.lowercase() },
|
||||
)
|
||||
.map { SeedArtist(it.key, it.value) }
|
||||
|
||||
/** Distinct albums of the collection, deduplicated loosely. */
|
||||
internal fun albumsOf(collection: List<SongRecord>): List<SeedAlbum> =
|
||||
collection.mapNotNull { song ->
|
||||
val album = song.album?.trim()?.takeIf { it.isNotEmpty() } ?: return@mapNotNull null
|
||||
SeedAlbum(album, song.artist)
|
||||
}.distinctBy { "${matchKey(it.title)}|${matchKey(it.artist)}" }
|
||||
|
||||
/**
|
||||
* Picks the best [limit] candidates while penalising an artist that keeps
|
||||
* winning: without it, a mood with three songs by one artist would fill every
|
||||
* slot with that artist. Each additional pick by the same artist costs
|
||||
* [ARTIST_REPEAT_PENALTY], so a related artist eventually takes a slot.
|
||||
*/
|
||||
internal fun rankCandidates(
|
||||
scored: List<Pair<TrackCandidate, Double>>,
|
||||
limit: Int,
|
||||
): List<TrackCandidate> {
|
||||
val remaining = scored.toMutableList()
|
||||
val chosen = ArrayList<TrackCandidate>(limit.coerceAtMost(remaining.size))
|
||||
val perArtist = HashMap<String, Int>()
|
||||
|
||||
while (chosen.size < limit && remaining.isNotEmpty()) {
|
||||
var bestIndex = 0
|
||||
var bestScore = Double.NEGATIVE_INFINITY
|
||||
for (index in remaining.indices) {
|
||||
val candidate = remaining[index].first
|
||||
val picked = perArtist[matchKey(candidate.artist)] ?: 0
|
||||
val adjusted = remaining[index].second - ARTIST_REPEAT_PENALTY * picked
|
||||
if (adjusted > bestScore) {
|
||||
bestScore = adjusted
|
||||
bestIndex = index
|
||||
}
|
||||
}
|
||||
val candidate = remaining.removeAt(bestIndex).first
|
||||
chosen += candidate
|
||||
val key = matchKey(candidate.artist)
|
||||
perArtist[key] = (perArtist[key] ?: 0) + 1
|
||||
}
|
||||
return chosen
|
||||
}
|
||||
|
||||
/**
|
||||
* Ranks a candidate against the whole collection:
|
||||
*
|
||||
* - an artist already in the mood is the strongest signal, and an artist with
|
||||
* more songs in the mood ranks higher;
|
||||
* - another track from an album already in the mood is a strong signal;
|
||||
* - a catalog-supplied [TrackCandidate.affinity] (related artist, top track)
|
||||
* is added as-is;
|
||||
* - the mood's own words in the title are a tie-breaker.
|
||||
*/
|
||||
internal fun scoreCandidate(
|
||||
candidate: TrackCandidate,
|
||||
seedArtists: List<String>,
|
||||
artistWeights: Map<String, Double>,
|
||||
albumKeys: Set<String>,
|
||||
moodText: String,
|
||||
): Double {
|
||||
var score = 0.0
|
||||
var score = candidate.affinity
|
||||
val artist = matchKey(candidate.artist)
|
||||
val seedKeys = seedArtists.map { matchKey(it) }.filter { it.isNotEmpty() }
|
||||
val exact = artistWeights[artist]
|
||||
when {
|
||||
artist.isNotEmpty() && artist in seedKeys -> score += 3.0
|
||||
artist.isNotEmpty() && seedKeys.any { artist.contains(it) || it.contains(artist) } -> score += 1.5
|
||||
exact != null -> score += 3.0 + (0.5 * (exact - 1)).coerceAtMost(1.5)
|
||||
artist.isNotEmpty() && artistWeights.keys.any { artistMatches(artist, it) } -> score += 1.5
|
||||
}
|
||||
val album = matchKey(candidate.album.orEmpty())
|
||||
if (album.isNotEmpty() && album in albumKeys) score += 1.0
|
||||
val text = matchKey(moodText)
|
||||
if (text.isNotEmpty() && matchKey(candidate.title).contains(text)) score += 1.0
|
||||
if ((candidate.duration ?: 0) > 8 * 60) score -= 1.0
|
||||
@@ -198,3 +314,4 @@ internal fun artistMatches(a: String, b: String): Boolean {
|
||||
|
||||
internal const val MIN_TRACK_SECONDS = 30
|
||||
internal const val MAX_TRACK_SECONDS = 600
|
||||
internal const val ARTIST_REPEAT_PENALTY = 1.0
|
||||
Reference in new issue
Block a user