1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2024 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Data class for a single name of a place.
 
  10 from typing import Optional, Dict, Mapping
 
  14     """ Each name and address part of a place is encapsulated in an object of
 
  15         this class. It saves not only the name proper but also describes the
 
  16         kind of name with two 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.
 
  23         In addition to that, a name may have arbitrary additional attributes.
 
  24         How attributes are used, depends on the sanitizers and token analysers.
 
  25         The exception is the 'analyzer' attribute. This attribute determines
 
  26         which token analysis module will be used to finalize the treatment of
 
  30     def __init__(self, name: str, kind: str, suffix: Optional[str]):
 
  34         self.attr: Dict[str, str] = {}
 
  36     def __repr__(self) -> str:
 
  37         return f"PlaceName(name={self.name!r},kind={self.kind!r},suffix={self.suffix!r})"
 
  39     def clone(self, name: Optional[str] = None,
 
  40               kind: Optional[str] = None,
 
  41               suffix: Optional[str] = None,
 
  42               attr: Optional[Mapping[str, str]] = None) -> 'PlaceName':
 
  43         """ Create a deep copy of the place name, optionally with the
 
  44             given parameters replaced. In the attribute list only the given
 
  45             keys are updated. The list is not replaced completely.
 
  46             In particular, the function cannot to be used to remove an
 
  47             attribute from a place name.
 
  49         newobj = PlaceName(name or self.name,
 
  51                            suffix or self.suffix)
 
  53         newobj.attr.update(self.attr)
 
  55             newobj.attr.update(attr)
 
  59     def set_attr(self, key: str, value: str) -> None:
 
  60         """ Add the given property to the name. If the property was already
 
  61             set, then the value is overwritten.
 
  63         self.attr[key] = value
 
  65     def get_attr(self, key: str, default: Optional[str] = None) -> Optional[str]:
 
  66         """ Return the given property or the value of 'default' if it
 
  69         return self.attr.get(key, default)
 
  71     def has_attr(self, key: str) -> bool:
 
  72         """ Check if the given attribute is set.
 
  74         return key in self.attr