2021-07-13 17:54:51 +03:00
|
|
|
<?php
|
2022-01-03 18:23:58 +03:00
|
|
|
/**
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
*
|
|
|
|
* This file is part of Nominatim. (https://nominatim.org)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2022 by the Nominatim developer community.
|
|
|
|
* For a full list of authors see the git log.
|
|
|
|
*/
|
2021-07-13 17:54:51 +03:00
|
|
|
|
|
|
|
namespace Nominatim\Token;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A standard word token.
|
|
|
|
*/
|
|
|
|
class Partial
|
|
|
|
{
|
|
|
|
/// Database word id, if applicable.
|
2021-07-17 21:24:33 +03:00
|
|
|
private $iId;
|
2021-07-13 17:54:51 +03:00
|
|
|
/// Number of appearances in the database.
|
2021-07-17 21:24:33 +03:00
|
|
|
private $iSearchNameCount;
|
|
|
|
/// True, if the token consists exclusively of digits and spaces.
|
|
|
|
private $bNumberToken;
|
2021-07-13 17:54:51 +03:00
|
|
|
|
2021-07-15 15:48:20 +03:00
|
|
|
public function __construct($iId, $sToken, $iSearchNameCount)
|
2021-07-13 17:54:51 +03:00
|
|
|
{
|
|
|
|
$this->iId = $iId;
|
2021-07-17 21:24:33 +03:00
|
|
|
$this->bNumberToken = (bool) preg_match('#^[0-9 ]+$#', $sToken);
|
2021-07-13 17:54:51 +03:00
|
|
|
$this->iSearchNameCount = $iSearchNameCount;
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:24:33 +03:00
|
|
|
public function getId()
|
|
|
|
{
|
|
|
|
return $this->iId;
|
|
|
|
}
|
|
|
|
|
2021-07-17 23:01:35 +03:00
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
|
2021-07-17 21:24:33 +03:00
|
|
|
/**
|
|
|
|
* 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,
|
2021-10-26 11:23:55 +03:00
|
|
|
$this->iSearchNameCount > CONST_Search_NameOnlySearchFrequencyThreshold,
|
2021-07-17 21:24:33 +03:00
|
|
|
$oPosition->getPhrase()
|
|
|
|
);
|
|
|
|
|
|
|
|
$aNewSearches[] = $oNewSearch;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $aNewSearches;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-13 17:54:51 +03:00
|
|
|
public function debugInfo()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'ID' => $this->iId,
|
|
|
|
'Type' => 'partial',
|
|
|
|
'Info' => array(
|
|
|
|
'count' => $this->iSearchNameCount
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-07-17 21:24:33 +03:00
|
|
|
|
|
|
|
public function debugCode()
|
|
|
|
{
|
|
|
|
return 'w';
|
|
|
|
}
|
2021-07-13 17:54:51 +03:00
|
|
|
}
|