getAttributes() as $attribute) { if ($attribute instanceof AttributeDTO) { $fields[strtolower($attribute->getName())] = trim($attribute->getValue()); } } $domain = MediaType::normalize($domain ?? $fields['media_type'] ?? ''); if (!in_array($domain, [MediaType::BOOK, MediaType::MUSIC], true)) { return null; } $title = $this->text($domain === MediaType::MUSIC ? ($fields['canonical_title'] ?? $product->getName()) : $product->getName()); $needle = $this->text($query); if ($title === '' || $needle === '') { return 0; } if ($domain === MediaType::BOOK && ctype_digit($needle) && $title !== $needle && !str_starts_with($title, $needle.' ')) { return 40; } $tokens = array_unique(explode(' ', $needle)); $nameTokens = array_unique(explode(' ', $title)); $coverage = count(array_intersect($tokens, $nameTokens))/count($tokens); // Rich unrelated records must never outrank a coherent title. if ($coverage < 0.6 && !str_contains($title, $needle)) { return (int)round(60*$coverage); } $relevance = $title === $needle ? 160 : (str_starts_with($title, $needle) ? 145 : (str_contains($title, $needle) ? 130 : (int)round(110*$coverage))); $quality = 0; if ($domain === MediaType::BOOK) { $quality += ($fields['author'] ?? $fields['authors'] ?? '') !== '' ? 35 : 0; foreach (['isbn_13','isbn_10','isbn'] as $key) { if (IsbnValidator::normalize($fields[$key] ?? null) !== null) { $quality += 28; break; } } $quality += ($fields['publisher'] ?? '') !== '' ? 16 : 0; $quality += preg_match('/^\d{4}(?:-\d{2})?(?:-\d{2})?$/D', $fields['published_date'] ?? '') ? 10 : 0; $quality += preg_match('/^[1-9]\d{0,4}$/D', $fields['page_count'] ?? '') ? 8 : 0; $quality += ($fields['language'] ?? '') !== '' ? 6 : 0; } else { $quality += ($fields['artist'] ?? '') !== '' ? 20 : 0; $quality += strtolower($fields['status'] ?? '') === 'official' ? 15 : 0; $quality += ($fields['label'] ?? '') !== '' ? 8 : 0; $quality += ($fields['format'] ?? '') !== '' ? 8 : 0; $quality += strtolower($fields['release_group_type'] ?? '') === 'album' ? 12 : 0; $date = $fields['release_date'] ?? ''; if (preg_match('/^(\d{4})(?:-\d{2})?(?:-\d{2})?$/D', $date, $match)) { $quality += 10 + min(20, max(0, (int)gmdate('Y')-(int)$match[1])); } if (is_numeric($fields['source_relevance_score'] ?? null)) { $quality += (int)round(max(0,min(100,(float)$fields['source_relevance_score']))*0.15); } // Native release identity across coherent editions is a bounded // work signal, independent of artist/provider names and hit volume. if ($title === $needle && ($fields['release_group_id'] ?? '') !== '') { $editions = []; foreach ($peers as $peer) { if (!$peer instanceof ProductDTO) { continue; } $peerFields = []; foreach ($peer->getAttributes() as $attribute) { if ($attribute instanceof AttributeDTO) { $peerFields[$attribute->getName()] = $attribute->getValue(); } } if (($peerFields['release_group_id'] ?? '') === $fields['release_group_id'] && $this->text($peerFields['canonical_title'] ?? '') === $needle) { $editions[$peer->getId()] = true; } } $quality += min(30, (int)round(log(1+count($editions), 2)*6)); } } return $relevance + $quality; } private function text(string $value): string { $value = function_exists('mb_strtolower') ? mb_strtolower(trim($value), 'UTF-8') : strtolower(trim($value)); return trim((string)preg_replace('/\s+/u', ' ', (string)preg_replace('/[^\p{L}\p{N}]+/u', ' ', $value))); } }