Skip to content

Commit

Permalink
fix: handle two unit numbers
Browse files Browse the repository at this point in the history
Closes #63
  • Loading branch information
stdavis committed May 7, 2024
1 parent e687551 commit 79c6f0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/sweeper/address_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ class Address:
StreetNamePreModifier = None

def __init__(self, address_text):
parts, parsed_as = usaddress.tag(address_text.replace(".", ""), TAG_MAPPING)
address_text = address_text.replace(".", "")
extra_unit_parts = None
try:
parts, parsed_as = usaddress.tag(address_text, TAG_MAPPING)
except usaddress.RepeatedLabelError:
parts, parsed_as = usaddress.tag(" ".join(address_text.split(" ")[:-2]), TAG_MAPPING)
extra_unit_parts, _ = usaddress.tag(" ".join(address_text.split(" ")[-2:]), TAG_MAPPING)
if parsed_as not in ["Street Address", "PO Box"]:
raise Exception(f'"{address_text}" is not recognized as a valid street address, or P.O. Box')

Expand Down Expand Up @@ -159,6 +165,9 @@ def __init__(self, address_text):
elif self.unit_id.startswith("#") and self.unit_type is not None:
self.unit_id = self.unit_id[1:].strip()

if extra_unit_parts:
self.unit_id = f"{self.unit_id}-{extra_unit_parts['unit_id']}"

def __repr__(self):
properties = vars(self)
properties.update({"normalized": self.normalized})
Expand Down
11 changes: 11 additions & 0 deletions tests/test_address_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,17 @@ def test_units_not_appropriate_for_hash_sign(self):
assert address.street_type == "ST"
assert address.normalized == "957 PATTERSON ST REAR"

def test_multiple_units(self):
address = Address("1361 N 1075 WEST UNIT 12 BLDG B")

assert address.address_number == "1361"
assert address.prefix_direction == "N"
assert address.street_name == "1075"
assert address.street_direction == "W"
assert address.unit_type == "UNIT"
assert address.unit_id == "12-B"
assert address.normalized == "1361 N 1075 W UNIT 12-B"


class TestPOBox:
"""
Expand Down

0 comments on commit 79c6f0f

Please sign in to comment.