9 private $oNormalizer = null;
10 private $aCountryRestriction = null;
12 public function __construct(&$oDB)
15 $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
18 public function checkStatus()
20 $sStandardWord = $this->oDB->getOne("SELECT make_standard_name('a')");
21 if ($sStandardWord === false) {
22 throw new \Exception('Module failed', 701);
25 if ($sStandardWord != 'a') {
26 throw new \Exception('Module call failed', 702);
29 $sSQL = "SELECT word_id FROM word WHERE word_token IN (' a')";
30 $iWordID = $this->oDB->getOne($sSQL);
31 if ($iWordID === false) {
32 throw new \Exception('Query failed', 703);
35 throw new \Exception('No value', 704);
40 public function setCountryRestriction($aCountries)
42 $this->aCountryRestriction = $aCountries;
46 public function normalizeString($sTerm)
48 if ($this->oNormalizer === null) {
52 return $this->oNormalizer->transliterate($sTerm);
56 public function tokensForSpecialTerm($sTerm)
60 $sSQL = 'SELECT word_id, class, type FROM word ';
61 $sSQL .= ' WHERE word_token = \' \' || make_standard_name(: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' => $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)
87 // First get the normalized version of all phrases
91 foreach ($aPhrases as $iPhrase => $oPhrase) {
92 $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
93 $sSQL .= 'make_standard_name(:' .$iPhrase.') as p'.$iPhrase.',';
94 $aParams[':'.$iPhrase] = $oPhrase->getPhrase();
96 $sSQL = substr($sSQL, 0, -1);
98 Debug::printSQL($sSQL);
99 Debug::printVar('SQL parameters', $aParams);
101 $aNormPhrases = $this->oDB->getRow($sSQL, $aParams);
103 Debug::printVar('SQL result', $aNormPhrases);
105 // now compute all possible tokens
106 $aWordLists = array();
108 foreach ($aNormPhrases as $sPhrase) {
109 if (strlen($sPhrase) > 0) {
110 $aWords = explode(' ', $sPhrase);
111 Tokenizer::addTokens($aTokens, $aWords);
112 $aWordLists[] = $aWords;
114 $aWordLists[] = array();
118 Debug::printVar('Tokens', $aTokens);
119 Debug::printVar('WordLists', $aWordLists);
121 $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
123 foreach ($aPhrases as $iPhrase => $oPhrase) {
124 $oPhrase->computeWordSets($aWordLists[$iPhrase], $oValidTokens);
127 return $oValidTokens;
131 private function computeValidTokens($aTokens, $sNormQuery)
133 $oValidTokens = new TokenList();
135 if (!empty($aTokens)) {
136 $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
138 // Try more interpretations for Tokens that could not be matched.
139 foreach ($aTokens as $sToken) {
140 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
141 if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
142 // US ZIP+4 codes - merge in the 5-digit ZIP code
143 $oValidTokens->addToken(
145 new Token\Postcode(null, $aData[1], 'us')
147 } elseif (preg_match('/^[0-9]+$/', $sToken)) {
148 // Unknown single word token with a number.
149 // Assume it is a house number.
150 $oValidTokens->addToken(
152 new Token\HouseNumber(null, trim($sToken))
159 return $oValidTokens;
163 private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
165 // Check which tokens we have, get the ID numbers
166 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
167 $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
168 $sSQL .= ' FROM word WHERE word_token in (';
169 $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
171 Debug::printSQL($sSQL);
173 $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
175 foreach ($aDBWords as $aWord) {
177 $iId = (int) $aWord['word_id'];
179 if ($aWord['class']) {
180 // Special terms need to appear in their normalized form.
181 // (postcodes are not normalized in the word table)
182 $sNormWord = $this->normalizeString($aWord['word']);
183 if ($aWord['word'] && strpos($sNormQuery, $sNormWord) === false) {
187 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
188 $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
189 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
191 && pg_escape_string($aWord['word']) == $aWord['word']
193 $oToken = new Token\Postcode(
196 $aWord['country_code']
200 // near and in operator the same at the moment
201 $oToken = new Token\SpecialTerm(
205 $aWord['operator'] ? Operator::NEAR : Operator::NONE
208 } elseif ($aWord['country_code']) {
209 // Filter country tokens that do not match restricted countries.
210 if (!$this->aCountryRestriction
211 || in_array($aWord['country_code'], $this->aCountryRestriction)
213 $oToken = new Token\Country($iId, $aWord['country_code']);
215 } elseif ($aWord['word_token'][0] == ' ') {
216 $oToken = new Token\Word(
218 (int) $aWord['count'],
219 substr_count($aWord['word_token'], ' ')
221 // For backward compatibility: ignore all partial tokens with more
223 } elseif (strpos($aWord['word_token'], ' ') === false) {
224 $oToken = new Token\Partial(
226 $aWord['word_token'],
227 (int) $aWord['count']
232 // remove any leading spaces
233 if ($aWord['word_token'][0] == ' ') {
234 $oValidTokens->addToken(substr($aWord['word_token'], 1), $oToken);
236 $oValidTokens->addToken($aWord['word_token'], $oToken);
244 * Add the tokens from this phrase to the given list of tokens.
246 * @param string[] $aTokens List of tokens to append.
250 private static function addTokens(&$aTokens, $aWords)
252 $iNumWords = count($aWords);
254 for ($i = 0; $i < $iNumWords; $i++) {
255 $sPhrase = $aWords[$i];
256 $aTokens[' '.$sPhrase] = ' '.$sPhrase;
257 $aTokens[$sPhrase] = $sPhrase;
259 for ($j = $i + 1; $j < $iNumWords; $j++) {
260 $sPhrase .= ' '.$aWords[$j];
261 $aTokens[' '.$sPhrase] = ' '.$sPhrase;
262 $aTokens[$sPhrase] = $sPhrase;