5 require_once(CONST_LibDir.'/SimpleWordList.php');
 
  11     private $oNormalizer = null;
 
  13     public function __construct(&$oDB)
 
  16         $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
 
  19     public function checkStatus()
 
  21         $sStandardWord = $this->oDB->getOne("SELECT make_standard_name('a')");
 
  22         if ($sStandardWord === false) {
 
  23             throw new \Exception('Module failed', 701);
 
  26         if ($sStandardWord != 'a') {
 
  27             throw new \Exception('Module call failed', 702);
 
  30         $sSQL = "SELECT word_id FROM word WHERE word_token IN (' a')";
 
  31         $iWordID = $this->oDB->getOne($sSQL);
 
  32         if ($iWordID === false) {
 
  33             throw new \Exception('Query failed', 703);
 
  36             throw new \Exception('No value', 704);
 
  41     public function normalizeString($sTerm)
 
  43         if ($this->oNormalizer === null) {
 
  47         return $this->oNormalizer->transliterate($sTerm);
 
  51     public function tokensForSpecialTerm($sTerm)
 
  55         $sSQL = 'SELECT word_id, class, type FROM word ';
 
  56         $sSQL .= '   WHERE word_token = \' \' || make_standard_name(:term)';
 
  57         $sSQL .= '   AND class is not null AND class not in (\'place\')';
 
  59         Debug::printVar('Term', $sTerm);
 
  60         Debug::printSQL($sSQL);
 
  61         $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $sTerm));
 
  63         Debug::printVar('Results', $aSearchWords);
 
  65         foreach ($aSearchWords as $aSearchTerm) {
 
  66             $aResults[] = new \Nominatim\Token\SpecialTerm(
 
  67                 $aSearchTerm['word_id'],
 
  68                 $aSearchTerm['class'],
 
  70                 \Nominatim\Operator::TYPE
 
  74         Debug::printVar('Special term tokens', $aResults);
 
  80     public function extractTokensFromPhrases(&$aPhrases)
 
  82         // First get the normalized version of all phrases
 
  86         foreach ($aPhrases as $iPhrase => $oPhrase) {
 
  87             $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
 
  88             $sSQL .= 'make_standard_name(:' .$iPhrase.') as p'.$iPhrase.',';
 
  89             $aParams[':'.$iPhrase] = $oPhrase->getPhrase();
 
  91             // Conflicts between US state abbreviations and various words
 
  92             // for 'the' in different languages
 
  93             switch (strtolower($oPhrase->getPhrase())) {
 
  95                     $aParams[':'.$iPhrase] = 'illinois';
 
  98                     $aParams[':'.$iPhrase] = 'alabama';
 
 101                     $aParams[':'.$iPhrase] = 'louisiana';
 
 104                     $aParams[':'.$iPhrase] = $oPhrase->getPhrase();
 
 108         $sSQL = substr($sSQL, 0, -1);
 
 110         Debug::printSQL($sSQL);
 
 111         Debug::printVar('SQL parameters', $aParams);
 
 113         $aNormPhrases = $this->oDB->getRow($sSQL, $aParams);
 
 115         Debug::printVar('SQL result', $aNormPhrases);
 
 117         // now compute all possible tokens
 
 118         $aWordLists = array();
 
 120         foreach ($aNormPhrases as $sPhrase) {
 
 121             $oWordList = new SimpleWordList($sPhrase);
 
 123             foreach ($oWordList->getTokens() as $sToken) {
 
 124                 $aTokens[' '.$sToken] = ' '.$sToken;
 
 125                 $aTokens[$sToken] = $sToken;
 
 128             $aWordLists[] = $oWordList;
 
 131         Debug::printVar('Tokens', $aTokens);
 
 132         Debug::printVar('WordLists', $aWordLists);
 
 134         $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
 
 136         foreach ($aPhrases as $iPhrase => $oPhrase) {
 
 137             $oPhrase->setWordSets($aWordLists[$iPhrase]->getWordSets($oValidTokens));
 
 140         return $oValidTokens;
 
 144     private function computeValidTokens($aTokens, $sNormQuery)
 
 146         $oValidTokens = new TokenList();
 
 148         if (!empty($aTokens)) {
 
 149             $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
 
 151             // Try more interpretations for Tokens that could not be matched.
 
 152             foreach ($aTokens as $sToken) {
 
 153                 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
 
 154                     if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
 
 155                         // US ZIP+4 codes - merge in the 5-digit ZIP code
 
 156                         $oValidTokens->addToken(
 
 158                             new Token\Postcode(null, $aData[1], 'us')
 
 160                     } elseif (preg_match('/^[0-9]+$/', $sToken)) {
 
 161                         // Unknown single word token with a number.
 
 162                         // Assume it is a house number.
 
 163                         $oValidTokens->addToken(
 
 165                             new Token\HouseNumber(null, trim($sToken))
 
 172         return $oValidTokens;
 
 176     private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
 
 178         // Check which tokens we have, get the ID numbers
 
 179         $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
 
 180         $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
 
 181         $sSQL .= ' FROM word WHERE word_token in (';
 
 182         $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
 
 184         Debug::printSQL($sSQL);
 
 186         $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
 
 188         foreach ($aDBWords as $aWord) {
 
 190             $iId = (int) $aWord['word_id'];
 
 192             if ($aWord['class']) {
 
 193                 // Special terms need to appear in their normalized form.
 
 194                 // (postcodes are not normalized in the word table)
 
 195                 $sNormWord = $this->normalizeString($aWord['word']);
 
 196                 if ($aWord['word'] && strpos($sNormQuery, $sNormWord) === false) {
 
 200                 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
 
 201                     $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
 
 202                 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
 
 204                         && pg_escape_string($aWord['word']) == $aWord['word']
 
 206                         $oToken = new Token\Postcode(
 
 209                             $aWord['country_code']
 
 213                     // near and in operator the same at the moment
 
 214                     $oToken = new Token\SpecialTerm(
 
 218                         $aWord['operator'] ? Operator::NEAR : Operator::NONE
 
 221             } elseif ($aWord['country_code']) {
 
 222                 $oToken = new Token\Country($iId, $aWord['country_code']);
 
 223             } elseif ($aWord['word_token'][0] == ' ') {
 
 224                 $oToken = new Token\Word(
 
 226                     (int) $aWord['count'],
 
 227                     substr_count($aWord['word_token'], ' ')
 
 229             // For backward compatibility: ignore all partial tokens with more
 
 231             } elseif (strpos($aWord['word_token'], ' ') === false) {
 
 232                 $oToken = new Token\Partial(
 
 234                     $aWord['word_token'],
 
 235                     (int) $aWord['count']
 
 240                 // remove any leading spaces
 
 241                 if ($aWord['word_token'][0] == ' ') {
 
 242                     $oValidTokens->addToken(substr($aWord['word_token'], 1), $oToken);
 
 244                     $oValidTokens->addToken($aWord['word_token'], $oToken);