Preserve house number from user query when Nominatim omits it

Many addresses resolve to just the road in Nominatim (no house_number
in response). Now extracts the leading number from the user's original
search query and prepends it to the road name, so "123 Adelaide Terrace"
stays as "123 Adelaide Terrace" instead of just "Adelaide Terrace".

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kyle 2026-03-17 14:52:46 +08:00
parent 90bfd00a82
commit d4117818c7

View File

@ -8,6 +8,7 @@ import json
import urllib.request import urllib.request
import urllib.parse import urllib.parse
import logging import logging
import re
from app.database import get_db from app.database import get_db
from app.models.location import Location from app.models.location import Location
@ -70,6 +71,12 @@ async def search_locations(
addr = item.get("address", {}) addr = item.get("address", {})
house_number = addr.get("house_number", "") house_number = addr.get("house_number", "")
road = addr.get("road", "") road = addr.get("road", "")
# If Nominatim didn't return a house_number but the user's
# query starts with one, preserve it from the original query.
if not house_number and road:
m = re.match(r"^(\d+[\w/-]*)\s+", q.strip())
if m:
house_number = m.group(1)
# Build a name that preserves the house number # Build a name that preserves the house number
if house_number and road: if house_number and road:
name = f"{house_number} {road}" name = f"{house_number} {road}"