Skip to content

Commit

Permalink
fix: normalize shortened numeric street names
Browse files Browse the repository at this point in the history
Such as 20th, 3rd, or 2nd.

Closes #57
  • Loading branch information
stdavis committed May 7, 2024
1 parent 3844924 commit e687551
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/sweeper/address_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
HWY_REGEX = re.compile("(SR|STATE ROUTE|HIGHWAY)")
UNIT_VALUES_NOT_APPROPRIATE_FOR_HASH_SIGN = ["rear"]
CARDINALS = ["N", "S", "E", "W", "NO", "SO", "EA", "WE", "NORTH", "SOUTH", "EAST", "WEST"]
SHORTENED_CARDINALS = re.compile("rd|nd|th$", re.IGNORECASE)


class Address:
Expand Down Expand Up @@ -110,7 +111,7 @@ def __init__(self, address_text):
del self.StreetNamePreModifier

#: look for two-character prefix directions which usaddress does not handle
if self.street_name:
if self.street_name is not None:
street_name_parts = self.street_name.split(" ")
if len(street_name_parts) > 1:
if street_name_parts[0].upper() in TWO_CHAR_DIRECTIONS and self.prefix_direction is None:
Expand Down Expand Up @@ -141,6 +142,10 @@ def __init__(self, address_text):
self.street_name += f" {self.street_type}"
self.street_type = None

if self.street_direction is not None and self.street_name is not None:
#: check for shortened cardinals
self.street_name = SHORTENED_CARDINALS.sub("00", self.street_name)

if self.unit_id is not None:
#: add `#` if there is not unit type and the unit is numeric
if (
Expand Down
27 changes: 26 additions & 1 deletion tests/test_address_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""

import pytest

from sweeper.address_parser import (
Address,
InvalidStreetTypeError,
Expand Down Expand Up @@ -121,6 +120,32 @@ def test_no_prefix_direction_street(self):
assert address.street_name == "MAIN"
assert address.prefix_direction is None

def test_shortened_numeric_names(self):
address = Address("2040 S 23rd E")

assert address.address_number == "2040"
assert address.prefix_direction == "S"
assert address.street_name == "2300"
assert address.street_direction == "E"
assert address.normalized == "2040 S 2300 E"
assert address.street_type is None

address = Address("70 N 2ND E")
assert address.address_number == "70"
assert address.prefix_direction == "N"
assert address.street_name == "200"
assert address.street_direction == "E"
assert address.normalized == "70 N 200 E"
assert address.street_type is None

address = Address("123 S 20th E")
assert address.address_number == "123"
assert address.prefix_direction == "S"
assert address.street_name == "2000"
assert address.street_direction == "E"
assert address.normalized == "123 S 2000 E"
assert address.street_type is None


class TestStreetDirection:
"""
Expand Down

0 comments on commit e687551

Please sign in to comment.