clean_housenumbers: make kinds and delimiters configurable

Also adds unit tests for various options.
This commit is contained in:
Sarah Hoffmann
2022-01-20 12:07:12 +01:00
parent 206ee87188
commit 4774e45218
7 changed files with 133 additions and 20 deletions

View File

@@ -6,13 +6,19 @@
# For a full list of authors see the git log.
"""
Sanitizer that cleans and normalizes housenumbers.
Arguments:
delimiters: Define the set of characters to be used for
splitting a list of housenumbers into parts. (default: ',;')
"""
import re
from nominatim.tokenizer.sanitizers.helpers import create_split_regex
class _HousenumberSanitizer:
def __init__(self, config):
pass
self.kinds = config.get('filter-kind', ('housenumber', ))
self.split_regexp = create_split_regex(config)
def __call__(self, obj):
@@ -21,7 +27,7 @@ class _HousenumberSanitizer:
new_address = []
for item in obj.address:
if item.kind in ('housenumber', 'streetnumber', 'conscriptionnumber'):
if item.kind in self.kinds:
new_address.extend(item.clone(kind='housenumber', name=n) for n in self.sanitize(item.name))
else:
# Don't touch other address items.
@@ -36,13 +42,9 @@ class _HousenumberSanitizer:
The function works as a generator that yields all valid housenumbers
that can be created from the value.
"""
for hnr in self._split_number(value):
yield from self._regularize(hnr)
def _split_number(self, hnr):
for part in re.split(r'[;,]', hnr):
yield part.strip()
for hnr in self.split_regexp.split(value):
if hnr:
yield from self._regularize(hnr)
def _regularize(self, hnr):