]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenPartial.php
add postcodes for most of the remaining countries
[nominatim.git] / lib-php / TokenPartial.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 standard word token.
15  */
16 class Partial
17 {
18     /// Database word id, if applicable.
19     private $iId;
20     /// Number of appearances in the database.
21     private $iSearchNameCount;
22     /// True, if the token consists exclusively of digits and spaces.
23     private $bNumberToken;
24
25     public function __construct($iId, $sToken, $iSearchNameCount)
26     {
27         $this->iId = $iId;
28         $this->bNumberToken = (bool) preg_match('#^[0-9 ]+$#', $sToken);
29         $this->iSearchNameCount = $iSearchNameCount;
30     }
31
32     public function getId()
33     {
34         return $this->iId;
35     }
36
37     /**
38      * Check if the token can be added to the given search.
39      * Derive new searches by adding this token to an existing search.
40      *
41      * @param object  $oSearch      Partial search description derived so far.
42      * @param object  $oPosition    Description of the token position within
43                                     the query.
44      *
45      * @return True if the token is compatible with the search configuration
46      *         given the position.
47      */
48     public function isExtendable($oSearch, $oPosition)
49     {
50         return !$oPosition->isPhrase('country');
51     }
52
53     /**
54      * Derive new searches by adding this token to an existing search.
55      *
56      * @param object  $oSearch      Partial search description derived so far.
57      * @param object  $oPosition    Description of the token position within
58                                     the query.
59      *
60      * @return SearchDescription[] List of derived search descriptions.
61      */
62     public function extendSearch($oSearch, $oPosition)
63     {
64         $aNewSearches = array();
65
66         // Partial token in Address.
67         if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
68             && $oSearch->hasName()
69         ) {
70             $iSearchCost = $this->bNumberToken ? 2 : 1;
71             if ($this->iSearchNameCount >= CONST_Max_Word_Frequency) {
72                 $iSearchCost += 1;
73             }
74
75             $oNewSearch = $oSearch->clone($iSearchCost);
76             $oNewSearch->addAddressToken(
77                 $this->iId,
78                 $this->iSearchNameCount < CONST_Max_Word_Frequency
79             );
80
81             $aNewSearches[] = $oNewSearch;
82         }
83
84         // Partial token in Name.
85         if ((!$oSearch->hasPostcode() && !$oSearch->hasAddress())
86             && (!$oSearch->hasName(true)
87                 || $oSearch->getNamePhrase() == $oPosition->getPhrase())
88         ) {
89             $iSearchCost = 1;
90             if (!$oSearch->hasName(true)) {
91                 $iSearchCost += 1;
92             }
93             if ($this->bNumberToken) {
94                 $iSearchCost += 1;
95             }
96
97             $oNewSearch = $oSearch->clone($iSearchCost);
98             $oNewSearch->addPartialNameToken(
99                 $this->iId,
100                 $this->iSearchNameCount < CONST_Max_Word_Frequency,
101                 $this->iSearchNameCount > CONST_Search_NameOnlySearchFrequencyThreshold,
102                 $oPosition->getPhrase()
103             );
104
105             $aNewSearches[] = $oNewSearch;
106         }
107
108         return $aNewSearches;
109     }
110
111
112     public function debugInfo()
113     {
114         return array(
115                 'ID' => $this->iId,
116                 'Type' => 'partial',
117                 'Info' => array(
118                            'count' => $this->iSearchNameCount
119                           )
120                );
121     }
122
123     public function debugCode()
124     {
125         return 'w';
126     }
127 }