5 require_once(CONST_LibDir.'/SimpleWordList.php');
11 private $oNormalizer = null;
13 public function __construct(&$oDB)
16 $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
19 public function checkStatus()
21 $sStandardWord = $this->oDB->getOne("SELECT make_standard_name('a')");
22 if ($sStandardWord === false) {
23 throw new \Exception('Module failed', 701);
26 if ($sStandardWord != 'a') {
27 throw new \Exception('Module call failed', 702);
30 $sSQL = "SELECT word_id FROM word WHERE word_token IN (' a')";
31 $iWordID = $this->oDB->getOne($sSQL);
32 if ($iWordID === false) {
33 throw new \Exception('Query failed', 703);
36 throw new \Exception('No value', 704);
41 public function normalizeString($sTerm)
43 if ($this->oNormalizer === null) {
47 return $this->oNormalizer->transliterate($sTerm);
51 public function tokensForSpecialTerm($sTerm)
55 $sSQL = 'SELECT word_id, class, type FROM word ';
56 $sSQL .= ' WHERE word_token = \' \' || make_standard_name(:term)';
57 $sSQL .= ' AND class is not null AND class not in (\'place\')';
59 Debug::printVar('Term', $sTerm);
60 Debug::printSQL($sSQL);
61 $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $sTerm));
63 Debug::printVar('Results', $aSearchWords);
65 foreach ($aSearchWords as $aSearchTerm) {
66 $aResults[] = new \Nominatim\Token\SpecialTerm(
67 $aSearchTerm['word_id'],
68 $aSearchTerm['class'],
70 \Nominatim\Operator::TYPE
74 Debug::printVar('Special term tokens', $aResults);
80 public function extractTokensFromPhrases(&$aPhrases)
82 // First get the normalized version of all phrases
86 foreach ($aPhrases as $iPhrase => $oPhrase) {
87 $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
88 $sSQL .= 'make_standard_name(:' .$iPhrase.') as p'.$iPhrase.',';
89 $aParams[':'.$iPhrase] = $oPhrase->getPhrase();
91 $sSQL = substr($sSQL, 0, -1);
93 Debug::printSQL($sSQL);
94 Debug::printVar('SQL parameters', $aParams);
96 $aNormPhrases = $this->oDB->getRow($sSQL, $aParams);
98 Debug::printVar('SQL result', $aNormPhrases);
100 // now compute all possible tokens
101 $aWordLists = array();
103 foreach ($aNormPhrases as $sPhrase) {
104 $oWordList = new SimpleWordList($sPhrase);
106 foreach ($oWordList->getTokens() as $sToken) {
107 $aTokens[' '.$sToken] = ' '.$sToken;
108 $aTokens[$sToken] = $sToken;
111 $aWordLists[] = $oWordList;
114 Debug::printVar('Tokens', $aTokens);
115 Debug::printVar('WordLists', $aWordLists);
117 $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
119 foreach ($aPhrases as $iPhrase => $oPhrase) {
120 $oPhrase->setWordSets($aWordLists[$iPhrase]->getWordSets($oValidTokens));
123 return $oValidTokens;
127 private function computeValidTokens($aTokens, $sNormQuery)
129 $oValidTokens = new TokenList();
131 if (!empty($aTokens)) {
132 $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
134 // Try more interpretations for Tokens that could not be matched.
135 foreach ($aTokens as $sToken) {
136 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
137 if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
138 // US ZIP+4 codes - merge in the 5-digit ZIP code
139 $oValidTokens->addToken(
141 new Token\Postcode(null, $aData[1], 'us')
143 } elseif (preg_match('/^[0-9]+$/', $sToken)) {
144 // Unknown single word token with a number.
145 // Assume it is a house number.
146 $oValidTokens->addToken(
148 new Token\HouseNumber(null, trim($sToken))
155 return $oValidTokens;
159 private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
161 // Check which tokens we have, get the ID numbers
162 $sSQL = 'SELECT word_id, word_token, word, class, type, country_code,';
163 $sSQL .= ' operator, coalesce(search_name_count, 0) as count';
164 $sSQL .= ' FROM word WHERE word_token in (';
165 $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
167 Debug::printSQL($sSQL);
169 $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
171 foreach ($aDBWords as $aWord) {
173 $iId = (int) $aWord['word_id'];
175 if ($aWord['class']) {
176 // Special terms need to appear in their normalized form.
177 // (postcodes are not normalized in the word table)
178 $sNormWord = $this->normalizeString($aWord['word']);
179 if ($aWord['word'] && strpos($sNormQuery, $sNormWord) === false) {
183 if ($aWord['class'] == 'place' && $aWord['type'] == 'house') {
184 $oToken = new Token\HouseNumber($iId, trim($aWord['word_token']));
185 } elseif ($aWord['class'] == 'place' && $aWord['type'] == 'postcode') {
187 && pg_escape_string($aWord['word']) == $aWord['word']
189 $oToken = new Token\Postcode(
192 $aWord['country_code']
196 // near and in operator the same at the moment
197 $oToken = new Token\SpecialTerm(
201 $aWord['operator'] ? Operator::NEAR : Operator::NONE
204 } elseif ($aWord['country_code']) {
205 $oToken = new Token\Country($iId, $aWord['country_code']);
206 } elseif ($aWord['word_token'][0] == ' ') {
207 $oToken = new Token\Word(
209 (int) $aWord['count'],
210 substr_count($aWord['word_token'], ' ')
212 // For backward compatibility: ignore all partial tokens with more
214 } elseif (strpos($aWord['word_token'], ' ') === false) {
215 $oToken = new Token\Partial(
217 $aWord['word_token'],
218 (int) $aWord['count']
223 // remove any leading spaces
224 if ($aWord['word_token'][0] == ' ') {
225 $oValidTokens->addToken(substr($aWord['word_token'], 1), $oToken);
227 $oValidTokens->addToken($aWord['word_token'], $oToken);