carlosjfcasero: 'Champagne Supernova (OurVinyl Sessions)' is in the library
but the artist page shows it unowned and wishlist cleanup never removes it.
Measured with the real catalogs: Deezer/iTunes title the TRACK with the
qualifier while the library track is bare (the qualifier lives in the album
title) — and _calculate_track_confidence crushed that pair to ~0.17: the
"clean" titles keep parenthetical words, so the length-ratio penalty treats
'Champagne Supernova' vs 'Champagne Supernova (OurVinyl Sessions)' as
different songs. (Also confirmed: the OurVinyl release is absent from
Deezer's discography for the artist, so the standard page's 25-release list
not showing it is the source catalog, not a bug.)
Fix 1 — core.text.title_match.strip_redundant_context_qualifiers: a
parenthetical qualifier whose text appears (word-bounded) in the db track's
ALBUM title — or in the other title — restates release context and is
stripped for a comparison variant scored with its own length guard. Genuine
version markers keep their penalty: '(Live)' on a studio album appears in no
context and still blocks; '(Live)' on 'Live at Wembley' correctly matches —
owning the live album IS owning the live cut. Wired into
_calculate_track_confidence, so every check_track_exists consumer (wishlist
cleanup, discography dedup, repair jobs) benefits.
Fix 2 — the artist-page ownership endpoint's album gate: when album-aware
narrowing eliminates EVERY library candidate (the source's album naming just
doesn't resemble the library's — 'Jillette Johnson | OurVinyl Sessions' vs
'Champagne Supernova (OurVinyl Sessions)' ~0.5), fall back to artist-wide
title matching instead of declaring everything unowned off a failed
album-NAME comparison.
Tests: 8 — the exact reported pair end-to-end through check_track_exists,
word-boundary containment ('live' in 'alive' doesn't count), version-marker
safety both ways, and prefix songs still blocked. 1125 matching/wishlist/
library tests pass.
118 lines
5.2 KiB
Python
118 lines
5.2 KiB
Python
"""Guard against char-level title false positives in track matching.
|
|
|
|
Issue #769: playlist sync matched tracks that aren't in the library to a
|
|
DIFFERENT song by the SAME artist, with high confidence — e.g. "Dani
|
|
California" -> "Californication" (Red Hot Chili Peppers), "Under The Bridge"
|
|
-> "Around the World". The confidence formula is ``0.5*title + 0.5*artist``,
|
|
and a same-artist comparison always yields ``artist = 1.0``, so the title score
|
|
is the only thing that can tell two of an artist's songs apart. But the title
|
|
score is a ``difflib.SequenceMatcher`` character ratio, which over-credits
|
|
unrelated titles that happen to share a long substring ("californi…") or only a
|
|
stopword ("the"): 0.67 and 0.62 respectively. With the flat 0.5 artist term
|
|
that lands at 0.83 / 0.81 — well over the 0.7 sync threshold.
|
|
|
|
``titles_plausibly_same`` adds a cheap word-level sanity check on top of the
|
|
char ratio: accept a pair only when it's near-identical char-wise (so typos and
|
|
punctuation/casing variants — "Beleive"/"Believe", "HUMBLE."/"Humble" — still
|
|
match) OR the two titles share at least one significant (non-stopword) token.
|
|
Two genuinely different songs by the same artist share no content word, so they
|
|
get rejected; the real track is then correctly reported missing.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
# Articles / prepositions / conjunctions only. Deliberately NOT pronouns
|
|
# ("you", "me", "i") — those carry meaning in song titles and dropping them
|
|
# could strip the only shared word from a real match. "the" MUST stay here:
|
|
# without it "Under The Bridge" and "Around the World" would falsely share it.
|
|
_TITLE_STOPWORDS = frozenset({
|
|
"the", "a", "an", "of", "and", "or", "to", "in", "on",
|
|
"for", "with", "at", "by", "from",
|
|
})
|
|
|
|
_TOKEN_RE = re.compile(r"[a-z0-9]+")
|
|
|
|
# Char ratio at/above which two titles are treated as the same regardless of
|
|
# shared words — covers typos, punctuation, casing, accents. Tuned so single-
|
|
# word typos ("Beleive"/"Believe" = 0.857) pass while the #769 false positives
|
|
# ("Dani California"/"Californication" = 0.667) do not.
|
|
_NEAR_IDENTICAL = 0.85
|
|
|
|
|
|
def _content_tokens(text: str) -> set[str]:
|
|
return {t for t in _TOKEN_RE.findall((text or "").lower()) if t not in _TITLE_STOPWORDS}
|
|
|
|
|
|
def titles_plausibly_same(
|
|
title_a: str,
|
|
title_b: str,
|
|
char_similarity: float,
|
|
*,
|
|
near_identical: float = _NEAR_IDENTICAL,
|
|
) -> bool:
|
|
"""Whether two titles could be the same track, given their char similarity.
|
|
|
|
``title_a`` / ``title_b`` should already be normalised/cleaned (lowercased,
|
|
brackets stripped) the same way the caller computed ``char_similarity``.
|
|
|
|
Returns ``True`` when the pair is near-identical char-wise OR shares at
|
|
least one significant (non-stopword) token. Returns ``False`` for two
|
|
titles that are only moderately char-similar and share no content word —
|
|
i.e. different songs the char ratio over-credited (#769)."""
|
|
if char_similarity >= near_identical:
|
|
return True
|
|
ta = _content_tokens(title_a)
|
|
tb = _content_tokens(title_b)
|
|
# Word-overlap is only a reliable "different song" signal when at least one
|
|
# side has 2+ content words — that's the #769 case where the char ratio
|
|
# over-credits a shared substring ("Dani California"/"Californication") or
|
|
# a stopword ("Under The Bridge"/"Around the World"). For single-word
|
|
# titles there's no other word to share, so applying it would wrongly fail
|
|
# legitimate stylized spellings ("Grey"/"Gray", "Tonite"/"Tonight",
|
|
# "Thru"/"Through") that the char ratio rightly accepts. In that case defer
|
|
# to the caller's existing char-similarity floor instead of force-failing.
|
|
if max(len(ta), len(tb)) < 2 or not ta or not tb:
|
|
return True
|
|
return not ta.isdisjoint(tb)
|
|
|
|
|
|
_QUALIFIER_RE = re.compile(r"[\(\[]([^\)\]]*)[\)\]]")
|
|
|
|
|
|
def strip_redundant_context_qualifiers(title: str, *context_texts: str) -> str:
|
|
"""Remove parenthetical/bracket qualifiers that merely restate known context.
|
|
|
|
A qualifier whose text appears (word-bounded) in one of ``context_texts``
|
|
— typically the release's album title, or the other side of a comparison —
|
|
is album context, not a version difference. #808: the wishlist held
|
|
'Champagne Supernova (OurVinyl Sessions)' while the library track was the
|
|
bare 'Champagne Supernova' on the album '… (OurVinyl Sessions)'; the
|
|
qualifier restated the album, but the length-ratio penalty treated the
|
|
pair as different songs and the cleanup never recognised the owned
|
|
edition. Version markers that do NOT appear in any context ('(Live)',
|
|
'(Remix)' on a studio album) are kept, so their mismatch penalty stands.
|
|
"""
|
|
if not title:
|
|
return title
|
|
|
|
contexts = [c.casefold() for c in context_texts if c]
|
|
if not contexts:
|
|
return title
|
|
|
|
def _drop(match: re.Match) -> str:
|
|
inner = match.group(1).strip().casefold()
|
|
if not inner:
|
|
return " "
|
|
pattern = r"\b" + re.escape(inner) + r"\b"
|
|
for ctx in contexts:
|
|
if re.search(pattern, ctx):
|
|
return " "
|
|
return match.group(0)
|
|
|
|
out = _QUALIFIER_RE.sub(_drop, title)
|
|
return re.sub(r"\s+", " ", out).strip()
|
|
|
|
|
|
__all__ = ["titles_plausibly_same", "strip_redundant_context_qualifiers"]
|