Skip to content

Commit da7f67c

Browse files
committed
fix capitalization of prefixes when they are not part of last name #70
1 parent ff6d888 commit da7f67c

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

docs/release_log.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Release Log
33
* 1.0.0 - August 30, 2018
44
- Fix support for nicknames in single quotes (#74)
55
- Change prefix handling to support prefixes on first names (#60)
6+
- Fix prefix capitalization when not part of last name (#70)
67
- No other big changes, just bumping to v1 to indicate approprite project maturity
78
* 0.5.8 - August 19, 2018
89
- Add "Junior" to suffixes (#76)

nameparser/parser.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,8 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0):
779779

780780
### Capitalization Support
781781

782-
def cap_word(self, word):
783-
if self.is_prefix(word) or self.is_conjunction(word):
782+
def cap_word(self, word, attribute):
783+
if (self.is_prefix(word) and attribute=='last') or self.is_conjunction(word):
784784
return word.lower()
785785
exceptions = self.C.capitalization_exceptions
786786
if lc(word) in exceptions:
@@ -793,10 +793,10 @@ def cap_after_mac(m):
793793
else:
794794
return word.capitalize()
795795

796-
def cap_piece(self, piece):
796+
def cap_piece(self, piece, attribute):
797797
if not piece:
798798
return ""
799-
replacement = lambda m: self.cap_word(m.group(0))
799+
replacement = lambda m: self.cap_word(m.group(0), attribute)
800800
return self.C.regexes.word.sub(replacement, piece)
801801

802802
def capitalize(self, force=False):
@@ -829,8 +829,8 @@ def capitalize(self, force=False):
829829
name = u(self)
830830
if not force and not (name == name.upper() or name == name.lower()):
831831
return
832-
self.title_list = self.cap_piece(self.title ).split(' ')
833-
self.first_list = self.cap_piece(self.first ).split(' ')
834-
self.middle_list = self.cap_piece(self.middle).split(' ')
835-
self.last_list = self.cap_piece(self.last ).split(' ')
836-
self.suffix_list = self.cap_piece(self.suffix).split(', ')
832+
self.title_list = self.cap_piece(self.title , 'title').split(' ')
833+
self.first_list = self.cap_piece(self.first , 'first').split(' ')
834+
self.middle_list = self.cap_piece(self.middle, 'middle').split(' ')
835+
self.last_list = self.cap_piece(self.last , 'last').split(' ')
836+
self.suffix_list = self.cap_piece(self.suffix, 'suffix').split(', ')

tests.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,11 @@ def test_portuguese_prefixes(self):
19411941
hn.capitalize()
19421942
self.m(str(hn), 'Joao da Silva do Amaral de Souza', hn)
19431943

1944+
def test_capitalize_prefix_clash_on_first_name(self):
1945+
hn = HumanName("van nguyen")
1946+
hn.capitalize()
1947+
self.m(str(hn), 'Van Nguyen', hn)
1948+
19441949

19451950
class HumanNameOutputFormatTests(HumanNameTestBase):
19461951

0 commit comments

Comments
 (0)