Initial commit: LastRecc discovery feed and weekly engine
This commit is contained in:
40 files changed
+6972
No files matched your search
@@ -0,0 +1,136 @@
|
||||
package text
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var (
|
||||
parenRe = regexp.MustCompile(`\([^)]*\)|\[[^\]]*\]`)
|
||||
featRe = regexp.MustCompile(`(?i)\b(feat\.?|ft\.?|featuring|with)\b.*$`)
|
||||
noisePhrases = []string{
|
||||
"live", "concert", "greatest hits", "best of", "compilation",
|
||||
"anthology", "collection", "essential", "hits", "remix", "remixes",
|
||||
"tribute", "karaoke", "instrumental", "cover", "edits",
|
||||
"концерт", "живой", "сборник", "лучшее", "хиты",
|
||||
"антология", "коллекция", "избранное", "ремикс", "караоке",
|
||||
"инструментал", "дискография", "трибьют", "кавер",
|
||||
}
|
||||
)
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
func TrackKey(artist, title string) string {
|
||||
return Normalize(artist) + "::" + Normalize(title)
|
||||
}
|
||||
|
||||
func AlbumKey(artist, album string) string {
|
||||
return Normalize(artist) + "::" + Normalize(album)
|
||||
}
|
||||
|
||||
func Similarity(a, b string) float64 {
|
||||
a = Normalize(a)
|
||||
b = Normalize(b)
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func WeekIndex(t time.Time) int {
|
||||
year, week := t.ISOWeek()
|
||||
return year*100 + week
|
||||
}
|
||||
|
||||
func WeekLabel(t time.Time) string {
|
||||
year, week := t.ISOWeek()
|
||||
return fmt.Sprintf("%d-W%02d", year, week)
|
||||
}
|
||||
|
||||
func FormatDuration(seconds int) string {
|
||||
if seconds <= 0 {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%d:%02d", seconds/60, seconds%60)
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package text
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeCyrillic(t *testing.T) {
|
||||
got := Normalize("Кино — Звезда по имени Солнце")
|
||||
want := "кино звезда по имени солнце"
|
||||
if got != want {
|
||||
t.Fatalf("Normalize() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeFeaturesAndBrackets(t *testing.T) {
|
||||
got := Normalize("Someday (Remastered 2011) [Live] feat. Someone")
|
||||
if got != "someday" {
|
||||
t.Fatalf("Normalize() = %q, want %q", got, "someday")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoisyAlbums(t *testing.T) {
|
||||
cases := map[string]bool{
|
||||
"Крылья": false,
|
||||
"Акустический концерт": true,
|
||||
"Квартирные концерты в Москве": true,
|
||||
"Полная Дискография": true,
|
||||
"Greatest Hits": true,
|
||||
"Live at Leeds": true,
|
||||
"Alive": false,
|
||||
"Communiqué": false,
|
||||
}
|
||||
for name, want := range cases {
|
||||
if got := IsNoisyAlbum(name); got != want {
|
||||
t.Errorf("IsNoisyAlbum(%q) = %v, want %v", name, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSimilarity(t *testing.T) {
|
||||
if Similarity("Кино", "кино") != 1 {
|
||||
t.Fatal("expected case-insensitive exact match")
|
||||
}
|
||||
if Similarity("Someday", "Someday (Remastered)") < 0.9 {
|
||||
t.Fatal("expected high similarity after normalization")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user