2 Handler for cleaning name and address tags in place information before it
3 is handed to the token analysis.
7 from nominatim.errors import UsageError
10 """ A searchable name for a place together with properties.
11 Every name object saves the name proper and two basic properties:
12 * 'kind' describes the name of the OSM key used without any suffixes
13 (i.e. the part after the colon removed)
14 * 'suffix' contains the suffix of the OSM tag, if any. The suffix
15 is the part of the key after the first colon.
16 In addition to that, the name may have arbitrary additional attributes.
17 Which attributes are used, depends on the token analyser.
20 def __init__(self, name, kind, suffix):
28 return f"PlaceName(name='{self.name}',kind='{self.kind}',suffix='{self.suffix}')"
31 def clone(self, name=None, kind=None, suffix=None, attr=None):
32 """ Create a deep copy of the place name, optionally with the
33 given parameters replaced. In the attribute list only the given
34 keys are updated. The list is not replaced completely.
35 In particular, the function cannot to be used to remove an
36 attribute from a place name.
38 newobj = PlaceName(name or self.name,
40 suffix or self.suffix)
42 newobj.attr.update(self.attr)
44 newobj.attr.update(attr)
49 def set_attr(self, key, value):
50 """ Add the given property to the name. If the property was already
51 set, then the value is overwritten.
53 self.attr[key] = value
56 def get_attr(self, key, default=None):
57 """ Return the given property or the value of 'default' if it
60 return self.attr.get(key, default)
63 def has_attr(self, key):
64 """ Check if the given attribute is set.
66 return key in self.attr
70 """ Container class for information handed into to handler functions.
71 The 'names' and 'address' members are mutable. A handler must change
72 them by either modifying the lists place or replacing the old content
76 def __init__(self, place):
78 self.names = self._convert_name_dict(place.name)
79 self.address = self._convert_name_dict(place.address)
83 def _convert_name_dict(names):
84 """ Convert a dictionary of names into a list of PlaceNames.
85 The dictionary key is split into the primary part of the key
86 and the suffix (the part after an optional colon).
91 for key, value in names.items():
92 parts = key.split(':', 1)
93 out.append(PlaceName(value.strip(),
95 parts[1].strip() if len(parts) > 1 else None))
100 class PlaceSanitizer:
101 """ Controller class which applies sanitizer functions on the place
102 names and address before they are used by the token analysers.
105 def __init__(self, rules):
110 if 'step' not in func:
111 raise UsageError("Sanitizer rule is missing the 'step' attribute.")
112 module_name = 'nominatim.tokenizer.sanitizers.' + func['step'].replace('-', '_')
113 handler_module = importlib.import_module(module_name)
114 self.handlers.append(handler_module.create(func))
117 def process_names(self, place):
118 """ Extract a sanitized list of names and address parts from the
119 given place. The function returns a tuple
120 (list of names, list of address names)
122 obj = _ProcessInfo(place)
124 for func in self.handlers:
127 return obj.names, obj.address