42 lines
973 B
Python
42 lines
973 B
Python
"""Text-normalization helpers shared by every resolver."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from typing import Any
|
|
|
|
from app.core.countries import normalize_country
|
|
|
|
|
|
def parse_float(value: Any) -> float | None:
|
|
try:
|
|
if value in (None, ""):
|
|
return None
|
|
return float(value)
|
|
except (TypeError, ValueError):
|
|
return None
|
|
|
|
|
|
def coerce_str(value: Any) -> str:
|
|
if value in (None, ""):
|
|
return ""
|
|
return str(value).strip()
|
|
|
|
|
|
def normalize_text(value: Any) -> str:
|
|
if value in (None, ""):
|
|
return ""
|
|
normalized = str(value).casefold()
|
|
normalized = re.sub(r"[^a-z0-9一-鿿]+", " ", normalized)
|
|
return re.sub(r"\s+", " ", normalized).strip()
|
|
|
|
|
|
def normalize_country_text(value: Any) -> str:
|
|
normalized = normalize_country(value)
|
|
return normalized or coerce_str(value)
|
|
|
|
|
|
def city_key(city: Any) -> str:
|
|
text = coerce_str(city).split(",", 1)[0]
|
|
return normalize_text(text)
|