Skip to content

Commit 64a789e

Browse files
derek73claude
andcommitted
fix: honor single-char symbol conjunctions regardless of name length (closes #151)
The `join_on_conjunction` heuristic skipped single-character conjunctions when `total_length < 4` to avoid treating alphabetic initials like `e` or `y` as conjunctions. This inadvertently also skipped non-alphabetic symbols like `&`, which can never be initials. Fix: only apply the "treat as initial" fallback when the character is alphabetic. Non-alphabetic conjunctions (e.g. `&`) are now always joined regardless of name length, so `"Mr. & Mrs. John Smith"` correctly parses the title as `"Mr. & Mrs."`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent b488e97 commit 64a789e

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

nameparser/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ def join_on_conjunctions(self, pieces: list[str], additional_parts_count: int =
870870
conj_index = [i for i, piece in enumerate(pieces) if self.is_conjunction(piece)]
871871

872872
for i in conj_index:
873-
if len(pieces[i]) == 1 and total_length < 4:
873+
if len(pieces[i]) == 1 and total_length < 4 and pieces[i].isalpha():
874874
# if there are only 3 total parts (minus known titles, suffixes
875875
# and prefixes) and this conjunction is a single letter, prefer
876876
# treating it as an initial rather than a conjunction.

tests/test_conjunctions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ def test_couple_titles(self) -> None:
111111
self.m(hn.first, "John and Jane", hn)
112112
self.m(hn.last, "Smith", hn)
113113

114+
def test_couple_titles_ampersand_conjunction(self) -> None:
115+
# issue 151: single-char conjunctions in the conjunctions list should
116+
# be honored even when total_length < 4
117+
hn = HumanName('Mr. & Mrs. John Smith')
118+
self.m(hn.title, "Mr. & Mrs.", hn)
119+
self.m(hn.first, "John", hn)
120+
self.m(hn.last, "Smith", hn)
121+
114122
def test_title_with_three_part_name_last_initial_is_suffix_uppercase_no_period(self) -> None:
115123
hn = HumanName("King John Alexander V")
116124
self.m(hn.title, "King", hn)

0 commit comments

Comments
 (0)