]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenPartial.php
Merge remote-tracking branch 'upstream/master'
[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                 $this->iSearchNameCount > CONST_Search_NameOnlySearchFrequencyThreshold,
94                 $oPosition->getPhrase()
95             );
96
97             $aNewSearches[] = $oNewSearch;
98         }
99
100         return $aNewSearches;
101     }
102
103
104     public function debugInfo()
105     {
106         return array(
107                 'ID' => $this->iId,
108                 'Type' => 'partial',
109                 'Info' => array(
110                            'count' => $this->iSearchNameCount
111                           )
112                );
113     }
114
115     public function debugCode()
116     {
117         return 'w';
118     }
119 }