3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
11 namespace Nominatim\Token;
14 * A standard word token.
18 /// Database word id, if applicable.
20 /// Number of appearances in the database.
21 private $iSearchNameCount;
22 /// True, if the token consists exclusively of digits and spaces.
23 private $bNumberToken;
25 public function __construct($iId, $sToken, $iSearchNameCount)
28 $this->bNumberToken = (bool) preg_match('#^[0-9 ]+$#', $sToken);
29 $this->iSearchNameCount = $iSearchNameCount;
32 public function getId()
38 * Check if the token can be added to the given search.
39 * Derive new searches by adding this token to an existing search.
41 * @param object $oSearch Partial search description derived so far.
42 * @param object $oPosition Description of the token position within
45 * @return True if the token is compatible with the search configuration
48 public function isExtendable($oSearch, $oPosition)
50 return !$oPosition->isPhrase('country');
54 * Derive new searches by adding this token to an existing search.
56 * @param object $oSearch Partial search description derived so far.
57 * @param object $oPosition Description of the token position within
60 * @return SearchDescription[] List of derived search descriptions.
62 public function extendSearch($oSearch, $oPosition)
64 $aNewSearches = array();
66 // Partial token in Address.
67 if (($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
68 && $oSearch->hasName()
70 $iSearchCost = $this->bNumberToken ? 2 : 1;
71 if ($this->iSearchNameCount >= CONST_Max_Word_Frequency) {
75 $oNewSearch = $oSearch->clone($iSearchCost);
76 $oNewSearch->addAddressToken(
78 $this->iSearchNameCount < CONST_Max_Word_Frequency
81 $aNewSearches[] = $oNewSearch;
84 // Partial token in Name.
85 if ((!$oSearch->hasPostcode() && !$oSearch->hasAddress())
86 && (!$oSearch->hasName(true)
87 || $oSearch->getNamePhrase() == $oPosition->getPhrase())
90 if (!$oSearch->hasName(true)) {
93 if ($this->bNumberToken) {
97 $oNewSearch = $oSearch->clone($iSearchCost);
98 $oNewSearch->addPartialNameToken(
100 $this->iSearchNameCount < CONST_Max_Word_Frequency,
101 $this->iSearchNameCount > CONST_Search_NameOnlySearchFrequencyThreshold,
102 $oPosition->getPhrase()
105 $aNewSearches[] = $oNewSearch;
108 return $aNewSearches;
112 public function debugInfo()
118 'count' => $this->iSearchNameCount
123 public function debugCode()