]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenWord.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib-php / TokenWord.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 namespace Nominatim\Token;
12
13 /**
14  * A standard word token.
15  */
16 class Word
17 {
18     /// Database word id, if applicable.
19     private $iId;
20     /// Number of appearances in the database.
21     private $iSearchNameCount;
22     /// Number of terms in the word.
23     private $iTermCount;
24
25     public function __construct($iId, $iSearchNameCount, $iTermCount)
26     {
27         $this->iId = $iId;
28         $this->iSearchNameCount = $iSearchNameCount;
29         $this->iTermCount = $iTermCount;
30     }
31
32     public function getId()
33     {
34         return $this->iId;
35     }
36
37     /**
38      * Check if the token can be added to the given search.
39      * Derive new searches by adding this token to an existing search.
40      *
41      * @param object  $oSearch      Partial search description derived so far.
42      * @param object  $oPosition    Description of the token position within
43                                     the query.
44      *
45      * @return True if the token is compatible with the search configuration
46      *         given the position.
47      */
48     public function isExtendable($oSearch, $oPosition)
49     {
50         return !$oPosition->isPhrase('country');
51     }
52
53     /**
54      * Derive new searches by adding this token to an existing search.
55      *
56      * @param object  $oSearch      Partial search description derived so far.
57      * @param object  $oPosition    Description of the token position within
58                                     the query.
59      *
60      * @return SearchDescription[] List of derived search descriptions.
61      */
62     public function extendSearch($oSearch, $oPosition)
63     {
64         // Full words can only be a name if they appear at the beginning
65         // of the phrase. In structured search the name must forcibly in
66         // the first phrase. In unstructured search it may be in a later
67         // phrase when the first phrase is a house number.
68         if ($oSearch->hasName()
69             || !($oPosition->isFirstPhrase() || $oPosition->isPhrase(''))
70         ) {
71             if ($this->iTermCount > 1
72                 && ($oPosition->isPhrase('') || !$oPosition->isFirstPhrase())
73             ) {
74                 $oNewSearch = $oSearch->clone(1);
75                 $oNewSearch->addAddressToken($this->iId);
76
77                 return array($oNewSearch);
78             }
79         } elseif (!$oSearch->hasName(true)) {
80             $oNewSearch = $oSearch->clone(1);
81             $oNewSearch->addNameToken(
82                 $this->iId,
83                 CONST_Search_NameOnlySearchFrequencyThreshold
84                 && $this->iSearchNameCount
85                           < CONST_Search_NameOnlySearchFrequencyThreshold
86             );
87
88             return array($oNewSearch);
89         }
90
91         return array();
92     }
93
94     public function debugInfo()
95     {
96         return array(
97                 'ID' => $this->iId,
98                 'Type' => 'word',
99                 'Info' => array(
100                            'count' => $this->iSearchNameCount,
101                            'terms' => $this->iTermCount
102                           )
103                );
104     }
105
106     public function debugCode()
107     {
108         return 'W';
109     }
110 }