Skip to content

Commit

Permalink
fix capitalization of prefixes when they are not part of last name #70
Browse files Browse the repository at this point in the history
  • Loading branch information
derek73 committed Aug 30, 2018
1 parent ff6d888 commit da7f67c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions docs/release_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Release Log
* 1.0.0 - August 30, 2018
- Fix support for nicknames in single quotes (#74)
- Change prefix handling to support prefixes on first names (#60)
- Fix prefix capitalization when not part of last name (#70)
- No other big changes, just bumping to v1 to indicate approprite project maturity
* 0.5.8 - August 19, 2018
- Add "Junior" to suffixes (#76)
Expand Down
18 changes: 9 additions & 9 deletions nameparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,8 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0):

### Capitalization Support

def cap_word(self, word):
if self.is_prefix(word) or self.is_conjunction(word):
def cap_word(self, word, attribute):
if (self.is_prefix(word) and attribute=='last') or self.is_conjunction(word):
return word.lower()
exceptions = self.C.capitalization_exceptions
if lc(word) in exceptions:
Expand All @@ -793,10 +793,10 @@ def cap_after_mac(m):
else:
return word.capitalize()

def cap_piece(self, piece):
def cap_piece(self, piece, attribute):
if not piece:
return ""
replacement = lambda m: self.cap_word(m.group(0))
replacement = lambda m: self.cap_word(m.group(0), attribute)
return self.C.regexes.word.sub(replacement, piece)

def capitalize(self, force=False):
Expand Down Expand Up @@ -829,8 +829,8 @@ def capitalize(self, force=False):
name = u(self)
if not force and not (name == name.upper() or name == name.lower()):
return
self.title_list = self.cap_piece(self.title ).split(' ')
self.first_list = self.cap_piece(self.first ).split(' ')
self.middle_list = self.cap_piece(self.middle).split(' ')
self.last_list = self.cap_piece(self.last ).split(' ')
self.suffix_list = self.cap_piece(self.suffix).split(', ')
self.title_list = self.cap_piece(self.title , 'title').split(' ')
self.first_list = self.cap_piece(self.first , 'first').split(' ')
self.middle_list = self.cap_piece(self.middle, 'middle').split(' ')
self.last_list = self.cap_piece(self.last , 'last').split(' ')
self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ')
5 changes: 5 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1941,6 +1941,11 @@ def test_portuguese_prefixes(self):
hn.capitalize()
self.m(str(hn), 'Joao da Silva do Amaral de Souza', hn)

def test_capitalize_prefix_clash_on_first_name(self):
hn = HumanName("van nguyen")
hn.capitalize()
self.m(str(hn), 'Van Nguyen', hn)


class HumanNameOutputFormatTests(HumanNameTestBase):

Expand Down

0 comments on commit da7f67c

Please sign in to comment.