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