factor out check if a token fits current search

Saves allocating an empty array.
This commit is contained in:
Sarah Hoffmann
2021-07-17 22:01:35 +02:00
parent a48ebd9b47
commit b14ce959d9
7 changed files with 107 additions and 34 deletions

View File

@@ -362,14 +362,16 @@ class Geocode
foreach ($aWordsetSearches as $oCurrentSearch) { foreach ($aWordsetSearches as $oCurrentSearch) {
foreach ($oValidTokens->get($sToken) as $oSearchTerm) { foreach ($oValidTokens->get($sToken) as $oSearchTerm) {
$aNewSearches = $oSearchTerm->extendSearch( if ($oSearchTerm->isExtendable($oCurrentSearch, $oPosition)) {
$oCurrentSearch, $aNewSearches = $oSearchTerm->extendSearch(
$oPosition $oCurrentSearch,
); $oPosition
);
foreach ($aNewSearches as $oSearch) { foreach ($aNewSearches as $oSearch) {
if ($oSearch->getRank() < $this->iMaxRank) { if ($oSearch->getRank() < $this->iMaxRank) {
$aNewWordsetSearches[] = $oSearch; $aNewWordsetSearches[] = $oSearch;
}
} }
} }
} }

View File

@@ -23,6 +23,22 @@ class Country
return $this->iId; 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. * Derive new searches by adding this token to an existing search.
* *
@@ -34,10 +50,6 @@ class Country
*/ */
public function extendSearch($oSearch, $oPosition) public function extendSearch($oSearch, $oPosition)
{ {
if ($oSearch->hasCountry() || !$oPosition->maybePhrase('country')) {
return array();
}
$oNewSearch = $oSearch->clone($oPosition->isLastToken() ? 1 : 6); $oNewSearch = $oSearch->clone($oPosition->isLastToken() ? 1 : 6);
$oNewSearch->setCountry($this->sCountryCode); $oNewSearch->setCountry($this->sCountryCode);

View File

@@ -23,6 +23,24 @@ class HouseNumber
return $this->iId; 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. * Derive new searches by adding this token to an existing search.
* *
@@ -36,13 +54,6 @@ class HouseNumber
{ {
$aNewSearches = array(); $aNewSearches = array();
if ($oSearch->hasHousenumber()
|| $oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
|| !$oPosition->maybePhrase('street')
) {
return $aNewSearches;
}
// sanity check: if the housenumber is not mainly made // sanity check: if the housenumber is not mainly made
// up of numbers, add a penalty // up of numbers, add a penalty
$iSearchCost = 1; $iSearchCost = 1;

View File

@@ -26,6 +26,22 @@ class Partial
return $this->iId; 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. * Derive new searches by adding this token to an existing search.
* *
@@ -37,10 +53,6 @@ class Partial
*/ */
public function extendSearch($oSearch, $oPosition) public function extendSearch($oSearch, $oPosition)
{ {
if ($oPosition->isPhrase('country')) {
return array();
}
$aNewSearches = array(); $aNewSearches = array();
// Partial token in Address. // Partial token in Address.

View File

@@ -26,6 +26,22 @@ class Postcode
return $this->iId; 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. * Derive new searches by adding this token to an existing search.
* *
@@ -39,10 +55,6 @@ class Postcode
{ {
$aNewSearches = array(); $aNewSearches = array();
if ($oSearch->hasPostcode() || !$oPosition->maybePhrase('postalcode')) {
return $aNewSearches;
}
// If we have structured search or this is the first term, // If we have structured search or this is the first term,
// make the postcode the primary search element. // make the postcode the primary search element.
if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) { if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) {

View File

@@ -31,6 +31,22 @@ class SpecialTerm
return $this->iId; 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. * Derive new searches by adding this token to an existing search.
* *
@@ -42,10 +58,6 @@ class SpecialTerm
*/ */
public function extendSearch($oSearch, $oPosition) public function extendSearch($oSearch, $oPosition)
{ {
if ($oSearch->hasOperator() || !$oPosition->isPhrase('')) {
return array();
}
$iSearchCost = 2; $iSearchCost = 2;
$iOp = $this->iOperator; $iOp = $this->iOperator;

View File

@@ -26,6 +26,22 @@ class Word
return $this->iId; 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. * Derive new searches by adding this token to an existing search.
* *
@@ -37,10 +53,6 @@ class Word
*/ */
public function extendSearch($oSearch, $oPosition) public function extendSearch($oSearch, $oPosition)
{ {
if ($oPosition->isPhrase('country')) {
return array();
}
// Full words can only be a name if they appear at the beginning // Full words can only be a name if they appear at the beginning
// of the phrase. In structured search the name must forcably in // of the phrase. In structured search the name must forcably in
// the first phrase. In unstructured search it may be in a later // the first phrase. In unstructured search it may be in a later