remove functions from reference_playlists_to_library.py move to serverlibary
This commit is contained in:
1 parent
0a6457abe0
commit
f5d7962aba
3 files changed
+57
-45
No files matched your search
@@ -5,7 +5,8 @@ import re
|
||||
import logging
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
from difflib import SequenceMatcher
|
||||
|
||||
from serverlibrary import words, word_similarity, score_match_jaccard, score_match_fuzzy, combined_score
|
||||
|
||||
WORKDIR = Path("/data/navidrome/music/")
|
||||
PLAYLIST_DIR = WORKDIR / "playlists"
|
||||
@@ -28,44 +29,6 @@ class CustomLogger(logging.Logger):
|
||||
logging.setLoggerClass(CustomLogger)
|
||||
logger: CustomLogger = logging.getLogger(__name__) # type: ignore[assignment]
|
||||
|
||||
|
||||
def words(text):
|
||||
return set(filter(None, re.split(r"[^a-z0-9]+", text.lower())))
|
||||
|
||||
|
||||
def word_similarity(a: str, b: str) -> float:
|
||||
return SequenceMatcher(None, a, b).ratio()
|
||||
|
||||
|
||||
def score_match_jaccard(search_words, candidate_words):
|
||||
intersection = search_words & candidate_words
|
||||
union = search_words | candidate_words
|
||||
|
||||
if not union:
|
||||
return 0.0
|
||||
|
||||
return len(intersection) / len(union)
|
||||
|
||||
|
||||
def score_match_fuzzy(search_words, candidate_words):
|
||||
total = 0.0
|
||||
|
||||
for sw in search_words:
|
||||
best = 0.0
|
||||
|
||||
for cw in candidate_words:
|
||||
best = max(best, word_similarity(sw, cw))
|
||||
|
||||
total += best
|
||||
|
||||
# logger.trace(f"Total: {total}/{len(search_words)}")
|
||||
return total / len(search_words)
|
||||
|
||||
|
||||
def combined_score(score_fuzzy: float, score_jaccard: float) -> float:
|
||||
return score_fuzzy * weight_fuzzy + score_jaccard * weight_jaccard
|
||||
|
||||
|
||||
def find_folder(directory: Path, folder: str) -> list[Path]:
|
||||
matches: list[tuple[Path, float]] = []
|
||||
search_words = words(folder)
|
||||
@@ -78,7 +41,7 @@ def find_folder(directory: Path, folder: str) -> list[Path]:
|
||||
|
||||
score_fuzzy = score_match_fuzzy(search_words, candidate_words)
|
||||
score_jaccard = score_match_jaccard(search_words, candidate_words)
|
||||
score = combined_score(score_fuzzy, score_jaccard)
|
||||
score = combined_score(score_fuzzy, score_jaccard, weight_fuzzy, weight_jaccard)
|
||||
logger.trace(f"Candidate: {score:.2f}:{candidate_words}")
|
||||
if score > folder_score_threshold:
|
||||
matches.append((candidate, score))
|
||||
@@ -216,7 +179,7 @@ def find_replacement(old_path: Path):
|
||||
|
||||
score_fuzzy = score_match_fuzzy(search_words, candidate_words)
|
||||
score_jaccard = score_match_jaccard(search_words, candidate_words)
|
||||
score = combined_score(score_fuzzy, score_jaccard)
|
||||
score = combined_score(score_fuzzy, score_jaccard, weight_fuzzy, weight_jaccard)
|
||||
logger.trace(
|
||||
f"{filename_structure} Words: {candidate_words} {score:.2f} / {best_score:.2f}, "
|
||||
f"{weight_fuzzy:.2} * {score_fuzzy:.2f} + {weight_jaccard:.2} * {score_jaccard:.2f} "
|
||||
|
||||
Reference in new issue
Block a user