]> git.openstreetmap.org Git - nominatim.git/blobdiff - nominatim/tokenizer/sanitizers/clean_housenumbers.py
Merge pull request #3112 from jenkin/fix-polgyon-polygon-typo
[nominatim.git] / nominatim / tokenizer / sanitizers / clean_housenumbers.py
index c229716f8e8353d8d8788670b767ea0c9cebc22f..ae6349a9c846cba2d9fa403471fe0c0f5b335e17 100644 (file)
@@ -24,27 +24,29 @@ Arguments:
                      or a list of strings, where each string is a regular
                      expression that must match the full house number value.
 """
                      or a list of strings, where each string is a regular
                      expression that must match the full house number value.
 """
-import re
+from typing import Callable, Iterator, List
+
+from nominatim.tokenizer.sanitizers.base import ProcessInfo
+from nominatim.data.place_name import PlaceName
+from nominatim.tokenizer.sanitizers.config import SanitizerConfig
 
 class _HousenumberSanitizer:
 
 
 class _HousenumberSanitizer:
 
-    def __init__(self, config):
-        self.filter_kind = config.get_filter_kind('housenumber')
+    def __init__(self, config: SanitizerConfig) -> None:
+        self.filter_kind = config.get_filter('filter-kind', ['housenumber'])
         self.split_regexp = config.get_delimiter()
 
         self.split_regexp = config.get_delimiter()
 
-        nameregexps = config.get_string_list('convert-to-name', [])
-        self.is_name_regexp = [re.compile(r) for r in nameregexps]
-
+        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
 
         if not obj.address:
             return
 
-        new_address = []
+        new_address: List[PlaceName] = []
         for item in obj.address:
         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)
                     obj.names.append(item.clone(kind='housenumber'))
                 else:
                     new_address.extend(item.clone(kind='housenumber', name=n)
@@ -56,7 +58,7 @@ class _HousenumberSanitizer:
         obj.address = new_address
 
 
         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
         """ Extract housenumbers in a regularized format from an OSM value.
 
             The function works as a generator that yields all valid housenumbers
@@ -67,16 +69,11 @@ class _HousenumberSanitizer:
                 yield from self._regularize(hnr)
 
 
                 yield from self._regularize(hnr)
 
 
-    @staticmethod
-    def _regularize(hnr):
+    def _regularize(self, hnr: str) -> Iterator[str]:
         yield hnr
 
 
         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.
     """
 
     """ Create a housenumber processing function.
     """