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 mostFrequentWords($iNum)
 
  53         $sSQL = 'SELECT word FROM word WHERE word is not null ';
 
  54         $sSQL .= 'ORDER BY search_name_count DESC LIMIT '.$iNum;
 
  55         return $this->oDB->getCol($sSQL);
 
  59     public function tokensForSpecialTerm($sTerm)
 
  63         $sSQL = 'SELECT word_id, class, type FROM word ';
 
  64         $sSQL .= '   WHERE word_token = \' \' || make_standard_name(:term)';
 
  65         $sSQL .= '   AND class is not null AND class not in (\'place\')';
 
  67         Debug::printVar('Term', $sTerm);
 
  68         Debug::printSQL($sSQL);
 
  69         $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $sTerm));
 
  71         Debug::printVar('Results', $aSearchWords);
 
  73         foreach ($aSearchWords as $aSearchTerm) {
 
  74             $aResults[] = new \Nominatim\Token\SpecialTerm(
 
  75                 $aSearchTerm['word_id'],
 
  76                 $aSearchTerm['class'],
 
  78                 \Nominatim\Operator::TYPE
 
  82         Debug::printVar('Special term tokens', $aResults);
 
  88     public function extractTokensFromPhrases(&$aPhrases)
 
  90         // First get the normalized version of all phrases
 
  94         foreach ($aPhrases as $iPhrase => $oPhrase) {
 
  95             $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
 
  96             $sSQL .= 'make_standard_name(:' .$iPhrase.') as p'.$iPhrase.',';
 
  97             $aParams[':'.$iPhrase] = $oPhrase->getPhrase();
 
  99             // Conflicts between US state abbreviations and various words
 
 100             // for 'the' in different languages
 
 101             switch (strtolower($oPhrase->getPhrase())) {
 
 103                     $aParams[':'.$iPhrase] = 'illinois';
 
 106                     $aParams[':'.$iPhrase] = 'alabama';
 
 109                     $aParams[':'.$iPhrase] = 'louisiana';
 
 112                     $aParams[':'.$iPhrase] = $oPhrase->getPhrase();
 
 116         $sSQL = substr($sSQL, 0, -1);
 
 118         Debug::printSQL($sSQL);
 
 119         Debug::printVar('SQL parameters', $aParams);
 
 121         $aNormPhrases = $this->oDB->getRow($sSQL, $aParams);
 
 123         Debug::printVar('SQL result', $aNormPhrases);
 
 125         // now compute all possible tokens
 
 126         $aWordLists = array();
 
 128         foreach ($aNormPhrases as $sPhrase) {
 
 129             $oWordList = new SimpleWordList($sPhrase);
 
 131             foreach ($oWordList->getTokens() as $sToken) {
 
 132                 $aTokens[' '.$sToken] = ' '.$sToken;
 
 133                 $aTokens[$sToken] = $sToken;
 
 136             $aWordLists[] = $oWordList;
 
 139         Debug::printVar('Tokens', $aTokens);
 
 140         Debug::printVar('WordLists', $aWordLists);
 
 142         $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
 
 144         foreach ($aPhrases as $iPhrase => $oPhrase) {
 
 145             $oPhrase->setWordSets($aWordLists[$iPhrase]->getWordSets($oValidTokens));
 
 148         return $oValidTokens;
 
 152     private function computeValidTokens($aTokens, $sNormQuery)
 
 154         $oValidTokens = new TokenList();
 
 156         if (!empty($aTokens)) {
 
 157             $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
 
 159             // Try more interpretations for Tokens that could not be matched.
 
 160             foreach ($aTokens as $sToken) {
 
 161                 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
 
 162                     if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
 
 163                         // US ZIP+4 codes - merge in the 5-digit ZIP code
 
 164                         $oValidTokens->addToken(
 
 166                             new Token\Postcode(null, $aData[1], 'us')
 
 168                     } elseif (preg_match('/^[0-9]+$/', $sToken)) {
 
 169                         // Unknown single word token with a number.
 
 170                         // Assume it is a house number.
 
 171                         $oValidTokens->addToken(
 
 173                             new Token\HouseNumber(null, trim($sToken))
 
 180         return $oValidTokens;
 
 184     private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
 
 186         // Check which tokens we have, get the ID numbers
 
 187         $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
 
 188         $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
 
 189         $sSQL .= ' FROM word WHERE word_token in (';
 
 190         $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
 
 192         Debug::printSQL($sSQL);
 
 194         $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
 
 196         foreach ($aDBWords as $aWord) {
 
 198             $iId = (int) $aWord['word_id'];
 
 200             if ($aWord['class']) {
 
 201                 // Special terms need to appear in their normalized form.
 
 202                 // (postcodes are not normalized in the word table)
 
 203                 $sNormWord = $this->normalizeString($aWord['word']);
 
 204                 if ($aWord['word'] && strpos($sNormQuery, $sNormWord) === false) {
 
 208                 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
 
 209                     $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
 
 210                 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
 
 212                         && pg_escape_string($aWord['word']) == $aWord['word']
 
 214                         $oToken = new Token\Postcode(
 
 217                             $aWord['country_code']
 
 221                     // near and in operator the same at the moment
 
 222                     $oToken = new Token\SpecialTerm(
 
 226                         $aWord['operator'] ? Operator::NEAR : Operator::NONE
 
 229             } elseif ($aWord['country_code']) {
 
 230                 $oToken = new Token\Country($iId, $aWord['country_code']);
 
 231             } elseif ($aWord['word_token'][0] == ' ') {
 
 232                 $oToken = new Token\Word(
 
 234                     (int) $aWord['count'],
 
 235                     substr_count($aWord['word_token'], ' ')
 
 237             // For backward compatibility: ignore all partial tokens with more
 
 239             } elseif (strpos($aWord['word_token'], ' ') === false) {
 
 240                 $oToken = new Token\Partial(
 
 242                     $aWord['word_token'],
 
 243                     (int) $aWord['count']
 
 248                 // remove any leading spaces
 
 249                 if ($aWord['word_token'][0] == ' ') {
 
 250                     $oValidTokens->addToken(substr($aWord['word_token'], 1), $oToken);
 
 252                     $oValidTokens->addToken($aWord['word_token'], $oToken);