X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/a79a3210e69c648e3e5c1123b4049ce0419b484c..0722495434e039b7b716206974d0076db8813ca6:/nominatim/tokenizer/sanitizers/clean_housenumbers.py?ds=inline diff --git a/nominatim/tokenizer/sanitizers/clean_housenumbers.py b/nominatim/tokenizer/sanitizers/clean_housenumbers.py index 48021793..ae6349a9 100644 --- a/nominatim/tokenizer/sanitizers/clean_housenumbers.py +++ b/nominatim/tokenizer/sanitizers/clean_housenumbers.py @@ -19,29 +19,34 @@ Arguments: where each string is a regular expression. An address item is considered a house number if the 'kind' fully matches any of the given regular expressions. (default: 'housenumber') - + convert-to-name: Define house numbers that should be treated as a name + instead of a house number. Either takes a single string + or a list of strings, where each string is a regular + expression that must match the full house number value. """ -from nominatim.tokenizer.sanitizers.helpers import create_split_regex, create_kind_filter +from typing import Callable, Iterator, List -class _HousenumberSanitizer: +from nominatim.tokenizer.sanitizers.base import ProcessInfo +from nominatim.data.place_name import PlaceName +from nominatim.tokenizer.sanitizers.config import SanitizerConfig - def __init__(self, config): - self.filter_kind = create_kind_filter(config, 'housenumber') - self.split_regexp = create_split_regex(config) +class _HousenumberSanitizer: - nameregexps = config.get('is-a-name', []) - self.is_name_regexp = [re.compile(r) for r in nameregexps] + def __init__(self, config: SanitizerConfig) -> None: + self.filter_kind = config.get_filter('filter-kind', ['housenumber']) + self.split_regexp = config.get_delimiter() + self.filter_name = config.get_filter('convert-to-name', 'FAIL_ALL') - def __call__(self, obj): + def __call__(self, obj: ProcessInfo) -> None: if not obj.address: return - new_address = [] + new_address: List[PlaceName] = [] for item in obj.address: - if self.filter_kind(item): - if self.treat_as_name(item.name): + if self.filter_kind(item.kind): + if self.filter_name(item.name): obj.names.append(item.clone(kind='housenumber')) else: new_address.extend(item.clone(kind='housenumber', name=n) @@ -53,7 +58,7 @@ class _HousenumberSanitizer: obj.address = new_address - def sanitize(self, value): + def sanitize(self, value: str) -> Iterator[str]: """ Extract housenumbers in a regularized format from an OSM value. The function works as a generator that yields all valid housenumbers @@ -64,16 +69,11 @@ class _HousenumberSanitizer: yield from self._regularize(hnr) - @staticmethod - def _regularize(hnr): + def _regularize(self, hnr: str) -> Iterator[str]: yield hnr - def _treat_as_name(self, housenumber): - return any(r.fullmatch(housenumber) is not None for r in self.is_name_regexp) - - -def create(config): +def create(config: SanitizerConfig) -> Callable[[ProcessInfo], None]: """ Create a housenumber processing function. """