Skip to content

Commit

Permalink
bugfix: ensure correct behaviour of apply_transliterations
Browse files Browse the repository at this point in the history
previously, v/a releases with a mixture of ascii and non-ascii artist
names would result in -all- names getting transliterations -- even the
ascii names. this commit avoids this unnecessary redoubling by skipping
the append for names that are already ascii
  • Loading branch information
hejops committed Sep 5, 2024
1 parent 8d449eb commit ceb4095
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions dita/discogs/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,47 +138,49 @@ def apply_transliterations(
transliterations: dict[str, list[str]],
discogs_tags: pd.DataFrame,
) -> pd.DataFrame:
"""Append transliteration to artist column if unambiguous (or if tty input
possible), else return df unchanged."""

def append_transliteration(x):
match = transliterations.get(x.lower())
if match:
return f"{x} ({match[0]})"
return x
"""Append transliteration to artist column.
This only modifies the df if it is possible to apply from
`transliterations` in an unambiguous manner, or if tty input is possible.
Otherwise, the df is returned unchanged.
"""
if (
# 1 transliteration per artist
all(len(x) == 1 for x in transliterations.values())
# all artists have 1 translit
and len(transliterations) == len(set(discogs_tags.artist))
):
discogs_tags.artist = discogs_tags.artist.apply(append_transliteration)
assert all(is_ascii(x) for x in discogs_tags.artist)
# return discogs_tags

elif sys.__stdin__.isatty():
# print(transliterations)
# foo = transliterations.copy()
for native, trans_l in transliterations.items():
if len(trans_l) == 1:
trans = trans_l[0]
elif not trans_l:
# if artist["profile"]:
# eprint(artist["profile"])
print("No transliterations found:")
open_url("https://duckduckgo.com/?t=ffab&q=", native)
trans = input(f"Provide transliteration for {native}: ")
else:
trans: str = select_from_list(trans_l, "Select transliteration")

n_trans = f"{native} ({trans})"
discogs_tags.artist = discogs_tags.artist.apply(
lambda n: n.lower().replace(native, n_trans)
)
def auto_transliterate(name: str) -> str:
match = transliterations.get(name.lower())
if match:
return f"{name} ({match[0]})"
return name

# else:
# raise NotImplementedError
discogs_tags.artist = discogs_tags.artist.apply(auto_transliterate)
assert all(is_ascii(x) for x in discogs_tags.artist)
return discogs_tags

if not sys.__stdin__.isatty():
# raise NotImplementedError
return discogs_tags

def transliterate(name: str) -> str:
if is_ascii(name):
return name
return name.lower().replace(native, f"{native} ({trans})")

for native, trans_l in transliterations.items():
if len(trans_l) == 1:
trans = trans_l[0]
elif not trans_l:
# print("No transliterations found:")
open_url("https://duckduckgo.com/?t=ffab&q=", native)
trans = input(f"Provide transliteration for {native}: ")
else:
trans: str = select_from_list(trans_l, "Select transliteration")

discogs_tags.artist = discogs_tags.artist.apply(transliterate)

return discogs_tags

Expand Down

0 comments on commit ceb4095

Please sign in to comment.