fix: improve fuzzy matching for tracks with special formatting

- Lower matching threshold from 60 to 50 for more lenient matching
- Add fallback to trust provider's top result when artist matches well (>=70)
- Helps match tracks with parentheses, brackets, and stylized titles like 'PiLlOwT4lK'
- Provider search already does fuzzy matching, so trust it when artist is correct
This commit is contained in:
2026-02-06 20:16:36 -05:00
parent b366a4b771
commit 3ffa09dcfa

View File

@@ -539,7 +539,8 @@ public class SpotifyTrackMatchingService : BackgroundService
if (results.Count == 0) return null;
var bestMatch = results
// Score all results
var scoredResults = results
.Select(song => new
{
Song = song,
@@ -554,13 +555,27 @@ public class SpotifyTrackMatchingService : BackgroundService
TotalScore = (x.TitleScore * 0.6) + (x.ArtistScore * 0.4)
})
.OrderByDescending(x => x.TotalScore)
.FirstOrDefault();
.ToList();
if (bestMatch != null && bestMatch.TotalScore >= 60)
var bestMatch = scoredResults.FirstOrDefault();
// If we have a good match (50+), use it
if (bestMatch != null && bestMatch.TotalScore >= 50)
{
return bestMatch.Song;
}
// Fallback: If the provider returned results and the top result has decent artist match,
// trust the provider's search algorithm (it already did fuzzy matching)
// This helps with tracks that have features/remixes in parentheses/brackets
// where the provider might format them differently
if (bestMatch != null && bestMatch.ArtistScore >= 70)
{
_logger.LogDebug("Using provider's top result despite low title score (Artist: {ArtistScore}, Title: {TitleScore}): {Title}",
bestMatch.ArtistScore, bestMatch.TitleScore, bestMatch.Song.Title);
return bestMatch.Song;
}
return null;
}
catch