jwjacobson / jazztunes

a jazz repertoire management app
https://jazztunes.org
GNU General Public License v3.0
3 stars 1 forks source link

Add validators to text-input forms for capitalization/title case #105

Closed jwjacobson closed 6 months ago

jwjacobson commented 8 months ago

Key and Other Keys fields already do this, the relevant ones are title and composer. Title might be a little tricky as it should be smart title case, e.g. "Coming on the Hudson" not "Coming On The Hudson." Is there maybe some kind of built-in for that or does it need to be hardcoded?

bbelderbos commented 8 months ago

Nope does not seem covered by Standard Library as is:

>>> a = "coming on the hudson"
>>> a.title()
'Coming On The Hudson'
>>> a.capitalize()
'Coming on the hudson'
>>> import string
>>> string.capwords(a)
'Coming On The Hudson'

So you probably want to write a function to ignore stopwords like "on" and "the", for example:

def title_case(s):
    # List of words to keep in lowercase (unless they are the first or last word)
    lowercase_words = ['a', 'an', 'the', 'at', 'by', 'for', 'in', 'of', 'on', 'to', 'up', 'and', 'as', 'but', 'or', 'nor']

    words = s.split()

    # Don't capitalize the first and last words, capitalize the rest
    final_words = [words[0].capitalize()]
    final_words += [word if word in lowercase_words else word.capitalize() for word in words[1:-1]]
    final_words += [words[-1].capitalize()]

    return ' '.join(final_words)

example_title = "coming on the hudson"
title_cased = title_case(example_title)
print(title_cased)
jwjacobson commented 6 months ago

Decided this is a non-issue, let users decide their own capitalization