mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-11 05:14:07 +00:00
generalize filter-kind parameter for sanatizers
Now behaves the same for tag_analyzer_by_language and clean_housenumbers. Adds tests.
This commit is contained in:
@@ -5,19 +5,24 @@
|
||||
# Copyright (C) 2022 by the Nominatim developer community.
|
||||
# For a full list of authors see the git log.
|
||||
"""
|
||||
Sanitizer that cleans and normalizes housenumbers.
|
||||
Sanitizer that cleans and normalizes house numbers.
|
||||
|
||||
Arguments:
|
||||
delimiters: Define the set of characters to be used for
|
||||
splitting a list of housenumbers into parts. (default: ',;')
|
||||
splitting a list of house numbers into parts. (default: ',;')
|
||||
filter-kind: Define the address tags that are considered to be a
|
||||
house number. Either takes a single string or a list of strings,
|
||||
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')
|
||||
|
||||
"""
|
||||
from nominatim.tokenizer.sanitizers.helpers import create_split_regex
|
||||
from nominatim.tokenizer.sanitizers.helpers import create_split_regex, create_kind_filter
|
||||
|
||||
class _HousenumberSanitizer:
|
||||
|
||||
def __init__(self, config):
|
||||
self.kinds = config.get('filter-kind', ('housenumber', ))
|
||||
self.filter_kind = create_kind_filter(config, 'housenumber')
|
||||
self.split_regexp = create_split_regex(config)
|
||||
|
||||
|
||||
@@ -27,7 +32,7 @@ class _HousenumberSanitizer:
|
||||
|
||||
new_address = []
|
||||
for item in obj.address:
|
||||
if item.kind in self.kinds:
|
||||
if self.filter_kind(item):
|
||||
new_address.extend(item.clone(kind='housenumber', name=n) for n in self.sanitize(item.name))
|
||||
else:
|
||||
# Don't touch other address items.
|
||||
|
||||
@@ -27,3 +27,26 @@ def create_split_regex(config, default=',;'):
|
||||
raise UsageError("Empty 'delimiter' parameter not allowed for sanitizer.")
|
||||
|
||||
return re.compile('\\s*[{}]+\\s*'.format(''.join('\\' + d for d in delimiter_set)))
|
||||
|
||||
|
||||
def create_kind_filter(config, default=None):
|
||||
""" Create a filter function for the name kind from the 'filter-kind'
|
||||
config parameter. The filter functions takes a name item and returns
|
||||
True when the item passes the filter.
|
||||
|
||||
If the parameter is empty, the filter lets all items pass. If the
|
||||
paramter is a string, it is interpreted as a single regular expression
|
||||
that must match the full kind string. If the parameter is a list then
|
||||
any of the regular expressions in the list must match to pass.
|
||||
"""
|
||||
filters = config.get('filter-kind', default)
|
||||
|
||||
if not filters:
|
||||
return lambda _: True
|
||||
|
||||
if isinstance(filters, str):
|
||||
regex = re.compile(filters)
|
||||
return lambda name: regex.fullmatch(name.kind)
|
||||
|
||||
regexes = [re.compile(regex) for regex in filters]
|
||||
return lambda name: any(regex.fullmatch(name.kind) for regex in regexes)
|
||||
|
||||
@@ -33,17 +33,14 @@ Arguments:
|
||||
import re
|
||||
|
||||
from nominatim.tools import country_info
|
||||
from nominatim.tokenizer.sanitizers.helpers import create_kind_filter
|
||||
|
||||
class _AnalyzerByLanguage:
|
||||
""" Processor for tagging the language of names in a place.
|
||||
"""
|
||||
|
||||
def __init__(self, config):
|
||||
if 'filter-kind' in config:
|
||||
self.regexes = [re.compile(regex) for regex in config['filter-kind']]
|
||||
else:
|
||||
self.regexes = None
|
||||
|
||||
self.filter_kind = create_kind_filter(config)
|
||||
self.replace = config.get('mode', 'replace') != 'append'
|
||||
self.whitelist = config.get('whitelist')
|
||||
|
||||
@@ -63,13 +60,6 @@ class _AnalyzerByLanguage:
|
||||
self.deflangs[ccode] = clangs
|
||||
|
||||
|
||||
def _kind_matches(self, kind):
|
||||
if self.regexes is None:
|
||||
return True
|
||||
|
||||
return any(regex.fullmatch(kind) for regex in self.regexes)
|
||||
|
||||
|
||||
def _suffix_matches(self, suffix):
|
||||
if self.whitelist is None:
|
||||
return len(suffix) in (2, 3) and suffix.islower()
|
||||
@@ -84,7 +74,7 @@ class _AnalyzerByLanguage:
|
||||
more_names = []
|
||||
|
||||
for name in (n for n in obj.names
|
||||
if not n.has_attr('analyzer') and self._kind_matches(n.kind)):
|
||||
if not n.has_attr('analyzer') and self.filter_kind(n)):
|
||||
if name.suffix:
|
||||
langs = [name.suffix] if self._suffix_matches(name.suffix) else None
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user