]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenWord.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib-php / TokenWord.php
1 <?php
2
3 namespace Nominatim\Token;
4
5 /**
6  * A standard word token.
7  */
8 class Word
9 {
10     /// Database word id, if applicable.
11     private $iId;
12     /// Number of appearances in the database.
13     private $iSearchNameCount;
14     /// Number of terms in the word.
15     private $iTermCount;
16
17     public function __construct($iId, $iSearchNameCount, $iTermCount)
18     {
19         $this->iId = $iId;
20         $this->iSearchNameCount = $iSearchNameCount;
21         $this->iTermCount = $iTermCount;
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         // Full words can only be a name if they appear at the beginning
57         // of the phrase. In structured search the name must forcably in
58         // the first phrase. In unstructured search it may be in a later
59         // phrase when the first phrase is a house number.
60         if ($oSearch->hasName()
61             || !($oPosition->isFirstPhrase() || $oPosition->isPhrase(''))
62         ) {
63             if ($this->iTermCount > 1
64                 && ($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
65             ) {
66                 $oNewSearch = $oSearch->clone(1);
67                 $oNewSearch->addAddressToken($this->iId);
68
69                 return array($oNewSearch);
70             }
71         } elseif (!$oSearch->hasName(true)) {
72             $oNewSearch = $oSearch->clone(1);
73             $oNewSearch->addNameToken(
74                 $this->iId,
75                 CONST_Search_NameOnlySearchFrequencyThreshold
76                 && $this->iSearchNameCount
77                           < CONST_Search_NameOnlySearchFrequencyThreshold
78             );
79
80             return array($oNewSearch);
81         }
82
83         return array();
84     }
85
86     public function debugInfo()
87     {
88         return array(
89                 'ID' => $this->iId,
90                 'Type' => 'word',
91                 'Info' => array(
92                            'count' => $this->iSearchNameCount,
93                            'terms' => $this->iTermCount
94                           )
95                );
96     }
97
98     public function debugCode()
99     {
100         return 'W';
101     }
102 }