X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/3cd85eaaf16f740bb984436184b37a84a94b2553..3734f1d0b84c642ba89b5a4c39bac845f5627be7:/lib-php/TokenPartial.php?ds=sidebyside diff --git a/lib-php/TokenPartial.php b/lib-php/TokenPartial.php index 99a75947..3dc6f308 100644 --- a/lib-php/TokenPartial.php +++ b/lib-php/TokenPartial.php @@ -1,4 +1,12 @@ iId = $iId; - $this->sToken = $sToken; + $this->bNumberToken = (bool) preg_match('#^[0-9 ]+$#', $sToken); $this->iSearchNameCount = $iSearchNameCount; } + public function getId() + { + return $this->iId; + } + + /** + * Check if the token can be added to the given search. + * Derive new searches by adding this token to an existing search. + * + * @param object $oSearch Partial search description derived so far. + * @param object $oPosition Description of the token position within + the query. + * + * @return True if the token is compatible with the search configuration + * given the position. + */ + public function isExtendable($oSearch, $oPosition) + { + return !$oPosition->isPhrase('country'); + } + + /** + * Derive new searches by adding this token to an existing search. + * + * @param object $oSearch Partial search description derived so far. + * @param object $oPosition Description of the token position within + the query. + * + * @return SearchDescription[] List of derived search descriptions. + */ + public function extendSearch($oSearch, $oPosition) + { + $aNewSearches = array(); + + // Partial token in Address. + if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase()) + && $oSearch->hasName() + ) { + $iSearchCost = $this->bNumberToken ? 2 : 1; + if ($this->iSearchNameCount >= CONST_Max_Word_Frequency) { + $iSearchCost += 1; + } + + $oNewSearch = $oSearch->clone($iSearchCost); + $oNewSearch->addAddressToken( + $this->iId, + $this->iSearchNameCount < CONST_Max_Word_Frequency + ); + + $aNewSearches[] = $oNewSearch; + } + + // Partial token in Name. + if ((!$oSearch->hasPostcode() && !$oSearch->hasAddress()) + && (!$oSearch->hasName(true) + || $oSearch->getNamePhrase() == $oPosition->getPhrase()) + ) { + $iSearchCost = 1; + if (!$oSearch->hasName(true)) { + $iSearchCost += 1; + } + if ($this->bNumberToken) { + $iSearchCost += 1; + } + + $oNewSearch = $oSearch->clone($iSearchCost); + $oNewSearch->addPartialNameToken( + $this->iId, + $this->iSearchNameCount < CONST_Max_Word_Frequency, + $this->iSearchNameCount > CONST_Search_NameOnlySearchFrequencyThreshold, + $oPosition->getPhrase() + ); + + $aNewSearches[] = $oNewSearch; + } + + return $aNewSearches; + } + + public function debugInfo() { return array( @@ -31,4 +119,9 @@ class Partial ) ); } + + public function debugCode() + { + return 'w'; + } }