]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenWord.php
factor out check if a token fits current search
[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($this->iId);
74             if (CONST_Search_NameOnlySearchFrequencyThreshold
75                 && $this->iSearchNameCount
76                           < CONST_Search_NameOnlySearchFrequencyThreshold
77             ) {
78                 $oNewSearch->markRareName();
79             }
80
81             return array($oNewSearch);
82         }
83
84         return array();
85     }
86
87     public function debugInfo()
88     {
89         return array(
90                 'ID' => $this->iId,
91                 'Type' => 'word',
92                 'Info' => array(
93                            'count' => $this->iSearchNameCount,
94                            'terms' => $this->iTermCount
95                           )
96                );
97     }
98
99     public function debugCode()
100     {
101         return 'W';
102     }
103 }