Skip to content

Commit 2951abf

Browse files
authored
Merge pull request #207 from derek73/simplify/join-on-conjunctions
Simplify join_on_conjunctions bookkeeping
2 parents 7901046 + 133a01c commit 2951abf

1 file changed

Lines changed: 17 additions & 19 deletions

File tree

nameparser/parser.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,13 @@ def register_joined_piece(new_piece: str, neighbor: str) -> None:
12801280
# chain onto a following prefix/lastname (see "von und zu")
12811281
self.C.prefixes.add(new_piece)
12821282

1283+
def shift_conj_index(past: int, by: int) -> None:
1284+
# after removing pieces at/after `past`, indices of the
1285+
# remaining conjunctions need to shift down by `by`
1286+
for j, val in enumerate(conj_index):
1287+
if val > past:
1288+
conj_index[j] = val - by
1289+
12831290
for i in conj_index:
12841291
if len(pieces[i]) == 1 and total_length < 4 and pieces[i].isalpha():
12851292
# if there are only 3 total parts (minus known titles, suffixes
@@ -1293,27 +1300,18 @@ def register_joined_piece(new_piece: str, neighbor: str) -> None:
12931300
register_joined_piece(new_piece, pieces[i+1])
12941301
pieces[i] = new_piece
12951302
pieces.pop(i+1)
1296-
# subtract 1 from the index of all the remaining conjunctions
1297-
for j, val in enumerate(conj_index):
1298-
if val > i:
1299-
conj_index[j] = val-1
1303+
shift_conj_index(past=i, by=1)
13001304

13011305
else:
13021306
new_piece = " ".join(pieces[i-1:i+2])
13031307
register_joined_piece(new_piece, pieces[i-1])
13041308
pieces[i-1] = new_piece
1305-
pieces.pop(i)
1306-
rm_count = 2
1307-
try:
1308-
pieces.pop(i)
1309-
except IndexError:
1310-
rm_count = 1
1311-
1312-
# subtract the number of removed pieces from the index
1313-
# of all the remaining conjunctions
1314-
for j, val in enumerate(conj_index):
1315-
if val > i:
1316-
conj_index[j] = val - rm_count
1309+
# len(pieces) - i is always >= 1 here: pieces[i-1:i+2] above
1310+
# already accessed index i, so i is guaranteed in range.
1311+
rm_count = min(2, len(pieces) - i)
1312+
assert rm_count > 0, f"unexpected empty deletion at i={i}, pieces={pieces}"
1313+
del pieces[i:i+rm_count]
1314+
shift_conj_index(past=i, by=rm_count)
13171315

13181316
# join prefixes to following lastnames: ['de la Vega'], ['van Buren']
13191317
prefixes = list(filter(self.is_prefix, pieces))
@@ -1343,7 +1341,7 @@ def register_joined_piece(new_piece: str, neighbor: str) -> None:
13431341
# if there are two prefixes in sequence, join to the following piece
13441342
j += 1
13451343
new_piece = ' '.join(pieces[i:j])
1346-
pieces = pieces[:i] + [new_piece] + pieces[j:]
1344+
pieces[i:j] = [new_piece]
13471345
except StopIteration:
13481346
try:
13491347
# if there are no more prefixes, look for a suffix to stop at
@@ -1355,12 +1353,12 @@ def register_joined_piece(new_piece: str, neighbor: str) -> None:
13551353
# before the prefix) would be matched instead.
13561354
j = pieces.index(stop_at, i + 1)
13571355
new_piece = ' '.join(pieces[i:j])
1358-
pieces = pieces[:i] + [new_piece] + pieces[j:]
1356+
pieces[i:j] = [new_piece]
13591357
except StopIteration:
13601358
# if there were no suffixes, nothing to stop at so join all
13611359
# remaining pieces
13621360
new_piece = ' '.join(pieces[i:])
1363-
pieces = pieces[:i] + [new_piece]
1361+
pieces[i:] = [new_piece]
13641362

13651363
log.debug("pieces: %s", pieces)
13661364
return pieces

0 commit comments

Comments
 (0)