]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenWord.php
move SearchDescription building into tokens
[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      * 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         // Full words can only be a name if they appear at the beginning
45         // of the phrase. In structured search the name must forcably in
46         // the first phrase. In unstructured search it may be in a later
47         // phrase when the first phrase is a house number.
48         if ($oSearch->hasName()
49             || !($oPosition->isFirstPhrase() || $oPosition->isPhrase(''))
50         ) {
51             if ($this->iTermCount > 1
52                 && ($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
53             ) {
54                 $oNewSearch = $oSearch->clone(1);
55                 $oNewSearch->addAddressToken($this->iId);
56
57                 return array($oNewSearch);
58             }
59         } elseif (!$oSearch->hasName(true)) {
60             $oNewSearch = $oSearch->clone(1);
61             $oNewSearch->addNameToken($this->iId);
62             if (CONST_Search_NameOnlySearchFrequencyThreshold
63                 && $this->iSearchNameCount
64                           < CONST_Search_NameOnlySearchFrequencyThreshold
65             ) {
66                 $oNewSearch->markRareName();
67             }
68
69             return array($oNewSearch);
70         }
71
72         return array();
73     }
74
75     public function debugInfo()
76     {
77         return array(
78                 'ID' => $this->iId,
79                 'Type' => 'word',
80                 'Info' => array(
81                            'count' => $this->iSearchNameCount,
82                            'terms' => $this->iTermCount
83                           )
84                );
85     }
86
87     public function debugCode()
88     {
89         return 'W';
90     }
91 }