]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenPartial.php
e52161cc0168953f30fed95296cef5c196c64bcb
[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      * Derive new searches by adding this token to an existing search.
31      *
32      * @param object  $oSearch      Partial search description derived so far.
33      * @param object  $oPosition    Description of the token position within
34                                     the query.
35      *
36      * @return SearchDescription[] List of derived search descriptions.
37      */
38     public function extendSearch($oSearch, $oPosition)
39     {
40         if ($oPosition->isPhrase('country')) {
41             return array();
42         }
43
44         $aNewSearches = array();
45
46         // Partial token in Address.
47         if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
48             && $oSearch->hasName()
49         ) {
50             $iSearchCost = $this->bNumberToken ? 2 : 1;
51             if ($this->iSearchNameCount >= CONST_Max_Word_Frequency) {
52                 $iSearchCost += 1;
53             }
54
55             $oNewSearch = $oSearch->clone($iSearchCost);
56             $oNewSearch->addAddressToken(
57                 $this->iId,
58                 $this->iSearchNameCount < CONST_Max_Word_Frequency
59             );
60
61             $aNewSearches[] = $oNewSearch;
62         }
63
64         // Partial token in Name.
65         if ((!$oSearch->hasPostcode() && !$oSearch->hasAddress())
66             && (!$oSearch->hasName(true)
67                 || $oSearch->getNamePhrase() == $oPosition->getPhrase())
68         ) {
69             $iSearchCost = 1;
70             if (!$oSearch->hasName(true)) {
71                 $iSearchCost += 1;
72             }
73             if ($this->bNumberToken) {
74                 $iSearchCost += 1;
75             }
76
77             $oNewSearch = $oSearch->clone($iSearchCost);
78             $oNewSearch->addPartialNameToken(
79                 $this->iId,
80                 $this->iSearchNameCount < CONST_Max_Word_Frequency,
81                 $oPosition->getPhrase()
82             );
83
84             $aNewSearches[] = $oNewSearch;
85         }
86
87         return $aNewSearches;
88     }
89
90
91     public function debugInfo()
92     {
93         return array(
94                 'ID' => $this->iId,
95                 'Type' => 'partial',
96                 'Info' => array(
97                            'count' => $this->iSearchNameCount
98                           )
99                );
100     }
101
102     public function debugCode()
103     {
104         return 'w';
105     }
106 }