]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/sanitizers/clean_tiger_tags.py
Split lookupInCountry in two functions and document NOMINATIM_SEARCH_WITHIN_COUNTRIES...
[nominatim.git] / nominatim / tokenizer / sanitizers / clean_tiger_tags.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 preprocesses tags from the TIGER import.
9
10 It makes the following changes:
11
12 * remove state reference from tiger:county
13 """
14 from typing import Callable
15 import re
16
17 from nominatim.tokenizer.sanitizers.base import ProcessInfo
18 from nominatim.tokenizer.sanitizers.config import SanitizerConfig
19
20 COUNTY_MATCH = re.compile('(.*), [A-Z][A-Z]')
21
22 def _clean_tiger_county(obj: ProcessInfo) -> None:
23     """ Remove the state reference from tiger:county tags.
24
25         This transforms a name like 'Hamilton, AL' into 'Hamilton'.
26         If no state reference is detected at the end, the name is left as is.
27     """
28     if not obj.address:
29         return
30
31     for item in obj.address:
32         if item.kind == 'tiger' and item.suffix == 'county':
33             m = COUNTY_MATCH.fullmatch(item.name)
34             if m:
35                 item.name = m[1]
36             # Switch kind and suffix, the split left them reversed.
37             item.kind = 'county'
38             item.suffix = 'tiger'
39
40             return
41
42
43 def create(_: SanitizerConfig) -> Callable[[ProcessInfo], None]:
44     """ Create a function that preprocesses tags from the TIGER import.
45     """
46     return _clean_tiger_county