9 private $oNormalizer = null;
11 public function __construct(&$oDB)
14 $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
17 public function checkStatus()
19 $sStandardWord = $this->oDB->getOne("SELECT make_standard_name('a')");
20 if ($sStandardWord === false) {
21 throw new \Exception('Module failed', 701);
24 if ($sStandardWord != 'a') {
25 throw new \Exception('Module call failed', 702);
28 $sSQL = "SELECT word_id FROM word WHERE word_token IN (' a')";
29 $iWordID = $this->oDB->getOne($sSQL);
30 if ($iWordID === false) {
31 throw new \Exception('Query failed', 703);
34 throw new \Exception('No value', 704);
39 public function normalizeString($sTerm)
41 if ($this->oNormalizer === null) {
45 return $this->oNormalizer->transliterate($sTerm);
49 public function tokensForSpecialTerm($sTerm)
53 $sSQL = 'SELECT word_id, class, type FROM word ';
54 $sSQL .= ' WHERE word_token = \' \' || make_standard_name(:term)';
55 $sSQL .= ' AND class is not null AND class not in (\'place\')';
57 Debug::printVar('Term', $sTerm);
58 Debug::printSQL($sSQL);
59 $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $sTerm));
61 Debug::printVar('Results', $aSearchWords);
63 foreach ($aSearchWords as $aSearchTerm) {
64 $aResults[] = new \Nominatim\Token\SpecialTerm(
65 $aSearchTerm['word_id'],
66 $aSearchTerm['class'],
68 \Nominatim\Operator::TYPE
72 Debug::printVar('Special term tokens', $aResults);
78 public function extractTokensFromPhrases(&$aPhrases)
80 // First get the normalized version of all phrases
84 foreach ($aPhrases as $iPhrase => $oPhrase) {
85 $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
86 $sSQL .= 'make_standard_name(:' .$iPhrase.') as p'.$iPhrase.',';
87 $aParams[':'.$iPhrase] = $oPhrase->getPhrase();
89 $sSQL = substr($sSQL, 0, -1);
91 Debug::printSQL($sSQL);
92 Debug::printVar('SQL parameters', $aParams);
94 $aNormPhrases = $this->oDB->getRow($sSQL, $aParams);
96 Debug::printVar('SQL result', $aNormPhrases);
98 // now compute all possible tokens
99 $aWordLists = array();
101 foreach ($aNormPhrases as $sPhrase) {
102 if (strlen($sPhrase) > 0) {
103 $aWords = explode(' ', $sPhrase);
104 Tokenizer::addTokens($aTokens, $aWords);
105 $aWordLists[] = $aWords;
107 $aWordLists[] = array();
111 Debug::printVar('Tokens', $aTokens);
112 Debug::printVar('WordLists', $aWordLists);
114 $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
116 foreach ($aPhrases as $iPhrase => $oPhrase) {
117 $oPhrase->computeWordSets($aWordLists[$iPhrase], $oValidTokens);
120 return $oValidTokens;
124 private function computeValidTokens($aTokens, $sNormQuery)
126 $oValidTokens = new TokenList();
128 if (!empty($aTokens)) {
129 $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
131 // Try more interpretations for Tokens that could not be matched.
132 foreach ($aTokens as $sToken) {
133 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
134 if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
135 // US ZIP+4 codes - merge in the 5-digit ZIP code
136 $oValidTokens->addToken(
138 new Token\Postcode(null, $aData[1], 'us')
140 } elseif (preg_match('/^[0-9]+$/', $sToken)) {
141 // Unknown single word token with a number.
142 // Assume it is a house number.
143 $oValidTokens->addToken(
145 new Token\HouseNumber(null, trim($sToken))
152 return $oValidTokens;
156 private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
158 // Check which tokens we have, get the ID numbers
159 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
160 $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
161 $sSQL .= ' FROM word WHERE word_token in (';
162 $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
164 Debug::printSQL($sSQL);
166 $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
168 foreach ($aDBWords as $aWord) {
170 $iId = (int) $aWord['word_id'];
172 if ($aWord['class']) {
173 // Special terms need to appear in their normalized form.
174 // (postcodes are not normalized in the word table)
175 $sNormWord = $this->normalizeString($aWord['word']);
176 if ($aWord['word'] && strpos($sNormQuery, $sNormWord) === false) {
180 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
181 $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
182 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
184 && pg_escape_string($aWord['word']) == $aWord['word']
186 $oToken = new Token\Postcode(
189 $aWord['country_code']
193 // near and in operator the same at the moment
194 $oToken = new Token\SpecialTerm(
198 $aWord['operator'] ? Operator::NEAR : Operator::NONE
201 } elseif ($aWord['country_code']) {
202 $oToken = new Token\Country($iId, $aWord['country_code']);
203 } elseif ($aWord['word_token'][0] == ' ') {
204 $oToken = new Token\Word(
206 (int) $aWord['count'],
207 substr_count($aWord['word_token'], ' ')
209 // For backward compatibility: ignore all partial tokens with more
211 } elseif (strpos($aWord['word_token'], ' ') === false) {
212 $oToken = new Token\Partial(
214 $aWord['word_token'],
215 (int) $aWord['count']
220 // remove any leading spaces
221 if ($aWord['word_token'][0] == ' ') {
222 $oValidTokens->addToken(substr($aWord['word_token'], 1), $oToken);
224 $oValidTokens->addToken($aWord['word_token'], $oToken);
232 * Add the tokens from this phrase to the given list of tokens.
234 * @param string[] $aTokens List of tokens to append.
238 private static function addTokens(&$aTokens, $aWords)
240 $iNumWords = count($aWords);
242 for ($i = 0; $i < $iNumWords; $i++) {
243 $sPhrase = $aWords[$i];
244 $aTokens[' '.$sPhrase] = ' '.$sPhrase;
245 $aTokens[$sPhrase] = $sPhrase;
247 for ($j = $i + 1; $j < $iNumWords; $j++) {
248 $sPhrase .= ' '.$aWords[$j];
249 $aTokens[' '.$sPhrase] = ' '.$sPhrase;
250 $aTokens[$sPhrase] = $sPhrase;