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