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