]> git.openstreetmap.org Git - nominatim.git/blob - lib/TokenList.php
Merge pull request #1758 from krahulreddy/advanced-installations
[nominatim.git] / lib / TokenList.php
1 <?php
2
3 namespace Nominatim;
4
5 require_once(CONST_BasePath.'/lib/TokenCountry.php');
6 require_once(CONST_BasePath.'/lib/TokenHousenumber.php');
7 require_once(CONST_BasePath.'/lib/TokenPostcode.php');
8 require_once(CONST_BasePath.'/lib/TokenSpecialTerm.php');
9 require_once(CONST_BasePath.'/lib/TokenWord.php');
10 require_once(CONST_BasePath.'/lib/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     /**
84      * Add token information from the word table in the database.
85      *
86      * @param object   $oDB           Nominatim::DB instance.
87      * @param string[] $aTokens       List of tokens to look up in the database.
88      * @param string[] $aCountryCodes List of country restrictions.
89      * @param string   $sNormQuery    Normalized query string.
90      * @param object   $oNormalizer   Normalizer function to use on tokens.
91      *
92      * @return void
93      */
94     public function addTokensFromDB(&$oDB, &$aTokens, &$aCountryCodes, $sNormQuery, $oNormalizer)
95     {
96         // Check which tokens we have, get the ID numbers
97         $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
98         $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
99         $sSQL .= ' FROM word WHERE word_token in (';
100         $sSQL .= join(',', $oDB->getDBQuotedList($aTokens)).')';
101
102         Debug::printSQL($sSQL);
103
104         $aDBWords = $oDB->getAll($sSQL, null, 'Could not get word tokens.');
105
106         foreach ($aDBWords as $aWord) {
107             $oToken = null;
108             $iId = (int) $aWord['word_id'];
109
110             if ($aWord['class']) {
111                 // Special terms need to appear in their normalized form.
112                 if ($aWord['word']) {
113                     $sNormWord = $aWord['word'];
114                     if ($oNormalizer != null) {
115                         $sNormWord = $oNormalizer->transliterate($aWord['word']);
116                     }
117                     if (strpos($sNormQuery, $sNormWord) === false) {
118                         continue;
119                     }
120                 }
121
122                 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
123                     $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
124                 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
125                     if ($aWord['word']
126                         && pg_escape_string($aWord['word']) == $aWord['word']
127                     ) {
128                         $oToken = new Token\Postcode(
129                             $iId,
130                             $aWord['word'],
131                             $aWord['country_code']
132                         );
133                     }
134                 } else {
135                     // near and in operator the same at the moment
136                     $oToken = new Token\SpecialTerm(
137                         $iId,
138                         $aWord['class'],
139                         $aWord['type'],
140                         $aWord['operator'] ? Operator::NEAR : Operator::NONE
141                     );
142                 }
143             } elseif ($aWord['country_code']) {
144                 // Filter country tokens that do not match restricted countries.
145                 if (!$aCountryCodes
146                     || in_array($aWord['country_code'], $aCountryCodes)
147                 ) {
148                     $oToken = new Token\Country($iId, $aWord['country_code']);
149                 }
150             } else {
151                 $oToken = new Token\Word(
152                     $iId,
153                     $aWord['word_token'][0] != ' ',
154                     (int) $aWord['count']
155                 );
156             }
157
158             if ($oToken) {
159                 $this->addToken($aWord['word_token'], $oToken);
160             }
161         }
162     }
163
164     /**
165      * Add a new token for the given word.
166      *
167      * @param string $sWord  Word the token describes.
168      * @param object $oToken Token object to add.
169      *
170      * @return void
171      */
172     public function addToken($sWord, $oToken)
173     {
174         if (isset($this->aTokens[$sWord])) {
175             $this->aTokens[$sWord][] = $oToken;
176         } else {
177             $this->aTokens[$sWord] = array($oToken);
178         }
179     }
180
181     public function debugTokenByWordIdList()
182     {
183         $aWordsIDs = array();
184         foreach ($this->aTokens as $sToken => $aWords) {
185             foreach ($aWords as $aToken) {
186                 if ($aToken->iId !== null) {
187                     $aWordsIDs[$aToken->iId] =
188                         '#'.$sToken.'('.$aToken->iId.')#';
189                 }
190             }
191         }
192
193         return $aWordsIDs;
194     }
195
196     public function debugInfo()
197     {
198         return $this->aTokens;
199     }
200 }