]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenHousenumber.php
Merge pull request #3367 from lonvia/address-word-counts
[nominatim.git] / lib-php / TokenHousenumber.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim\Token;
12
13 /**
14  * A house number token.
15  */
16 class HouseNumber
17 {
18     /// Database word id, if available.
19     private $iId;
20     /// Normalized house number.
21     private $sToken;
22
23     public function __construct($iId, $sToken)
24     {
25         $this->iId = $iId;
26         $this->sToken = $sToken;
27     }
28
29     public function getId()
30     {
31         return $this->iId;
32     }
33
34     /**
35      * Check if the token can be added to the given search.
36      * Derive new searches by adding this token to an existing search.
37      *
38      * @param object  $oSearch      Partial search description derived so far.
39      * @param object  $oPosition    Description of the token position within
40                                     the query.
41      *
42      * @return True if the token is compatible with the search configuration
43      *         given the position.
44      */
45     public function isExtendable($oSearch, $oPosition)
46     {
47         return !$oSearch->hasHousenumber()
48                && !$oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
49                && $oPosition->maybePhrase('street');
50     }
51
52     /**
53      * Derive new searches by adding this token to an existing search.
54      *
55      * @param object  $oSearch      Partial search description derived so far.
56      * @param object  $oPosition    Description of the token position within
57                                     the query.
58      *
59      * @return SearchDescription[] List of derived search descriptions.
60      */
61     public function extendSearch($oSearch, $oPosition)
62     {
63         $aNewSearches = array();
64
65         // sanity check: if the housenumber is not mainly made
66         // up of numbers, add a penalty
67         $iSearchCost = 1;
68         if (preg_match('/\\d/', $this->sToken) === 0
69             || preg_match_all('/[^0-9 ]/', $this->sToken, $aMatches) > 3) {
70             $iSearchCost += strlen($this->sToken) - 1;
71         }
72         if (!$oSearch->hasOperator(\Nominatim\Operator::NONE)) {
73             $iSearchCost++;
74         }
75         if (empty($this->iId)) {
76             $iSearchCost++;
77         }
78         // also must not appear in the middle of the address
79         if ($oSearch->hasAddress() || $oSearch->hasPostcode()) {
80             $iSearchCost++;
81         }
82
83         $oNewSearch = $oSearch->clone($iSearchCost);
84         $oNewSearch->setHousenumber($this->sToken);
85         $aNewSearches[] = $oNewSearch;
86
87         // Housenumbers may appear in the name when the place has its own
88         // address terms.
89         if ($this->iId !== null
90             && ($oSearch->getNamePhrase() >= 0 || !$oSearch->hasName())
91             && !$oSearch->hasAddress()
92         ) {
93             $oNewSearch = $oSearch->clone($iSearchCost);
94             $oNewSearch->setHousenumberAsName($this->iId);
95
96             $aNewSearches[] = $oNewSearch;
97         }
98
99         return $aNewSearches;
100     }
101
102
103     public function debugInfo()
104     {
105         return array(
106                 'ID' => $this->iId,
107                 'Type' => 'house number',
108                 'Info' => array('nr' => $this->sToken)
109                );
110     }
111
112     public function debugCode()
113     {
114         return 'H';
115     }
116 }