]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/sanitizers/split_name_list.py
add API functions for search functions
[nominatim.git] / nominatim / tokenizer / sanitizers / split_name_list.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Sanitizer that splits lists of names into their components.
9
10 Arguments:
11     delimiters: Define the set of characters to be used for
12                 splitting the list. (default: ',;')
13 """
14 from typing import Callable
15
16 from nominatim.tokenizer.sanitizers.base import ProcessInfo
17 from nominatim.tokenizer.sanitizers.config import SanitizerConfig
18
19 def create(config: SanitizerConfig) -> Callable[[ProcessInfo], None]:
20     """ Create a name processing function that splits name values with
21         multiple values into their components.
22     """
23     regexp = config.get_delimiter()
24
25     def _process(obj: ProcessInfo) -> None:
26         if not obj.names:
27             return
28
29         new_names = []
30         for name in obj.names:
31             split_names = regexp.split(name.name)
32             if len(split_names) == 1:
33                 new_names.append(name)
34             else:
35                 new_names.extend(name.clone(name=n) for n in split_names if n)
36
37         obj.names = new_names
38
39     return _process