Skip to content

Commit 2a3edae

Browse files
committed
fix #126, don't count as prefix when first name
update initials processing to only exclude conjunctions and prefixes when it is not a first name.
1 parent 322d5e6 commit 2a3edae

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

nameparser/parser.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,17 @@ def as_dict(self, include_empty=True):
191191

192192
def __process_initial__(self, name_part):
193193
"""
194-
Name parts may include prefixes or conjuctions. This function filters these from the name.
195-
"""
196-
parsed = " ".join([split for split in name_part.split(" ") if len(split) and not (self.is_prefix(split) or self.is_conjunction(split))])
197-
return parsed[0] if len(parsed) else ""
194+
Name parts may include prefixes or conjuctions. This function filters these from the name unless it is
195+
a first name, since first names cannot be conjunctions or prefixes.
196+
"""
197+
parts = name_part.split(" ")
198+
parsed = ""
199+
if len(parts) and not (name_part == 'first' and (self.is_prefix(parts) or self.is_conjunction(parts))):
200+
parsed = " ".join(parts)
201+
if len(parsed) > 0:
202+
return parsed[0]
203+
else:
204+
return self.C.empty_attribute_default
198205

199206
def initials_list(self):
200207
"""
@@ -855,6 +862,9 @@ def join_on_conjunctions(self, pieces, additional_parts_count=0):
855862
# join everything after the prefix until the next prefix or suffix
856863

857864
try:
865+
if i == 0 and total_length >= 1:
866+
# If it's the first piece and there are more than 1 rootnames, assume it's a first name
867+
continue
858868
next_prefix = next(iter(filter(self.is_prefix, pieces[i + 1:])))
859869
j = pieces.index(next_prefix)
860870
if j == i + 1:

tests.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,17 @@ def test_prefix_before_two_part_last_name(self):
16051605
self.m(hn.first, "pennie", hn)
16061606
self.m(hn.last, "von bergen wessels", hn)
16071607

1608+
def test_prefix_is_first_name(self):
1609+
hn = HumanName("Van Johnson")
1610+
self.m(hn.first, "Van", hn)
1611+
self.m(hn.last, "Johnson", hn)
1612+
1613+
def test_prefix_is_first_name_with_middle_name(self):
1614+
hn = HumanName("Van Jeremy Johnson")
1615+
self.m(hn.first, "Van", hn)
1616+
self.m(hn.middle, "Jeremy", hn)
1617+
self.m(hn.last, "Johnson", hn)
1618+
16081619
def test_prefix_before_two_part_last_name_with_suffix(self):
16091620
hn = HumanName("pennie von bergen wessels III")
16101621
self.m(hn.first, "pennie", hn)
@@ -2313,6 +2324,10 @@ def test_initials_list_complex_name(self):
23132324
hn = HumanName("Doe, John A. Kenneth, Jr.")
23142325
self.m(hn.initials_list(), ["J", "A", "K", "D"], hn)
23152326

2327+
def test_initials_with_prefix_firstname(self):
2328+
hn = HumanName("Van Jeremy Johnson")
2329+
self.m(hn.initials_list(), ["V", "J", "J"], hn)
2330+
23162331

23172332
TEST_NAMES = (
23182333
"John Doe",

0 commit comments

Comments
 (0)