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 limit 1";
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, info->>'class' as class, info->>'type' as type ";
59 $sSQL .= ' FROM word WHERE word_token = :term and type = \'S\'';
61 Debug::printVar('Term', $sTerm);
62 Debug::printSQL($sSQL);
63 $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $this->makeStandardWord($sTerm)));
65 Debug::printVar('Results', $aSearchWords);
67 foreach ($aSearchWords as $aSearchTerm) {
68 $aResults[] = new \Nominatim\Token\SpecialTerm(
69 $aSearchTerm['word_id'],
70 $aSearchTerm['class'],
72 \Nominatim\Operator::TYPE
76 Debug::printVar('Special term tokens', $aResults);
82 public function extractTokensFromPhrases(&$aPhrases)
85 $aWordLists = array();
87 foreach ($aPhrases as $iPhrase => $oPhrase) {
88 $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
89 $sPhrase = $this->makeStandardWord($oPhrase->getPhrase());
90 Debug::printVar('Phrase', $sPhrase);
91 if (strlen($sPhrase) > 0) {
92 $aWords = explode(' ', $sPhrase);
93 Tokenizer::addTokens($aTokens, $aWords);
94 $aWordLists[] = $aWords;
96 $aWordLists[] = array();
100 Debug::printVar('Tokens', $aTokens);
101 Debug::printVar('WordLists', $aWordLists);
103 $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
105 foreach ($aPhrases as $iPhrase => $oPhrase) {
106 $oPhrase->computeWordSets($aWordLists[$iPhrase], $oValidTokens);
109 return $oValidTokens;
113 private function computeValidTokens($aTokens, $sNormQuery)
115 $oValidTokens = new TokenList();
117 if (!empty($aTokens)) {
118 $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
120 // Try more interpretations for Tokens that could not be matched.
121 foreach ($aTokens as $sToken) {
122 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
123 if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
124 // US ZIP+4 codes - merge in the 5-digit ZIP code
125 $oValidTokens->addToken(
127 new Token\Postcode(null, $aData[1], 'us')
129 } elseif (preg_match('/^[0-9]+$/', $sToken)) {
130 // Unknown single word token with a number.
131 // Assume it is a house number.
132 $oValidTokens->addToken(
134 new Token\HouseNumber(null, trim($sToken))
141 return $oValidTokens;
145 private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
147 // Check which tokens we have, get the ID numbers
148 $sSQL = 'SELECT word_id, word_token, type,';
149 $sSQL .= " info->>'cc' as country, info->>'postcode' as postcode,";
150 $sSQL .= " info->>'op' as operator,";
151 $sSQL .= " info->>'class' as class, info->>'type' as ctype,";
152 $sSQL .= " info->>'count' as count";
153 $sSQL .= ' FROM word WHERE word_token in (';
154 $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
156 Debug::printSQL($sSQL);
158 $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
160 foreach ($aDBWords as $aWord) {
161 $iId = (int) $aWord['word_id'];
162 $sTok = $aWord['word_token'];
164 switch ($aWord['type']) {
165 case 'C': // country name tokens
166 if ($aWord['country'] !== null
167 && (!$this->aCountryRestriction
168 || in_array($aWord['country'], $this->aCountryRestriction))
170 $oValidTokens->addToken($sTok, new Token\Country($iId, $aWord['country']));
173 case 'H': // house number tokens
174 $oValidTokens->addToken($sTok, new Token\HouseNumber($iId, $aWord['word_token']));
176 case 'P': // postcode tokens
177 // Postcodes are not normalized, so they may have content
178 // that makes SQL injection possible. Reject postcodes
179 // that would need special escaping.
180 if ($aWord['postcode'] !== null
181 && pg_escape_string($aWord['postcode']) == $aWord['postcode']
183 $sNormPostcode = $this->normalizeString($aWord['postcode']);
184 if (strpos($sNormQuery, $sNormPostcode) !== false) {
185 $oValidTokens->addToken($sTok, new Token\Postcode($iId, $aWord['postcode'], null));
189 case 'S': // tokens for classification terms (special phrases)
190 if ($aWord['class'] !== null && $aWord['ctype'] !== null) {
191 $oValidTokens->addToken($sTok, new Token\SpecialTerm(
195 (isset($aWord['op'])) ? Operator::NEAR : Operator::NONE
199 case 'W': // full-word tokens
200 $oValidTokens->addToken($sTok, new Token\Word(
202 (int) $aWord['count'],
203 substr_count($aWord['word_token'], ' ')
206 case 'w': // partial word terms
207 $oValidTokens->addToken($sTok, new Token\Partial(
209 $aWord['word_token'],
210 (int) $aWord['count']
221 * Add the tokens from this phrase to the given list of tokens.
223 * @param string[] $aTokens List of tokens to append.
227 private static function addTokens(&$aTokens, $aWords)
229 $iNumWords = count($aWords);
231 for ($i = 0; $i < $iNumWords; $i++) {
232 $sPhrase = $aWords[$i];
233 $aTokens[$sPhrase] = $sPhrase;
235 for ($j = $i + 1; $j < $iNumWords; $j++) {
236 $sPhrase .= ' '.$aWords[$j];
237 $aTokens[$sPhrase] = $sPhrase;