mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-16 15:47:58 +00:00
factor out housenumber splitting into sanitizer
This commit is contained in:
@@ -413,14 +413,16 @@ class LegacyICUNameAnalyzer(AbstractAnalyzer):
|
|||||||
|
|
||||||
|
|
||||||
def _process_place_address(self, token_info, address):
|
def _process_place_address(self, token_info, address):
|
||||||
hnrs = []
|
hnrs = set()
|
||||||
addr_terms = []
|
addr_terms = []
|
||||||
streets = []
|
streets = []
|
||||||
for item in address:
|
for item in address:
|
||||||
if item.kind == 'postcode':
|
if item.kind == 'postcode':
|
||||||
self._add_postcode(item.name)
|
self._add_postcode(item.name)
|
||||||
elif item.kind in ('housenumber', 'streetnumber', 'conscriptionnumber'):
|
elif item.kind == 'housenumber':
|
||||||
hnrs.append(item.name)
|
norm_name = self._make_standard_hnr(item.name)
|
||||||
|
if norm_name:
|
||||||
|
hnrs.add(norm_name)
|
||||||
elif item.kind == 'street':
|
elif item.kind == 'street':
|
||||||
streets.extend(self._retrieve_full_tokens(item.name))
|
streets.extend(self._retrieve_full_tokens(item.name))
|
||||||
elif item.kind == 'place':
|
elif item.kind == 'place':
|
||||||
@@ -431,8 +433,7 @@ class LegacyICUNameAnalyzer(AbstractAnalyzer):
|
|||||||
addr_terms.append((item.kind, self._compute_partial_tokens(item.name)))
|
addr_terms.append((item.kind, self._compute_partial_tokens(item.name)))
|
||||||
|
|
||||||
if hnrs:
|
if hnrs:
|
||||||
hnrs = self._split_housenumbers(hnrs)
|
token_info.add_housenumbers(self.conn, hnrs)
|
||||||
token_info.add_housenumbers(self.conn, [self._make_standard_hnr(n) for n in hnrs])
|
|
||||||
|
|
||||||
if addr_terms:
|
if addr_terms:
|
||||||
token_info.add_address_terms(addr_terms)
|
token_info.add_address_terms(addr_terms)
|
||||||
@@ -545,24 +546,6 @@ class LegacyICUNameAnalyzer(AbstractAnalyzer):
|
|||||||
self._cache.postcodes.add(postcode)
|
self._cache.postcodes.add(postcode)
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _split_housenumbers(hnrs):
|
|
||||||
if len(hnrs) > 1 or ',' in hnrs[0] or ';' in hnrs[0]:
|
|
||||||
# split numbers if necessary
|
|
||||||
simple_list = []
|
|
||||||
for hnr in hnrs:
|
|
||||||
simple_list.extend((x.strip() for x in re.split(r'[;,]', hnr)))
|
|
||||||
|
|
||||||
if len(simple_list) > 1:
|
|
||||||
hnrs = list(set(simple_list))
|
|
||||||
else:
|
|
||||||
hnrs = simple_list
|
|
||||||
|
|
||||||
return hnrs
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class _TokenInfo:
|
class _TokenInfo:
|
||||||
""" Collect token information to be sent back to the database.
|
""" Collect token information to be sent back to the database.
|
||||||
"""
|
"""
|
||||||
|
|||||||
56
nominatim/tokenizer/sanitizers/clean_housenumbers.py
Normal file
56
nominatim/tokenizer/sanitizers/clean_housenumbers.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
# SPDX-License-Identifier: GPL-2.0-only
|
||||||
|
#
|
||||||
|
# This file is part of Nominatim. (https://nominatim.org)
|
||||||
|
#
|
||||||
|
# Copyright (C) 2022 by the Nominatim developer community.
|
||||||
|
# For a full list of authors see the git log.
|
||||||
|
"""
|
||||||
|
Sanitizer that cleans and normalizes housenumbers.
|
||||||
|
"""
|
||||||
|
import re
|
||||||
|
|
||||||
|
class _HousenumberSanitizer:
|
||||||
|
|
||||||
|
def __init__(self, config):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def __call__(self, obj):
|
||||||
|
if not obj.address:
|
||||||
|
return
|
||||||
|
|
||||||
|
new_address = []
|
||||||
|
for item in obj.address:
|
||||||
|
if item.kind in ('housenumber', 'streetnumber', 'conscriptionnumber'):
|
||||||
|
new_address.extend(item.clone(kind='housenumber', name=n) for n in self.sanitize(item.name))
|
||||||
|
else:
|
||||||
|
# Don't touch other address items.
|
||||||
|
new_address.append(item)
|
||||||
|
|
||||||
|
obj.address = new_address
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize(self, value):
|
||||||
|
""" Extract housenumbers in a regularized format from an OSM value.
|
||||||
|
|
||||||
|
The function works as a generator that yields all valid housenumbers
|
||||||
|
that can be created from the value.
|
||||||
|
"""
|
||||||
|
for hnr in self._split_number(value):
|
||||||
|
yield from self._regularize(hnr)
|
||||||
|
|
||||||
|
|
||||||
|
def _split_number(self, hnr):
|
||||||
|
for part in re.split(r'[;,]', hnr):
|
||||||
|
yield part.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def _regularize(self, hnr):
|
||||||
|
yield hnr
|
||||||
|
|
||||||
|
|
||||||
|
def create(config):
|
||||||
|
""" Create a housenumber processing function.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return _HousenumberSanitizer(config)
|
||||||
@@ -27,6 +27,7 @@ transliteration:
|
|||||||
sanitizers:
|
sanitizers:
|
||||||
- step: split-name-list
|
- step: split-name-list
|
||||||
- step: strip-brace-terms
|
- step: strip-brace-terms
|
||||||
|
- step: clean-housenumbers
|
||||||
- step: tag-analyzer-by-language
|
- step: tag-analyzer-by-language
|
||||||
filter-kind: [".*name.*"]
|
filter-kind: [".*name.*"]
|
||||||
whitelist: [bg,ca,cs,da,de,el,en,es,et,eu,fi,fr,gl,hu,it,ja,mg,ms,nl,no,pl,pt,ro,ru,sk,sl,sv,tr,uk,vi]
|
whitelist: [bg,ca,cs,da,de,el,en,es,et,eu,fi,fr,gl,hu,it,ja,mg,ms,nl,no,pl,pt,ro,ru,sk,sl,sv,tr,uk,vi]
|
||||||
|
|||||||
55
test/bdd/db/query/housenumbers.feature
Normal file
55
test/bdd/db/query/housenumbers.feature
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
@DB
|
||||||
|
Feature: Searching of house numbers
|
||||||
|
Test for specialised treeatment of housenumbers
|
||||||
|
|
||||||
|
Background:
|
||||||
|
Given the grid
|
||||||
|
| 1 | | 2 | | 3 |
|
||||||
|
| | 9 | | | |
|
||||||
|
| | | | | 4 |
|
||||||
|
|
||||||
|
|
||||||
|
Scenario: A simple numeral housenumber is found
|
||||||
|
Given the places
|
||||||
|
| osm | class | type | housenr | geometry |
|
||||||
|
| N1 | building | yes | 45 | 9 |
|
||||||
|
And the places
|
||||||
|
| osm | class | type | name | geometry |
|
||||||
|
| W10 | highway | path | North Road | 1,2,3 |
|
||||||
|
When importing
|
||||||
|
And sending search query "45, North Road"
|
||||||
|
Then results contain
|
||||||
|
| osm |
|
||||||
|
| N1 |
|
||||||
|
When sending search query "North Road 45"
|
||||||
|
Then results contain
|
||||||
|
| osm |
|
||||||
|
| N1 |
|
||||||
|
|
||||||
|
|
||||||
|
Scenario Outline: Each housenumber in a list is found
|
||||||
|
Given the places
|
||||||
|
| osm | class | type | housenr | geometry |
|
||||||
|
| N1 | building | yes | <hnrs> | 9 |
|
||||||
|
And the places
|
||||||
|
| osm | class | type | name | geometry |
|
||||||
|
| W10 | highway | path | Multistr | 1,2,3 |
|
||||||
|
When importing
|
||||||
|
When sending search query "2 Multistr"
|
||||||
|
Then results contain
|
||||||
|
| osm |
|
||||||
|
| N1 |
|
||||||
|
When sending search query "4 Multistr"
|
||||||
|
Then results contain
|
||||||
|
| osm |
|
||||||
|
| N1 |
|
||||||
|
When sending search query "12 Multistr"
|
||||||
|
Then results contain
|
||||||
|
| osm |
|
||||||
|
| N1 |
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
| hnrs |
|
||||||
|
| 2;4;12 |
|
||||||
|
| 2,4,12 |
|
||||||
|
| 2, 4, 12 |
|
||||||
Reference in New Issue
Block a user