]> git.openstreetmap.org Git - nominatim.git/blob - nominatim/tokenizer/sanitizers/tag_japanese.py
Fixed cosmetic issues
[nominatim.git] / nominatim / tokenizer / sanitizers / tag_japanese.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
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 This sanitizer maps OSM data to Japanese block addresses.
9 It replaces blocknumber and housenumber with housenumber,
10 and quarter and neighbourhood with place.
11 """
12
13
14 from typing import Callable
15 from typing import List, Optional
16
17 from nominatim.tokenizer.sanitizers.base import ProcessInfo
18 from nominatim.tokenizer.sanitizers.config import SanitizerConfig
19 from nominatim.data.place_name import PlaceName
20
21 KANJI_MAP = {
22       ord('零'): '0',
23       ord('一'): '1',
24       ord('二'): '2',
25       ord('三'): '3',
26       ord('四'): '4',
27       ord('五'): '5',
28       ord('六'): '6',
29       ord('七'): '7',
30       ord('八'): '8',
31       ord('九'): '9'
32     }
33
34 def convert_kanji_sequence_to_number(sequence: str) -> str:
35     """Converts Kanji numbers to Arabic numbers
36     """
37     converted = sequence.translate(KANJI_MAP)
38     return converted
39
40 def create(_: SanitizerConfig) -> Callable[[ProcessInfo], None]:
41     """Set up the sanitizer
42     """
43     return tag_japanese
44
45 def reconbine_housenumber(
46     new_address: List[PlaceName],
47     tmp_housenumber: Optional[str],
48     tmp_blocknumber: Optional[str]
49 ) -> List[PlaceName]:
50     """ Recombine the tag of housenumber by using housenumber and blocknumber
51     """
52     if tmp_blocknumber:
53         tmp_blocknumber = convert_kanji_sequence_to_number(tmp_blocknumber)
54     if tmp_housenumber:
55         tmp_housenumber = convert_kanji_sequence_to_number(tmp_housenumber)
56
57     if tmp_blocknumber and tmp_housenumber:
58         new_address.append(
59             PlaceName(
60                 kind='housenumber',
61                 name=f'{tmp_blocknumber}-{tmp_housenumber}',
62                 suffix=''
63             )
64         )
65     elif tmp_blocknumber:
66         new_address.append(
67             PlaceName(
68                 kind='housenumber',
69                 name=tmp_blocknumber,
70                 suffix=''
71             )
72         )
73     elif tmp_housenumber:
74         new_address.append(
75             PlaceName(
76                 kind='housenumber',
77                 name=tmp_housenumber,
78                 suffix=''
79             )
80         )
81     return new_address
82
83 def reconbine_place(
84     new_address: List[PlaceName],
85     tmp_neighbourhood: Optional[str],
86     tmp_quarter: Optional[str]
87 ) -> List[PlaceName]:
88     """ Recombine the tag of place by using neighbourhood and quarter
89     """
90     if tmp_neighbourhood:
91         tmp_neighbourhood = convert_kanji_sequence_to_number(tmp_neighbourhood)
92     if tmp_quarter:
93         tmp_quarter = convert_kanji_sequence_to_number(tmp_quarter)
94
95     if tmp_neighbourhood and tmp_quarter:
96         new_address.append(
97             PlaceName(
98                 kind='place',
99                 name=f'{tmp_quarter}{tmp_neighbourhood}',
100                 suffix=''
101             )
102         )
103     elif tmp_neighbourhood:
104         new_address.append(
105             PlaceName(
106                 kind='place',
107                 name=tmp_neighbourhood,
108                 suffix=''
109             )
110         )
111     elif tmp_quarter:
112         new_address.append(
113             PlaceName(
114                 kind='place',
115                 name=tmp_quarter,
116                 suffix=''
117             )
118         )
119     return new_address
120 def tag_japanese(obj: ProcessInfo) -> None:
121     """Recombine kind of address
122     """
123     if obj.place.country_code != 'jp':
124         return
125     tmp_housenumber = None
126     tmp_blocknumber = None
127     tmp_neighbourhood = None
128     tmp_quarter = None
129
130     new_address = []
131     for item in obj.address:
132         if item.kind == 'housenumber':
133             tmp_housenumber = item.name
134         elif item.kind == 'block_number':
135             tmp_blocknumber = item.name
136         elif item.kind == 'neighbourhood':
137             tmp_neighbourhood = item.name
138         elif item.kind == 'quarter':
139             tmp_quarter = item.name
140         else:
141             new_address.append(item)
142
143     new_address = reconbine_housenumber(new_address, tmp_housenumber, tmp_blocknumber)
144     new_address = reconbine_place(new_address, tmp_neighbourhood, tmp_quarter)
145
146     obj.address = [item for item in new_address if item.name is not None]