From a9437dd2c500f546a0068bb09f391878a416d5c9 Mon Sep 17 00:00:00 2001 From: Max Filenko Date: Mon, 25 Mar 2024 13:51:06 +0100 Subject: [PATCH] Try to match [other abbreviations][1] as well as verbose names. [1]: https://en.wikipedia.org/wiki/List_of_U.S._state_and_territory_abbreviations#Table --- us/states.py | 17 +++++++++++++++-- us/tests/test_us.py | 8 ++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/us/states.py b/us/states.py index 1e67ffc..99b2343 100644 --- a/us/states.py +++ b/us/states.py @@ -63,8 +63,8 @@ def shapefile_urls(self) -> Optional[Dict[str, str]]: return urls -def lookup(val, field: Optional[str] = None, use_cache: bool = True) -> Optional[State]: - """Semi-fuzzy state lookup. This method will make a best effort +def lookup(val, field: Optional[str] = None, use_cache: bool = True, deep_lookup: bool = False) -> Optional[State]: + """Semi-fuzzy state lookup. This method will make the best effort attempt at finding the state based on the lookup value provided. * two digits will search for FIPS code @@ -105,6 +105,19 @@ def lookup(val, field: Optional[str] = None, use_cache: bool = True) -> Optional if use_cache: _lookup_cache[cache_key] = state + if not matched_state and deep_lookup: + val = " ".join( + val.rstrip(".") + .lower() + .replace("state", "").replace(" of ", "").replace("a ", "").replace("the ", "") + .split() + ) + for state in STATES_AND_TERRITORIES: + if state.name.lower().startswith(val): + matched_state = state + if use_cache: + _lookup_cache[cache_key] = state + return matched_state diff --git a/us/tests/test_us.py b/us/tests/test_us.py index 02a8c76..0dd5d4d 100644 --- a/us/tests/test_us.py +++ b/us/tests/test_us.py @@ -73,6 +73,14 @@ def test_obsolete_lookup(): assert us.states.lookup(state.name) is None +def test_deep_lookup(): + assert us.states.lookup("Cal.", field="name", deep_lookup=True) == us.states.CA + assert us.states.lookup(" Mary ", field="name", deep_lookup=True) == us.states.MD + assert us.states.lookup("The New York State ", field="name", deep_lookup=True) == us.states.NY + assert us.states.lookup(" a State of Washington", field="name", deep_lookup=True) == us.states.WA + assert us.states.lookup("State of the Washington", field="name", deep_lookup=True) == us.states.WA + + # test metaphone