X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/6070c3d1d58fb8737b387e8a3ef1f17fb1eb5d54..456d439e97675ca77018733f3106f507c3eedaa1:/lib-php/tokenizer/legacy_tokenizer.php?ds=sidebyside diff --git a/lib-php/tokenizer/legacy_tokenizer.php b/lib-php/tokenizer/legacy_tokenizer.php index ec2d7e68..6f3d2304 100644 --- a/lib-php/tokenizer/legacy_tokenizer.php +++ b/lib-php/tokenizer/legacy_tokenizer.php @@ -1,13 +1,22 @@ oDB->getOne("SELECT make_standard_name('a')"); if ($sStandardWord === false) { - throw new Exception('Module failed', 701); + throw new \Exception('Module failed', 701); } if ($sStandardWord != 'a') { - throw new Exception('Module call failed', 702); + throw new \Exception('Module call failed', 702); } $sSQL = "SELECT word_id FROM word WHERE word_token IN (' a')"; $iWordID = $this->oDB->getOne($sSQL); if ($iWordID === false) { - throw new Exception('Query failed', 703); + throw new \Exception('Query failed', 703); } if (!$iWordID) { - throw new Exception('No value', 704); + throw new \Exception('No value', 704); } } - public function setCountryRestriction($aCountries) - { - $this->aCountryRestriction = $aCountries; - } - - public function normalizeString($sTerm) { if ($this->oNormalizer === null) { @@ -53,6 +56,14 @@ class Tokenizer } + public function mostFrequentWords($iNum) + { + $sSQL = 'SELECT word FROM word WHERE word is not null '; + $sSQL .= 'ORDER BY search_name_count DESC LIMIT '.$iNum; + return $this->oDB->getCol($sSQL); + } + + public function tokensForSpecialTerm($sTerm) { $aResults = array(); @@ -92,6 +103,23 @@ class Tokenizer $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase()); $sSQL .= 'make_standard_name(:' .$iPhrase.') as p'.$iPhrase.','; $aParams[':'.$iPhrase] = $oPhrase->getPhrase(); + + // Conflicts between US state abbreviations and various words + // for 'the' in different languages + switch (strtolower($oPhrase->getPhrase())) { + case 'il': + $aParams[':'.$iPhrase] = 'illinois'; + break; + case 'al': + $aParams[':'.$iPhrase] = 'alabama'; + break; + case 'la': + $aParams[':'.$iPhrase] = 'louisiana'; + break; + default: + $aParams[':'.$iPhrase] = $oPhrase->getPhrase(); + break; + } } $sSQL = substr($sSQL, 0, -1); @@ -106,13 +134,14 @@ class Tokenizer $aWordLists = array(); $aTokens = array(); foreach ($aNormPhrases as $sPhrase) { - if (strlen($sPhrase) > 0) { - $aWords = explode(' ', $sPhrase); - Tokenizer::addTokens($aTokens, $aWords); - $aWordLists[] = $aWords; - } else { - $aWordLists[] = array(); + $oWordList = new SimpleWordList($sPhrase); + + foreach ($oWordList->getTokens() as $sToken) { + $aTokens[' '.$sToken] = ' '.$sToken; + $aTokens[$sToken] = $sToken; } + + $aWordLists[] = $oWordList; } Debug::printVar('Tokens', $aTokens); @@ -121,7 +150,7 @@ class Tokenizer $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery); foreach ($aPhrases as $iPhrase => $oPhrase) { - $oPhrase->computeWordSets($aWordLists[$iPhrase], $oValidTokens); + $oPhrase->setWordSets($aWordLists[$iPhrase]->getWordSets($oValidTokens)); } return $oValidTokens; @@ -137,14 +166,14 @@ class Tokenizer // Try more interpretations for Tokens that could not be matched. foreach ($aTokens as $sToken) { - if ($sToken[0] == ' ' && !$oValidTokens->contains($sToken)) { - if (preg_match('/^ ([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) { + if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) { + if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) { // US ZIP+4 codes - merge in the 5-digit ZIP code $oValidTokens->addToken( $sToken, new Token\Postcode(null, $aData[1], 'us') ); - } elseif (preg_match('/^ [0-9]+$/', $sToken)) { + } elseif (preg_match('/^[0-9]+$/', $sToken)) { // Unknown single word token with a number. // Assume it is a house number. $oValidTokens->addToken( @@ -206,21 +235,19 @@ class Tokenizer ); } } elseif ($aWord['country_code']) { - // Filter country tokens that do not match restricted countries. - if (!$this->aCountryRestriction - || in_array($aWord['country_code'], $this->aCountryRestriction) - ) { - $oToken = new Token\Country($iId, $aWord['country_code']); - } + $oToken = new Token\Country($iId, $aWord['country_code']); } elseif ($aWord['word_token'][0] == ' ') { $oToken = new Token\Word( $iId, (int) $aWord['count'], substr_count($aWord['word_token'], ' ') ); - } else { + // For backward compatibility: ignore all partial tokens with more + // than one word. + } elseif (strpos($aWord['word_token'], ' ') === false) { $oToken = new Token\Partial( $iId, + $aWord['word_token'], (int) $aWord['count'] ); } @@ -235,29 +262,4 @@ class Tokenizer } } } - - - /** - * Add the tokens from this phrase to the given list of tokens. - * - * @param string[] $aTokens List of tokens to append. - * - * @return void - */ - private static function addTokens(&$aTokens, $aWords) - { - $iNumWords = count($aWords); - - for ($i = 0; $i < $iNumWords; $i++) { - $sPhrase = $aWords[$i]; - $aTokens[' '.$sPhrase] = ' '.$sPhrase; - $aTokens[$sPhrase] = $sPhrase; - - for ($j = $i + 1; $j < $iNumWords; $j++) { - $sPhrase .= ' '.$aWords[$j]; - $aTokens[' '.$sPhrase] = ' '.$sPhrase; - $aTokens[$sPhrase] = $sPhrase; - } - } - } }