Skip to content

Fix casing of O'Brien / d'Artagnan style names in all-caps input - #101

Open
afonsojanu wants to merge 1 commit into
ppannuto:mainfrom
afonsojanu:fix-apos-second-all-caps
Open

Fix casing of O'Brien / d'Artagnan style names in all-caps input#101
afonsojanu wants to merge 1 commit into
ppannuto:mainfrom
afonsojanu:fix-apos-second-all-caps

Conversation

@afonsojanu

Copy link
Copy Markdown

While running some all-caps headlines through titlecase I noticed names with an apostrophe in the second position come out wrong:

>>> titlecase("O'BRIEN'S HOUSE IS FOR SALE")
"O'BRIEN'S House Is for Sale"
>>> titlecase("D'ARTAGNAN AND THE THREE MUSKETEERS")
"d'ARTAGNAN and the Three Musketeers"

Everything else in the sentence gets shouted-caps normalized before recapitalization, but the word itself stays untouched past the second letter.

The APOS_SECOND branch only ever touches the letter right after the apostrophe (word[2]) and appends the rest of the word unchanged. That's fine for mixed-case input, which the library deliberately leaves alone (there's an existing test for l'GranGe staying as-is). But when the whole line is in caps, every other branch in the loop lowercases the word first so recapitalizing produces a normal title-case word, and this branch skips that step, so the tail of the word is left shouting.

Fix downcases the remainder of the word before recapitalizing it, but only when all_caps is true, so the existing mixed-case behavior is untouched:

>>> titlecase("O'BRIEN'S HOUSE IS FOR SALE")
"O'Brien's House Is for Sale"
>>> titlecase("D'ARTAGNAN AND THE THREE MUSKETEERS")
"d'Artagnan and the Three Musketeers"

Added two cases to TEST_DATA covering both the vowel-prefix (O') and consonant-prefix (D') paths through this branch. Confirmed both fail against the old code and pass with the fix; the rest of the suite (including the existing mixed-case apostrophe cases) is unaffected.

When a whole line is shouted in caps, titlecase lowercases each word
before recapitalizing it, except for words caught by the apostrophe
rule (d', l', o' style names). That branch only fixed up the two
letters around the apostrophe and left everything after them exactly
as it came in, so "O'BRIEN'S HOUSE" turned into "O'BRIEN'S House"
instead of "O'Brien's House", and "D'ARTAGNAN" came out as
"d'ARTAGNAN".

Downcase the remainder of the word first when the line is all caps,
same as the other word-handling branches already do, before applying
the apostrophe-specific capitalization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant