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 return trim($this->oTransliterator->transliterate(' '.$sTerm.' '));
54 public function tokensForSpecialTerm($sTerm)
58 $sSQL = 'SELECT word_id, class, type FROM word ';
59 $sSQL .= ' WHERE word_token = \' \' || :term';
60 $sSQL .= ' AND class is not null AND class not in (\'place\')';
62 Debug::printVar('Term', $sTerm);
63 Debug::printSQL($sSQL);
64 $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $this->makeStandardWord($sTerm)));
66 Debug::printVar('Results', $aSearchWords);
68 foreach ($aSearchWords as $aSearchTerm) {
69 $aResults[] = new \Nominatim\Token\SpecialTerm(
70 $aSearchTerm['word_id'],
71 $aSearchTerm['class'],
73 \Nominatim\Operator::TYPE
77 Debug::printVar('Special term tokens', $aResults);
83 public function extractTokensFromPhrases(&$aPhrases)
86 $aWordLists = array();
88 foreach ($aPhrases as $iPhrase => $oPhrase) {
89 $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
90 $sPhrase = $this->makeStandardWord($oPhrase->getPhrase());
91 Debug::printVar('Phrase', $sPhrase);
92 if (strlen($sPhrase) > 0) {
93 $aWords = explode(' ', $sPhrase);
94 Tokenizer::addTokens($aTokens, $aWords);
95 $aWordLists[] = $aWords;
97 $aWordLists[] = array();
101 Debug::printVar('Tokens', $aTokens);
102 Debug::printVar('WordLists', $aWordLists);
104 $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
106 foreach ($aPhrases as $iPhrase => $oPhrase) {
107 $oPhrase->computeWordSets($aWordLists[$iPhrase], $oValidTokens);
110 return $oValidTokens;
114 private function computeValidTokens($aTokens, $sNormQuery)
116 $oValidTokens = new TokenList();
118 if (!empty($aTokens)) {
119 $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
121 // Try more interpretations for Tokens that could not be matched.
122 foreach ($aTokens as $sToken) {
123 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
124 if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
125 // US ZIP+4 codes - merge in the 5-digit ZIP code
126 $oValidTokens->addToken(
128 new Token\Postcode(null, $aData[1], 'us')
130 } elseif (preg_match('/^[0-9]+$/', $sToken)) {
131 // Unknown single word token with a number.
132 // Assume it is a house number.
133 $oValidTokens->addToken(
135 new Token\HouseNumber(null, trim($sToken))
142 return $oValidTokens;
146 private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
148 // Check which tokens we have, get the ID numbers
149 $sSQL = 'SELECT word_id, word_token, type';
150 $sSQL .= " info->>'cc' as country, info->>'postcode' as postcode,";
151 $sSQL .= " info->>'op' as operator,";
152 $sSQL .= " info->>'class' as class, info->>'type' as type,";
153 $sSQL .= " info->>'count' as count";
154 $sSQL .= ' FROM word WHERE word_token in (';
155 $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
157 Debug::printSQL($sSQL);
159 $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
161 foreach ($aDBWords as $aWord) {
162 $iId = (int) $aWord['word_id'];
164 switch ($aWord['type']) {
165 'C': // country name tokens
166 if ($aWord['country'] === null
167 || ($this->aCountryRestriction
168 && !in_array($aWord['country'], $this->aCountryRestriction))
172 $oToken = new Token\Country($iId, $aWord['country'])
174 'H': // house number tokens
175 $oToken = new Token\HouseNumber($iId, $aWord['word_token']);
177 'P': // postcode tokens
178 // Postcodes are not normalized, so they may have content
179 // that makes SQL injection possible. Reject postcodes
180 // that would need special escaping.
181 if ($aWord['postcode'] === null
182 || pg_escape_string($aWord['postcode']) == $aWord['postcode']
186 $sNormPostcode = $this->normalizeString($aWord['postcode']);
187 if (strpos($sNormQuery, $sNormPostcode) === false) {
190 $oToken = new Token\Postcode($iId, $aWord['postcode'], null);
192 'S': // tokens for classification terms (special phrases)
193 if ($aWord['class'] === null || $aWord['type'] === null
197 $oToken = new Token\SpecialTerm(
201 $aWord['op'] ? Operator::NEAR : Operator::NONE
204 'W': // full-word tokens
205 $oToken = new Token\Word(
207 (int) $aWord['count'],
208 substr_count($aWord['word_token'], ' ')
211 'w': // partial word terms
212 $oToken = new Token\Partial(
214 $aWord['word_token'],
215 (int) $aWord['count']
222 $oValidTokens->addToken($aWord['word_token'], $oToken);
228 * Add the tokens from this phrase to the given list of tokens.
230 * @param string[] $aTokens List of tokens to append.
234 private static function addTokens(&$aTokens, $aWords)
236 $iNumWords = count($aWords);
238 for ($i = 0; $i < $iNumWords; $i++) {
239 $sPhrase = $aWords[$i];
240 $aTokens[$sPhrase] = $sPhrase;
242 for ($j = $i + 1; $j < $iNumWords; $j++) {
243 $sPhrase .= ' '.$aWords[$j];
244 $aTokens[$sPhrase] = $sPhrase;