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
 
  14 from nominatim.tokenizer.sanitizers.config import SanitizerConfig
 
  17     """ A searchable name for a place together with properties.
 
  18         Every name object saves the name proper and two basic properties:
 
  19         * 'kind' describes the name of the OSM key used without any suffixes
 
  20           (i.e. the part after the colon removed)
 
  21         * 'suffix' contains the suffix of the OSM tag, if any. The suffix
 
  22           is the part of the key after the first colon.
 
  23         In addition to that, the name may have arbitrary additional attributes.
 
  24         Which attributes are used, depends on the token analyser.
 
  27     def __init__(self, name, kind, suffix):
 
  35         return f"PlaceName(name='{self.name}',kind='{self.kind}',suffix='{self.suffix}')"
 
  38     def clone(self, name=None, kind=None, suffix=None, attr=None):
 
  39         """ Create a deep copy of the place name, optionally with the
 
  40             given parameters replaced. In the attribute list only the given
 
  41             keys are updated. The list is not replaced completely.
 
  42             In particular, the function cannot to be used to remove an
 
  43             attribute from a place name.
 
  45         newobj = PlaceName(name or self.name,
 
  47                            suffix or self.suffix)
 
  49         newobj.attr.update(self.attr)
 
  51             newobj.attr.update(attr)
 
  56     def set_attr(self, key, value):
 
  57         """ Add the given property to the name. If the property was already
 
  58             set, then the value is overwritten.
 
  60         self.attr[key] = value
 
  63     def get_attr(self, key, default=None):
 
  64         """ Return the given property or the value of 'default' if it
 
  67         return self.attr.get(key, default)
 
  70     def has_attr(self, key):
 
  71         """ Check if the given attribute is set.
 
  73         return key in self.attr
 
  77     """ Container class for information handed into to handler functions.
 
  78         The 'names' and 'address' members are mutable. A handler must change
 
  79         them by either modifying the lists place or replacing the old content
 
  83     def __init__(self, place):
 
  85         self.names = self._convert_name_dict(place.name)
 
  86         self.address = self._convert_name_dict(place.address)
 
  90     def _convert_name_dict(names):
 
  91         """ Convert a dictionary of names into a list of PlaceNames.
 
  92             The dictionary key is split into the primary part of the key
 
  93             and the suffix (the part after an optional colon).
 
  98             for key, value in names.items():
 
  99                 parts = key.split(':', 1)
 
 100                 out.append(PlaceName(value.strip(),
 
 102                                      parts[1].strip() if len(parts) > 1 else None))
 
 107 class PlaceSanitizer:
 
 108     """ Controller class which applies sanitizer functions on the place
 
 109         names and address before they are used by the token analysers.
 
 112     def __init__(self, rules):
 
 117                 if 'step' not in func:
 
 118                     raise UsageError("Sanitizer rule is missing the 'step' attribute.")
 
 119                 module_name = 'nominatim.tokenizer.sanitizers.' + func['step'].replace('-', '_')
 
 120                 handler_module = importlib.import_module(module_name)
 
 121                 self.handlers.append(handler_module.create(SanitizerConfig(func)))
 
 124     def process_names(self, place):
 
 125         """ Extract a sanitized list of names and address parts from the
 
 126             given place. The function returns a tuple
 
 127             (list of names, list of address names)
 
 129         obj = _ProcessInfo(place)
 
 131         for func in self.handlers:
 
 134         return obj.names, obj.address