190 lines
4.9 KiB
Go
190 lines
4.9 KiB
Go
// Package text provides the normalization and fuzzy-matching helpers shared by
|
|
// the matcher and library packages. It is adapted from LastRecc's text package
|
|
// so that the same titles match across both tools.
|
|
package text
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"unicode"
|
|
)
|
|
|
|
var (
|
|
parenRe = regexp.MustCompile(`\([^)]*\)|\[[^\]]*\]`)
|
|
featRe = regexp.MustCompile(`(?i)\b(feat\.?|ft\.?|featuring|with)\b.*$`)
|
|
albumSuffixRe = regexp.MustCompile(`(?i)\s+-\s+(single|ep|album)$`)
|
|
trailingBracket = regexp.MustCompile(`(?i)\s*[\(\[]([^\)\]]*)[\)\]]\s*$`)
|
|
descriptiveRe = regexp.MustCompile(`(?i)\s+-\s+(deluxe|deluxe edition|expanded|expanded edition|remastered|remastered edition|bonus track version|special edition)$`)
|
|
noisePhrases = []string{
|
|
"live", "concert", "greatest hits", "best of", "compilation",
|
|
"anthology", "collection", "essential", "hits", "remix", "remixes",
|
|
"tribute", "karaoke", "instrumental", "cover", "edits",
|
|
"концерт", "живой", "сборник", "лучшее", "хиты",
|
|
"антология", "коллекция", "избранное", "ремикс", "караоке",
|
|
"инструментал", "дискография", "трибьют", "кавер",
|
|
}
|
|
)
|
|
|
|
// Normalize lowercases a string, drops parentheses/brackets, feature credits
|
|
// and punctuation, and collapses whitespace.
|
|
func Normalize(value string) string {
|
|
value = strings.ToLower(value)
|
|
value = parenRe.ReplaceAllString(value, " ")
|
|
value = featRe.ReplaceAllString(value, " ")
|
|
var b strings.Builder
|
|
space := true
|
|
for _, r := range value {
|
|
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
|
b.WriteRune(r)
|
|
space = false
|
|
} else if !space {
|
|
b.WriteRune(' ')
|
|
space = true
|
|
}
|
|
}
|
|
return strings.TrimSpace(b.String())
|
|
}
|
|
|
|
// CleanAlbumTitle strips Apple's " - Single" / " - EP" / " - Album" suffixes.
|
|
func CleanAlbumTitle(value string) string {
|
|
return strings.TrimSpace(albumSuffixRe.ReplaceAllString(strings.TrimSpace(value), ""))
|
|
}
|
|
|
|
// SearchTitle additionally strips trailing parenthetical and edition suffixes
|
|
// such as "(Deluxe)", "(Remastered)" or " - Deluxe Edition". It is used to
|
|
// build Qobuz search queries; matching still normalizes punctuation separately.
|
|
func SearchTitle(value string) string {
|
|
s := CleanAlbumTitle(value)
|
|
for {
|
|
trimmed := trailingBracket.ReplaceAllString(s, "")
|
|
trimmed = descriptiveRe.ReplaceAllString(trimmed, "")
|
|
trimmed = strings.TrimSpace(trimmed)
|
|
if trimmed == s || trimmed == "" {
|
|
break
|
|
}
|
|
s = trimmed
|
|
}
|
|
return s
|
|
}
|
|
|
|
// TrackKey builds a normalized artist::title key.
|
|
func TrackKey(artist, title string) string {
|
|
return Normalize(artist) + "::" + Normalize(title)
|
|
}
|
|
|
|
// AlbumKey builds a normalized artist::album key.
|
|
func AlbumKey(artist, album string) string {
|
|
return Normalize(artist) + "::" + Normalize(album)
|
|
}
|
|
|
|
// Similarity returns a normalized Levenshtein similarity in [0, 1].
|
|
func Similarity(a, b string) float64 {
|
|
a = Normalize(a)
|
|
b = Normalize(b)
|
|
if a == "" && b == "" {
|
|
return 1
|
|
}
|
|
if a == "" || b == "" {
|
|
return 0
|
|
}
|
|
if a == b {
|
|
return 1
|
|
}
|
|
distance := levenshtein([]rune(a), []rune(b))
|
|
maxLen := len([]rune(a))
|
|
if len([]rune(b)) > maxLen {
|
|
maxLen = len([]rune(b))
|
|
}
|
|
if maxLen == 0 {
|
|
return 1
|
|
}
|
|
return 1 - float64(distance)/float64(maxLen)
|
|
}
|
|
|
|
func levenshtein(a, b []rune) int {
|
|
if len(a) == 0 {
|
|
return len(b)
|
|
}
|
|
if len(b) == 0 {
|
|
return len(a)
|
|
}
|
|
prev := make([]int, len(b)+1)
|
|
for j := range prev {
|
|
prev[j] = j
|
|
}
|
|
for i := 1; i <= len(a); i++ {
|
|
cur := make([]int, len(b)+1)
|
|
cur[0] = i
|
|
for j := 1; j <= len(b); j++ {
|
|
cost := 1
|
|
if a[i-1] == b[j-1] {
|
|
cost = 0
|
|
}
|
|
cur[j] = min3(cur[j-1]+1, prev[j]+1, prev[j-1]+cost)
|
|
}
|
|
prev = cur
|
|
}
|
|
return prev[len(b)]
|
|
}
|
|
|
|
func min3(a, b, c int) int {
|
|
if a < b {
|
|
if a < c {
|
|
return a
|
|
}
|
|
return c
|
|
}
|
|
if b < c {
|
|
return b
|
|
}
|
|
return c
|
|
}
|
|
|
|
// IsNoisyAlbum reports whether an album title looks like a compilation, live
|
|
// album, remix collection, etc.
|
|
func IsNoisyAlbum(name string) bool {
|
|
lower := strings.ToLower(name)
|
|
replacer := strings.NewReplacer("(", " ", ")", " ", "[", " ", "]", " ", "-", " ", "—", " ")
|
|
padded := " " + replacer.Replace(lower) + " "
|
|
for _, phrase := range noisePhrases {
|
|
if strings.Contains(padded, " "+phrase) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Slug converts a playlist name into a safe file name stem.
|
|
func Slug(value string) string {
|
|
value = strings.ToLower(strings.TrimSpace(value))
|
|
var b strings.Builder
|
|
lastDash := true
|
|
for _, r := range value {
|
|
if unicode.IsLetter(r) || unicode.IsDigit(r) {
|
|
b.WriteRune(r)
|
|
lastDash = false
|
|
} else if !lastDash {
|
|
b.WriteRune('-')
|
|
lastDash = true
|
|
}
|
|
}
|
|
out := strings.Trim(b.String(), "-")
|
|
if out == "" {
|
|
out = "playlist"
|
|
}
|
|
if len([]rune(out)) > 80 {
|
|
out = string([]rune(out)[:80])
|
|
out = strings.TrimRight(out, "-")
|
|
}
|
|
return out
|
|
}
|
|
|
|
// FormatDuration renders seconds as m:ss.
|
|
func FormatDuration(seconds int) string {
|
|
if seconds <= 0 {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf("%d:%02d", seconds/60, seconds%60)
|
|
}
|