Skip to content

Commit

Permalink
feat: show suggestions when the prefix direction is missing
Browse files Browse the repository at this point in the history
Fixes #157
  • Loading branch information
stdavis committed Aug 29, 2023
1 parent 250ef8d commit 599c32d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"python.linting.pycodestyleEnabled": false,
"python.formatting.provider": "black",
"cSpell.words": [
"ADDNUM",
"ADDSYSTEM",
"AGRC",
"atch",
Expand Down Expand Up @@ -38,6 +39,7 @@
"pytest",
"Referer",
"simplejson",
"STREETNAME",
"UICLSID",
"usaddress",
"venv",
Expand Down
22 changes: 21 additions & 1 deletion src/masquerade/providers/open_sgid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
ADDSYSTEM = "addsystem"
CITY = "city"
NAME = "name"
STREETNAME = "streetname"
ADDNUM = "addnum"

RETRY_WAIT_MIN = 0.5 #: seconds
RETRY_WAIT_MAX = 3 #: seconds
Expand All @@ -43,6 +45,7 @@
permutations.append(permutation)
permutations.append(f"{permutation}.")
normalize_direction_infos.append((re.compile(rf'^(\d+) ({"|".join(permutations)})( |$)'), direction[0]))
has_prefix_direction = re.compile(r"^\d+ (n|s|e|w)")

pool = ConnectionPool(f"dbname={DATABASE} user={AGRC} password={AGRC} host={HOST}")

Expand Down Expand Up @@ -242,7 +245,24 @@ def get_suggestion_from_record(self, xid, matched_text, *context_values):
def get_suggest_query(self, search_text, limit):
"""create a query that returns records for suggestions"""

return super().get_suggest_query(normalize_prefix_direction(search_text), limit)
normalized_search_text = normalize_prefix_direction(search_text)

#: if there is no prefix direction, add a query to account for that...
if has_prefix_direction.match(normalized_search_text) is None and len(normalized_search_text.split(" ")) > 1:
where = f"""
{ADDNUM} = '{normalized_search_text.split(' ')[0]}'
and
{STREETNAME} ilike '{normalized_search_text.split(' ')[1]}%'
"""
else:
where = f"{self.search_field} ilike '{normalized_search_text}%'"

return f"""
select {self.get_out_fields()} from {self.table_name}
where {where}
order by {self.search_field} ASC
limit {limit}
"""


def normalize_prefix_direction(search_text):
Expand Down

0 comments on commit 599c32d

Please sign in to comment.