10 private $oTransliterator;
11 private $aCountryRestriction;
13 public function __construct(&$oDB)
16 $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
17 $this->oTransliterator = \Transliterator::createFromRules(CONST_Transliteration);
20 public function checkStatus()
22 $sSQL = "SELECT word_id FROM word WHERE word_token IN (' a')";
23 $iWordID = $this->oDB->getOne($sSQL);
24 if ($iWordID === false) {
25 throw new Exception('Query failed', 703);
28 throw new Exception('No value', 704);
33 public function setCountryRestriction($aCountries)
35 $this->aCountryRestriction = $aCountries;
39 public function normalizeString($sTerm)
41 if ($this->oNormalizer === null) {
45 return $this->oNormalizer->transliterate($sTerm);
48 private function makeStandardWord($sTerm)
50 $sNorm = ' '.$this->oTransliterator->transliterate($sTerm).' ';
52 return trim(str_replace(CONST_Abbreviations[0], CONST_Abbreviations[1], $sNorm));
56 public function tokensForSpecialTerm($sTerm)
60 $sSQL = 'SELECT word_id, class, type FROM word ';
61 $sSQL .= ' WHERE word_token = \' \' || :term';
62 $sSQL .= ' AND class is not null AND class not in (\'place\')';
64 Debug::printVar('Term', $sTerm);
65 Debug::printSQL($sSQL);
66 $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $this->makeStandardWord($sTerm)));
68 Debug::printVar('Results', $aSearchWords);
70 foreach ($aSearchWords as $aSearchTerm) {
71 $aResults[] = new \Nominatim\Token\SpecialTerm(
72 $aSearchTerm['word_id'],
73 $aSearchTerm['class'],
75 \Nominatim\Operator::TYPE
79 Debug::printVar('Special term tokens', $aResults);
85 public function extractTokensFromPhrases(&$aPhrases)
88 $aWordLists = array();
90 foreach ($aPhrases as $iPhrase => $oPhrase) {
91 $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
92 $sPhrase = $this->makeStandardWord($oPhrase->getPhrase());
93 if (strlen($sPhrase) > 0) {
94 $aWords = explode(' ', $sPhrase);
95 Tokenizer::addTokens($aTokens, $aWords);
96 $aWordLists[] = $aWords;
98 $aWordLists[] = array();
102 Debug::printVar('Tokens', $aTokens);
103 Debug::printVar('WordLists', $aWordLists);
105 $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
107 foreach ($aPhrases as $iPhrase => $oPhrase) {
108 $oPhrase->computeWordSets($aWordLists[$iPhrase], $oValidTokens);
111 return $oValidTokens;
115 private function computeValidTokens($aTokens, $sNormQuery)
117 $oValidTokens = new TokenList();
119 if (!empty($aTokens)) {
120 $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
122 // Try more interpretations for Tokens that could not be matched.
123 foreach ($aTokens as $sToken) {
124 if ($sToken[0] == ' ' && !$oValidTokens->contains($sToken)) {
125 if (preg_match('/^ ([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
126 // US ZIP+4 codes - merge in the 5-digit ZIP code
127 $oValidTokens->addToken(
129 new Token\Postcode(null, $aData[1], 'us')
131 } elseif (preg_match('/^ [0-9]+$/', $sToken)) {
132 // Unknown single word token with a number.
133 // Assume it is a house number.
134 $oValidTokens->addToken(
136 new Token\HouseNumber(null, trim($sToken))
143 return $oValidTokens;
147 private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
149 // Check which tokens we have, get the ID numbers
150 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
151 $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
152 $sSQL .= ' FROM word WHERE word_token in (';
153 $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
155 Debug::printSQL($sSQL);
157 $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
159 foreach ($aDBWords as $aWord) {
161 $iId = (int) $aWord['word_id'];
163 if ($aWord['class']) {
164 // Special terms need to appear in their normalized form.
165 // (postcodes are not normalized in the word table)
166 $sNormWord = $this->normalizeString($aWord['word']);
167 if ($aWord['word'] && strpos($sNormQuery, $sNormWord) === false) {
171 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
172 $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
173 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
175 && pg_escape_string($aWord['word']) == $aWord['word']
177 $oToken = new Token\Postcode(
180 $aWord['country_code']
184 // near and in operator the same at the moment
185 $oToken = new Token\SpecialTerm(
189 $aWord['operator'] ? Operator::NEAR : Operator::NONE
192 } elseif ($aWord['country_code']) {
193 // Filter country tokens that do not match restricted countries.
194 if (!$this->aCountryRestriction
195 || in_array($aWord['country_code'], $this->aCountryRestriction)
197 $oToken = new Token\Country($iId, $aWord['country_code']);
200 $oToken = new Token\Word(
202 $aWord['word_token'][0] != ' ',
203 (int) $aWord['count'],
204 substr_count($aWord['word_token'], ' ')
209 $oValidTokens->addToken($aWord['word_token'], $oToken);
216 * Add the tokens from this phrase to the given list of tokens.
218 * @param string[] $aTokens List of tokens to append.
222 private static function addTokens(&$aTokens, $aWords)
224 $iNumWords = count($aWords);
226 for ($i = 0; $i < $iNumWords; $i++) {
227 $sPhrase = $aWords[$i];
228 $aTokens[' '.$sPhrase] = ' '.$sPhrase;
229 $aTokens[$sPhrase] = $sPhrase;
231 for ($j = $i + 1; $j < $iNumWords; $j++) {
232 $sPhrase .= ' '.$aWords[$j];
233 $aTokens[' '.$sPhrase] = ' '.$sPhrase;
234 $aTokens[$sPhrase] = $sPhrase;