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 ($oValidTokens->get($sToken) as $oSearchTerm) {
$aNewSearches = $oSearchTerm->extendSearch(
$oCurrentSearch,
$oPosition
);
if ($oSearchTerm->isExtendable($oCurrentSearch, $oPosition)) {
$aNewSearches = $oSearchTerm->extendSearch(
$oCurrentSearch,
$oPosition
);
foreach ($aNewSearches as $oSearch) {
if ($oSearch->getRank() < $this->iMaxRank) {
$aNewWordsetSearches[] = $oSearch;
foreach ($aNewSearches as $oSearch) {
if ($oSearch->getRank() < $this->iMaxRank) {
$aNewWordsetSearches[] = $oSearch;
}
}
}
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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.

View File

@@ -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()) {

View File

@@ -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;

View File

@@ -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