Skip to content

Commit

Permalink
Add custom function for title case to handle apostrophe
Browse files Browse the repository at this point in the history
  • Loading branch information
zytact committed Nov 5, 2024
1 parent c6c17bf commit 0a6a9f3
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion picard/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,29 @@ def _customize_metadata(self):

@staticmethod
def _genres_to_metadata(genres, limit=None, minusage=0, filters='', join_with=None):
def titlize(text):
"""Converts text to title case using custom rules.
Capitalizes the first character of each word while converting remaining
characters to lowercase. Handles contractions properly by keeping the
apostrophe intact.
Args:
text (str): The input string to be converted to title case.
Returns:
str: The input text converted to title case format.
Examples:
>>> titlize("children's music")
'Children's Music'
>>> titlize("blues")
"Blues"
"""
return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
lambda m: m.group(0)[0].upper() + m.group(0)[1:].lower(),
text)

if limit is not None and limit < 1:
return []

Expand All @@ -335,7 +358,7 @@ def _genres_to_metadata(genres, limit=None, minusage=0, filters='', join_with=No

# Find most common genres
most_common_genres = genres.most_common(limit)
genres_list = [name.title() for name, _count in most_common_genres]
genres_list = [titlize(name) for name, _count in most_common_genres]
genres_list.sort()

# And generate the genre metadata tag
Expand Down

0 comments on commit 0a6a9f3

Please sign in to comment.