1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Handler for cleaning name and address tags in place information before it
9 is handed to the token analysis.
13 from nominatim.errors import UsageError
16 """ A searchable name for a place together with properties.
17 Every name object saves the name proper and two basic properties:
18 * 'kind' describes the name of the OSM key used without any suffixes
19 (i.e. the part after the colon removed)
20 * 'suffix' contains the suffix of the OSM tag, if any. The suffix
21 is the part of the key after the first colon.
22 In addition to that, the name may have arbitrary additional attributes.
23 Which attributes are used, depends on the token analyser.
26 def __init__(self, name, kind, suffix):
34 return f"PlaceName(name='{self.name}',kind='{self.kind}',suffix='{self.suffix}')"
37 def clone(self, name=None, kind=None, suffix=None, attr=None):
38 """ Create a deep copy of the place name, optionally with the
39 given parameters replaced. In the attribute list only the given
40 keys are updated. The list is not replaced completely.
41 In particular, the function cannot to be used to remove an
42 attribute from a place name.
44 newobj = PlaceName(name or self.name,
46 suffix or self.suffix)
48 newobj.attr.update(self.attr)
50 newobj.attr.update(attr)
55 def set_attr(self, key, value):
56 """ Add the given property to the name. If the property was already
57 set, then the value is overwritten.
59 self.attr[key] = value
62 def get_attr(self, key, default=None):
63 """ Return the given property or the value of 'default' if it
66 return self.attr.get(key, default)
69 def has_attr(self, key):
70 """ Check if the given attribute is set.
72 return key in self.attr
76 """ Container class for information handed into to handler functions.
77 The 'names' and 'address' members are mutable. A handler must change
78 them by either modifying the lists place or replacing the old content
82 def __init__(self, place):
84 self.names = self._convert_name_dict(place.name)
85 self.address = self._convert_name_dict(place.address)
89 def _convert_name_dict(names):
90 """ Convert a dictionary of names into a list of PlaceNames.
91 The dictionary key is split into the primary part of the key
92 and the suffix (the part after an optional colon).
97 for key, value in names.items():
98 parts = key.split(':', 1)
99 out.append(PlaceName(value.strip(),
101 parts[1].strip() if len(parts) > 1 else None))
106 class PlaceSanitizer:
107 """ Controller class which applies sanitizer functions on the place
108 names and address before they are used by the token analysers.
111 def __init__(self, rules):
116 if 'step' not in func:
117 raise UsageError("Sanitizer rule is missing the 'step' attribute.")
118 module_name = 'nominatim.tokenizer.sanitizers.' + func['step'].replace('-', '_')
119 handler_module = importlib.import_module(module_name)
120 self.handlers.append(handler_module.create(func))
123 def process_names(self, place):
124 """ Extract a sanitized list of names and address parts from the
125 given place. The function returns a tuple
126 (list of names, list of address names)
128 obj = _ProcessInfo(place)
130 for func in self.handlers:
133 return obj.names, obj.address