Skip to content

Commit 36fcb59

Browse files
committed
Handle single letter + apostrophe better
In the special case of a single letter plus apostrophe leading a word, often we're either looking at a non-English word with abbreviated article (usually starting with a consonant) or a name (usually starting with a vowel). We make an educated guess and assume the former should be lower case and the latter upper case. Handles issue ppannuto#5.
1 parent f78cfc7 commit 36fcb59

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

titlecase/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ def titlecase(text, callback=None):
6161
word = word.lower()
6262

6363
if APOS_SECOND.match(word):
64-
word = word.replace(word[0], word[0].upper())
65-
word = word.replace(word[2], word[2].upper())
64+
if len(word[0]) == 1 and word[0] not in 'aeiouAEIOU':
65+
word = word[0].lower() + word[1] + word[2].upper() + word[3:]
66+
else:
67+
word = word[0].upper() + word[1] + word[2].upper() + word[3:]
6668
tc_line.append(word)
6769
continue
6870
if INLINE_PERIOD.search(word) or UC_ELSEWHERE.match(word):

titlecase/tests.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@
155155
(
156156
"foo bar 5th st",
157157
"Foo Bar 5th St",
158+
),
159+
(
160+
"l'grange l'grange l'Grange l'Grange",
161+
"l'Grange l'Grange l'Grange l'Grange",
162+
),
163+
(
164+
"L'grange L'grange L'Grange L'Grange",
165+
"l'Grange l'Grange l'Grange l'Grange",
166+
),
167+
(
168+
"l'GranGe",
169+
"l'GranGe",
170+
),
171+
(
172+
"o'grange O'grange o'Grange O'Grange",
173+
"O'Grange O'Grange O'Grange O'Grange",
174+
),
175+
(
176+
"O'GranGe",
177+
"O'GranGe",
158178
)
159179
)
160180

0 commit comments

Comments
 (0)