From b14ce959d9532f192b748b12f6c71f23a9c1a60b Mon Sep 17 00:00:00 2001 From: Sarah Hoffmann Date: Sat, 17 Jul 2021 22:01:35 +0200 Subject: [PATCH] factor out check if a token fits current search Saves allocating an empty array. --- lib-php/Geocode.php | 18 ++++++++++-------- lib-php/TokenCountry.php | 20 ++++++++++++++++---- lib-php/TokenHousenumber.php | 25 ++++++++++++++++++------- lib-php/TokenPartial.php | 20 ++++++++++++++++---- lib-php/TokenPostcode.php | 20 ++++++++++++++++---- lib-php/TokenSpecialTerm.php | 20 ++++++++++++++++---- lib-php/TokenWord.php | 20 ++++++++++++++++---- 7 files changed, 108 insertions(+), 35 deletions(-) diff --git a/lib-php/Geocode.php b/lib-php/Geocode.php index 734f4069..82892eae 100644 --- a/lib-php/Geocode.php +++ b/lib-php/Geocode.php @@ -362,14 +362,16 @@ class Geocode foreach ($aWordsetSearches as $oCurrentSearch) { foreach ($oValidTokens->get($sToken) as $oSearchTerm) { - $aNewSearches = $oSearchTerm->extendSearch( - $oCurrentSearch, - $oPosition - ); - - foreach ($aNewSearches as $oSearch) { - if ($oSearch->getRank() < $this->iMaxRank) { - $aNewWordsetSearches[] = $oSearch; + if ($oSearchTerm->isExtendable($oCurrentSearch, $oPosition)) { + $aNewSearches = $oSearchTerm->extendSearch( + $oCurrentSearch, + $oPosition + ); + + foreach ($aNewSearches as $oSearch) { + if ($oSearch->getRank() < $this->iMaxRank) { + $aNewWordsetSearches[] = $oSearch; + } } } } diff --git a/lib-php/TokenCountry.php b/lib-php/TokenCountry.php index 917ed9d2..c9b7b6af 100644 --- a/lib-php/TokenCountry.php +++ b/lib-php/TokenCountry.php @@ -23,6 +23,22 @@ class Country 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 !$oSearch->hasCountry() && $oPosition->maybePhrase('country'); + } + /** * Derive new searches by adding this token to an existing search. * @@ -34,10 +50,6 @@ class Country */ public function extendSearch($oSearch, $oPosition) { - if ($oSearch->hasCountry() || !$oPosition->maybePhrase('country')) { - return array(); - } - $oNewSearch = $oSearch->clone($oPosition->isLastToken() ? 1 : 6); $oNewSearch->setCountry($this->sCountryCode); diff --git a/lib-php/TokenHousenumber.php b/lib-php/TokenHousenumber.php index 0cc67a12..cd60d3ca 100644 --- a/lib-php/TokenHousenumber.php +++ b/lib-php/TokenHousenumber.php @@ -23,6 +23,24 @@ class HouseNumber 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 !$oSearch->hasHousenumber() + && !$oSearch->hasOperator(\Nominatim\Operator::POSTCODE) + && $oPosition->maybePhrase('street'); + } + /** * Derive new searches by adding this token to an existing search. * @@ -36,13 +54,6 @@ class HouseNumber { $aNewSearches = array(); - if ($oSearch->hasHousenumber() - || $oSearch->hasOperator(\Nominatim\Operator::POSTCODE) - || !$oPosition->maybePhrase('street') - ) { - return $aNewSearches; - } - // sanity check: if the housenumber is not mainly made // up of numbers, add a penalty $iSearchCost = 1; diff --git a/lib-php/TokenPartial.php b/lib-php/TokenPartial.php index e52161cc..131bb2a3 100644 --- a/lib-php/TokenPartial.php +++ b/lib-php/TokenPartial.php @@ -26,6 +26,22 @@ class Partial 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. * @@ -37,10 +53,6 @@ class Partial */ public function extendSearch($oSearch, $oPosition) { - if ($oPosition->isPhrase('country')) { - return array(); - } - $aNewSearches = array(); // Partial token in Address. diff --git a/lib-php/TokenPostcode.php b/lib-php/TokenPostcode.php index 563fe7fa..c0b42fad 100644 --- a/lib-php/TokenPostcode.php +++ b/lib-php/TokenPostcode.php @@ -26,6 +26,22 @@ class Postcode 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 !$oSearch->hasPostcode() && $oPosition->maybePhrase('postalcode'); + } + /** * Derive new searches by adding this token to an existing search. * @@ -39,10 +55,6 @@ class Postcode { $aNewSearches = array(); - if ($oSearch->hasPostcode() || !$oPosition->maybePhrase('postalcode')) { - return $aNewSearches; - } - // If we have structured search or this is the first term, // make the postcode the primary search element. if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) { diff --git a/lib-php/TokenSpecialTerm.php b/lib-php/TokenSpecialTerm.php index 89dfa026..355dbb91 100644 --- a/lib-php/TokenSpecialTerm.php +++ b/lib-php/TokenSpecialTerm.php @@ -31,6 +31,22 @@ class SpecialTerm 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 !$oSearch->hasOperator() && $oPosition->isPhrase(''); + } + /** * Derive new searches by adding this token to an existing search. * @@ -42,10 +58,6 @@ class SpecialTerm */ public function extendSearch($oSearch, $oPosition) { - if ($oSearch->hasOperator() || !$oPosition->isPhrase('')) { - return array(); - } - $iSearchCost = 2; $iOp = $this->iOperator; diff --git a/lib-php/TokenWord.php b/lib-php/TokenWord.php index 7c653f8f..c9a43915 100644 --- a/lib-php/TokenWord.php +++ b/lib-php/TokenWord.php @@ -26,6 +26,22 @@ class Word 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. * @@ -37,10 +53,6 @@ class Word */ public function extendSearch($oSearch, $oPosition) { - if ($oPosition->isPhrase('country')) { - return array(); - } - // Full words can only be a name if they appear at the beginning // of the phrase. In structured search the name must forcably in // the first phrase. In unstructured search it may be in a later -- 2.43.2