Skip to content

Commit 7703b18

Browse files
committed
improve the default sort name generation a bit
1 parent eb06850 commit 7703b18

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

.pre-commit-config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ repos:
5454
name: ✅ Check code for common misspellings
5555
language: system
5656
types: [text]
57+
args:
58+
- --ignore-words-list=ue
59+
- --skip="./.*,*.csv,*.json,*.ambr"
60+
- --quiet-level=2
5761
entry: scripts/run-in-env.sh codespell
5862
- id: detect-private-key
5963
name: 🕵️ Detect Private Keys

music_assistant_models/helpers.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from asyncio import Task
1010
from types import MethodType
1111
from typing import Any
12+
from unicodedata import combining, normalize
1213
from uuid import UUID
1314

1415
from music_assistant_models.enums import MediaType
@@ -59,9 +60,25 @@ def get_serializable_value(obj: Any, raise_unhandled: bool = False) -> Any:
5960
return obj
6061

6162

63+
_LATIN = "ä æ ǽ đ ð ƒ ħ ı ł ø ǿ ö œ ß ŧ ü " # noqa: RUF001
64+
_ASCII = "ae ae ae d d f h i l o o oe oe ss t ue"
65+
_OUTLIERS = str.maketrans(dict(zip(_LATIN.split(), _ASCII.split(), strict=False)))
66+
67+
68+
def remove_diacritics(input_str: str) -> str:
69+
"""Remove diacritics from string."""
70+
return "".join(
71+
c for c in normalize("NFD", input_str.lower().translate(_OUTLIERS)) if not combining(c)
72+
)
73+
74+
6275
def create_sort_name(input_str: str) -> str:
6376
"""Create (basic/simple) sort name/title from string."""
64-
input_str = input_str.lower().strip()
77+
input_str = remove_diacritics(input_str.lower().strip())
78+
while input_str.startswith(
79+
(",", ".", ":", "!", "?", "(", "[", "{", "<", ">", ")", "]", "}", "/", "`", "'", '"')
80+
):
81+
input_str = input_str[1:]
6582
for item in ["the ", "de ", "les ", "dj ", "las ", "los ", "le ", "la ", "el ", "a ", "an "]:
6683
if input_str.startswith(item):
6784
input_str = input_str.replace(item, "", 1) + f", {item}"

0 commit comments

Comments
 (0)