This commit is contained in:
Broque Thomas 2025-07-24 17:03:30 -07:00
parent fda2b92724
commit 44c262c7b4

View file

@ -22,21 +22,17 @@ class MatchResult:
class MusicMatchingEngine: class MusicMatchingEngine:
def __init__(self): def __init__(self):
# More comprehensive patterns to strip extra info from titles # The order of these patterns is important. More general patterns go first.
self.title_patterns = [ self.title_patterns = [
# NEW: General patterns to remove all content in brackets/parentheses first # General patterns to remove all content in brackets/parentheses
r'\(.*\)', r'\(.*\)',
r'\[.*\]', r'\[.*\]',
# Patterns after a hyphen # General pattern to remove everything after a hyphen
r'-\s*single version', r'\s-\s.*',
r'-\s*remaster.*', # Patterns to remove featuring artists from the title itself
r'-\s*live.*', r'\sfeat\.?.*',
r'-\s*remix', r'\sft\.?.*',
r'-\s*radio edit', r'\sfeaturing.*'
# Patterns in the open title string (not in brackets)
r'\s+feat\.?.*',
r'\s+ft\.?.*',
r'\s+featuring.*'
] ]
self.artist_patterns = [ self.artist_patterns = [
@ -56,16 +52,10 @@ class MusicMatchingEngine:
if not text: if not text:
return "" return ""
# Transliterate Unicode characters (e.g., ñ -> n, é -> e) to ASCII
text = unidecode(text) text = unidecode(text)
# Convert to lowercase
text = text.lower() text = text.lower()
# Keep alphanumeric, spaces, and hyphens, but remove other punctuation like '.' or ','
# Remove specific punctuation but keep alphanumeric and spaces
text = re.sub(r'[^\w\s-]', '', text) text = re.sub(r'[^\w\s-]', '', text)
# Collapse multiple spaces into one
text = re.sub(r'\s+', ' ', text).strip() text = re.sub(r'\s+', ' ', text).strip()
return text return text
@ -104,56 +94,52 @@ class MusicMatchingEngine:
if abs(duration1 - duration2) <= 5000: if abs(duration1 - duration2) <= 5000:
return 1.0 return 1.0
# Penalize larger differences
diff_ratio = abs(duration1 - duration2) / max(duration1, duration2) diff_ratio = abs(duration1 - duration2) / max(duration1, duration2)
return max(0, 1.0 - diff_ratio * 5) # Scale penalty return max(0, 1.0 - diff_ratio * 5)
def calculate_match_confidence(self, spotify_track: SpotifyTrack, plex_track: PlexTrackInfo) -> Tuple[float, str]: def calculate_match_confidence(self, spotify_track: SpotifyTrack, plex_track: PlexTrackInfo) -> Tuple[float, str]:
"""Calculates a confidence score for a potential match with weighted factors.""" """Calculates a confidence score for a potential match with a more robust, prioritized logic."""
spotify_title_cleaned = self.clean_title(spotify_track.name) spotify_title_cleaned = self.clean_title(spotify_track.name)
plex_title_cleaned = self.clean_title(plex_track.title) plex_title_cleaned = self.clean_title(plex_track.title)
# --- Enhanced Artist Scoring --- # --- Artist Scoring ---
spotify_artists_cleaned = [self.clean_artist(a) for a in spotify_track.artists if a] spotify_artists_cleaned = [self.clean_artist(a) for a in spotify_track.artists if a]
plex_artist_cleaned = self.clean_artist(plex_track.artist)
plex_artist_normalized = self.normalize_string(plex_track.artist) plex_artist_normalized = self.normalize_string(plex_track.artist)
best_artist_score = 0.0 best_artist_score = 0.0
for spotify_artist in spotify_artists_cleaned: for spotify_artist in spotify_artists_cleaned:
if spotify_artist in plex_artist_normalized: if spotify_artist and spotify_artist in plex_artist_normalized:
score = 1.0 best_artist_score = 1.0
else: break
score = self.similarity_score(spotify_artist, plex_artist_cleaned) score = self.similarity_score(spotify_artist, self.clean_artist(plex_track.artist))
if score > best_artist_score: if score > best_artist_score:
best_artist_score = score best_artist_score = score
if best_artist_score == 1.0:
break
artist_score = best_artist_score artist_score = best_artist_score
# --- Calculate other scores --- # --- Title and Duration Scoring ---
title_score = self.similarity_score(spotify_title_cleaned, plex_title_cleaned) title_score = self.similarity_score(spotify_title_cleaned, plex_title_cleaned)
duration_score = self.duration_similarity(spotify_track.duration_ms, plex_track.duration if plex_track.duration else 0) duration_score = self.duration_similarity(spotify_track.duration_ms, plex_track.duration if plex_track.duration else 0)
# --- Weighted confidence calculation --- # --- Prioritized Confidence Logic ---
confidence = (title_score * 0.5) + (artist_score * 0.3) + (duration_score * 0.2) # Priority 1: Near-perfect title and artist match is a very strong signal.
if title_score > 0.98 and artist_score > 0.9:
# --- NEW: Add confidence boost for exact title matches --- confidence = 0.98
if spotify_title_cleaned == plex_title_cleaned and len(spotify_title_cleaned) > 0: match_type = "strong_match"
confidence = max(confidence, 0.85) # Boost to at least 0.85 for exact titles # Priority 2: Exact title match, even with a weaker artist match, should have high confidence.
# This helps with short titles like "Girls" or "LIL DEMON".
# Determine match type based on scores elif title_score > 0.98:
if title_score > 0.95 and artist_score > 0.9 and duration_score > 0.9: confidence = 0.90 + (artist_score * 0.05) # Base of 0.9, with a small artist bonus
match_type = "perfect_match" match_type = "exact_title_match"
confidence = max(confidence, 0.98) # Priority 3: High title similarity is still a good indicator.
elif title_score > 0.85 and artist_score > 0.8: elif title_score > 0.9:
confidence = (title_score * 0.6) + (artist_score * 0.3) + (duration_score * 0.1)
match_type = "high_confidence" match_type = "high_confidence"
elif title_score > 0.75: # Default: Standard weighted calculation for all other cases.
match_type = "medium_confidence"
else: else:
match_type = "low_confidence" confidence = (title_score * 0.5) + (artist_score * 0.3) + (duration_score * 0.2)
match_type = "standard_match"
return confidence, match_type return confidence, match_type