Skip to content

Commit bf1e0a5

Browse files
committed
Add parameter to force capitalization of mixed case strings
1 parent 9d9e83b commit bf1e0a5

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

docs/release_log.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Release Log
44
- Remove "CONSTANTS.suffixes", replaced by "suffix_acronyms" and "suffix_not_acronyms" (#49)
55
- Add "du" to prefixes
66
- Add "sheikh" variations to titles
7+
- Add parameter to force capitalization of mixed case strings
78
* 0.3.16 - March 24, 2016
89
- Clarify LGPL licence version (#47)
910
- Skip pickle tests if pickle not installed (#48)

nameparser/parser.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,8 @@ def parse_pieces(self, parts, additional_parts_count=0):
544544
raise TypeError("Name parts must be strings. Got {0}".format(type(part)))
545545
output += [x.strip(' ,') for x in part.split(' ')]
546546

547-
# If there's periods, check if it's titles without spaces and add spaces
548-
# so they get picked up later as titles.
547+
# If part contains periods, check if it's multiple titles or suffixes together without spaces
548+
# if so, add the new part with periods to the constants so they get parsed correctly later
549549
for part in output:
550550
# if this part has a period not at the beginning or end
551551
if self.C.regexes.period_not_at_end.match(part):
@@ -684,12 +684,15 @@ def cap_piece(self, piece):
684684
replacement = lambda m: self.cap_word(m.group(0))
685685
return self.C.regexes.word.sub(replacement, piece)
686686

687-
def capitalize(self):
687+
def capitalize(self, force=False):
688688
"""
689689
The HumanName class can try to guess the correct capitalization
690-
of name entered in all upper or lower case. It will not adjust
691-
the case of names entered in mixed case.
690+
of name entered in all upper or lower case. By default, it will not adjust
691+
the case of names entered in mixed case. To run capitalization on all names
692+
pass the parameter `force=True`.
692693
694+
:param bool force: force capitalization of strings that include mixed case
695+
693696
**Usage**
694697
695698
.. doctest:: capitalize
@@ -703,10 +706,13 @@ def capitalize(self):
703706
>>> name.capitalize()
704707
>>> str(name)
705708
'Shirley Maclaine'
709+
>>> name.capitalize(force=True)
710+
>>> str(name)
711+
'Shirley MacLaine'
706712
707713
"""
708714
name = u(self)
709-
if not (name == name.upper() or name == name.lower()):
715+
if not force and not (name == name.upper() or name == name.lower()):
710716
return
711717
self.title_list = self.cap_piece(self.title ).split(' ')
712718
self.first_list = self.cap_piece(self.first ).split(' ')

tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,6 +1791,11 @@ def test_no_change_to_mixed_chase(self):
17911791
hn.capitalize()
17921792
self.m(str(hn), 'Shirley Maclaine', hn)
17931793

1794+
def test_force_capitalization(self):
1795+
hn = HumanName('Shirley Maclaine')
1796+
hn.capitalize(force=True)
1797+
self.m(str(hn), 'Shirley MacLaine', hn)
1798+
17941799
def test_capitalize_diacritics(self):
17951800
hn = HumanName('matthëus schmidt')
17961801
hn.capitalize()

0 commit comments

Comments
 (0)