9 private $oNormalizer = null;
10 private $aCountryRestriction = null;
12 public function __construct(&$oDB)
15 $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
19 public function setCountryRestriction($aCountries)
21 $this->aCountryRestriction = $aCountries;
25 public function normalizeString($sTerm)
27 if ($this->oNormalizer === null) {
31 return $this->oNormalizer->transliterate($sTerm);
35 public function tokensForSpecialTerm($sTerm)
39 $sSQL = 'SELECT word_id, class, type FROM word ';
40 $sSQL .= ' WHERE word_token = \' \' || make_standard_name(:term)';
41 $sSQL .= ' AND class is not null AND class not in (\'place\')';
43 Debug::printVar('Term', $sTerm);
44 Debug::printSQL($sSQL);
45 $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $sTerm));
47 Debug::printVar('Results', $aSearchWords);
49 foreach ($aSearchWords as $aSearchTerm) {
50 $aResults[] = new \Nominatim\Token\SpecialTerm(
51 $aSearchTerm['word_id'],
52 $aSearchTerm['class'],
54 \Nominatim\Operator::TYPE
58 Debug::printVar('Special term tokens', $aResults);
64 public function extractTokensFromPhrases(&$aPhrases)
66 // First get the normalized version of all phrases
70 foreach ($aPhrases as $iPhrase => $oPhrase) {
71 $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
72 $sSQL .= 'make_standard_name(:' .$iPhrase.') as p'.$iPhrase.',';
73 $aParams[':'.$iPhrase] = $oPhrase->getPhrase();
75 $sSQL = substr($sSQL, 0, -1);
77 Debug::printSQL($sSQL);
78 Debug::printVar('SQL parameters', $aParams);
80 $aNormPhrases = $this->oDB->getRow($sSQL, $aParams);
82 Debug::printVar('SQL result', $aNormPhrases);
84 // now compute all possible tokens
85 $aWordLists = array();
87 foreach ($aNormPhrases as $sTitle => $sPhrase) {
88 if (strlen($sPhrase) > 0) {
89 $aWords = explode(' ', $sPhrase);
90 Tokenizer::addTokens($aTokens, $aWords);
91 $aWordLists[] = $aWords;
93 $aWordLists[] = array();
97 Debug::printVar('Tokens', $aTokens);
98 Debug::printVar('WordLists', $aWordLists);
100 $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
102 foreach ($aPhrases as $iPhrase => $oPhrase) {
103 $oPhrase->computeWordSets($aWordLists[$iPhrase], $oValidTokens);
106 return $oValidTokens;
110 private function computeValidTokens($aTokens, $sNormQuery)
112 $oValidTokens = new TokenList();
114 if (!empty($aTokens)) {
115 $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
117 // Try more interpretations for Tokens that could not be matched.
118 foreach ($aTokens as $sToken) {
119 if ($sToken[0] == ' ' && !$oValidTokens->contains($sToken)) {
120 if (preg_match('/^ ([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
121 // US ZIP+4 codes - merge in the 5-digit ZIP code
122 $oValidTokens->addToken(
124 new Token\Postcode(null, $aData[1], 'us')
126 } elseif (preg_match('/^ [0-9]+$/', $sToken)) {
127 // Unknown single word token with a number.
128 // Assume it is a house number.
129 $oValidTokens->addToken(
131 new Token\HouseNumber(null, trim($sToken))
138 return $oValidTokens;
142 private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
144 // Check which tokens we have, get the ID numbers
145 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
146 $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
147 $sSQL .= ' FROM word WHERE word_token in (';
148 $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
150 Debug::printSQL($sSQL);
152 $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
154 foreach ($aDBWords as $aWord) {
156 $iId = (int) $aWord['word_id'];
158 if ($aWord['class']) {
159 // Special terms need to appear in their normalized form.
160 // (postcodes are not normalized in the word table)
161 $sNormWord = $this->normalizeString($aWord['word']);
162 if ($aWord['word'] && strpos($sNormQuery, $sNormWord) === false) {
166 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
167 $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
168 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
170 && pg_escape_string($aWord['word']) == $aWord['word']
172 $oToken = new Token\Postcode(
175 $aWord['country_code']
179 // near and in operator the same at the moment
180 $oToken = new Token\SpecialTerm(
184 $aWord['operator'] ? Operator::NEAR : Operator::NONE
187 } elseif ($aWord['country_code']) {
188 // Filter country tokens that do not match restricted countries.
189 if (!$this->aCountryRestriction
190 || in_array($aWord['country_code'], $this->aCountryRestriction)
192 $oToken = new Token\Country($iId, $aWord['country_code']);
195 $oToken = new Token\Word(
197 $aWord['word_token'][0] != ' ',
198 (int) $aWord['count'],
199 substr_count($aWord['word_token'], ' ')
204 $oValidTokens->addToken($aWord['word_token'], $oToken);
211 * Add the tokens from this phrase to the given list of tokens.
213 * @param string[] $aTokens List of tokens to append.
217 private static function addTokens(&$aTokens, $aWords)
219 $iNumWords = count($aWords);
221 for ($i = 0; $i < $iNumWords; $i++) {
222 $sPhrase = $aWords[$i];
223 $aTokens[' '.$sPhrase] = ' '.$sPhrase;
224 $aTokens[$sPhrase] = $sPhrase;
226 for ($j = $i + 1; $j < $iNumWords; $j++) {
227 $sPhrase .= ' '.$aWords[$j];
228 $aTokens[' '.$sPhrase] = ' '.$sPhrase;
229 $aTokens[$sPhrase] = $sPhrase;