]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenList.php
2df9fe0586710f120c821b09f809f286cd616f44
[nominatim.git] / lib-php / TokenList.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_LibDir.'/TokenCountry.php');
6 require_once(CONST_LibDir.'/TokenHousenumber.php');
7 require_once(CONST_LibDir.'/TokenPostcode.php');
8 require_once(CONST_LibDir.'/TokenSpecialTerm.php');
9 require_once(CONST_LibDir.'/TokenWord.php');
10 require_once(CONST_LibDir.'/SpecialSearchOperator.php');
11
12 /**
13  * Saves information about the tokens that appear in a search query.
14  *
15  * Tokens are sorted by their normalized form, the token word. There are different
16  * kinds of tokens, represented by different Token* classes. Note that
17  * tokens do not have a common base class. All tokens need to have a field
18  * with the word id that points to an entry in the `word` database table
19  * but otherwise the information saved about a token can be very different.
20  *
21  * There are two different kinds of token words: full words and partial terms.
22  *
23  * Full words start with a space. They represent a complete name of a place.
24  * All special tokens are normally full words.
25  *
26  * Partial terms have no space at the beginning. They may represent a part of
27  * a name of a place (e.g. in the name 'World Trade Center' a partial term
28  * would be 'Trade' or 'Trade Center'). They are only used in TokenWord.
29  */
30 class TokenList
31 {
32     // List of list of tokens indexed by their word_token.
33     private $aTokens = array();
34
35
36     /**
37      * Return total number of tokens.
38      *
39      * @return Integer
40      */
41     public function count()
42     {
43         return count($this->aTokens);
44     }
45
46     /**
47      * Check if there are tokens for the given token word.
48      *
49      * @param string $sWord Token word to look for.
50      *
51      * @return bool True if there is one or more token for the token word.
52      */
53     public function contains($sWord)
54     {
55         return isset($this->aTokens[$sWord]);
56     }
57
58     /**
59      * Check if there are partial or full tokens for the given word.
60      *
61      * @param string $sWord Token word to look for.
62      *
63      * @return bool True if there is one or more token for the token word.
64      */
65     public function containsAny($sWord)
66     {
67         return isset($this->aTokens[$sWord]) || isset($this->aTokens[' '.$sWord]);
68     }
69
70     /**
71      * Get the list of tokens for the given token word.
72      *
73      * @param string $sWord Token word to look for.
74      *
75      * @return object[] Array of tokens for the given token word or an
76      *                  empty array if no tokens could be found.
77      */
78     public function get($sWord)
79     {
80         return isset($this->aTokens[$sWord]) ? $this->aTokens[$sWord] : array();
81     }
82
83     public function getFullWordIDs()
84     {
85         $ids = array();
86
87         foreach ($this->aTokens as $aTokenList) {
88             foreach ($aTokenList as $oToken) {
89                 if (is_a($oToken, '\Nominatim\Token\Word') && !$oToken->bPartial) {
90                     $ids[$oToken->iId] = $oToken->iId;
91                 }
92             }
93         }
94
95         return $ids;
96     }
97
98     /**
99      * Add a new token for the given word.
100      *
101      * @param string $sWord  Word the token describes.
102      * @param object $oToken Token object to add.
103      *
104      * @return void
105      */
106     public function addToken($sWord, $oToken)
107     {
108         if (isset($this->aTokens[$sWord])) {
109             $this->aTokens[$sWord][] = $oToken;
110         } else {
111             $this->aTokens[$sWord] = array($oToken);
112         }
113     }
114
115     public function debugTokenByWordIdList()
116     {
117         $aWordsIDs = array();
118         foreach ($this->aTokens as $sToken => $aWords) {
119             foreach ($aWords as $aToken) {
120                 if ($aToken->iId !== null) {
121                     $aWordsIDs[$aToken->iId] =
122                         '#'.$sToken.'('.$aToken->iId.')#';
123                 }
124             }
125         }
126
127         return $aWordsIDs;
128     }
129
130     public function debugInfo()
131     {
132         return $this->aTokens;
133     }
134 }