5 require_once(CONST_LibDir.'/SimpleWordList.php');
12 private $oTransliterator;
14 public function __construct(&$oDB)
17 $this->oNormalizer = \Transliterator::createFromRules(CONST_Term_Normalization_Rules);
18 $this->oTransliterator = \Transliterator::createFromRules(CONST_Transliteration);
21 public function checkStatus()
23 $sSQL = 'SELECT word_id FROM word WHERE word_id is not null limit 1';
24 $iWordID = $this->oDB->getOne($sSQL);
25 if ($iWordID === false) {
26 throw new \Exception('Query failed', 703);
29 throw new \Exception('No value', 704);
34 public function normalizeString($sTerm)
36 if ($this->oNormalizer === null) {
40 return $this->oNormalizer->transliterate($sTerm);
44 public function mostFrequentWords($iNum)
46 $sSQL = "SELECT word FROM word WHERE type = 'W'";
47 $sSQL .= "ORDER BY info->'count' DESC LIMIT ".$iNum;
48 return $this->oDB->getCol($sSQL);
52 private function makeStandardWord($sTerm)
54 return trim($this->oTransliterator->transliterate(' '.$sTerm.' '));
58 public function tokensForSpecialTerm($sTerm)
62 $sSQL = "SELECT word_id, info->>'class' as class, info->>'type' as type ";
63 $sSQL .= ' FROM word WHERE word_token = :term and type = \'S\'';
65 Debug::printVar('Term', $sTerm);
66 Debug::printSQL($sSQL);
67 $aSearchWords = $this->oDB->getAll($sSQL, array(':term' => $this->makeStandardWord($sTerm)));
69 Debug::printVar('Results', $aSearchWords);
71 foreach ($aSearchWords as $aSearchTerm) {
72 $aResults[] = new \Nominatim\Token\SpecialTerm(
73 $aSearchTerm['word_id'],
74 $aSearchTerm['class'],
76 \Nominatim\Operator::TYPE
80 Debug::printVar('Special term tokens', $aResults);
86 public function extractTokensFromPhrases(&$aPhrases)
89 $aWordLists = array();
91 foreach ($aPhrases as $iPhrase => $oPhrase) {
92 $sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
93 $sPhrase = $this->makeStandardWord($oPhrase->getPhrase());
94 Debug::printVar('Phrase', $sPhrase);
96 $oWordList = new SimpleWordList($sPhrase);
97 $aTokens = array_merge($aTokens, $oWordList->getTokens());
98 $aWordLists[] = $oWordList;
101 Debug::printVar('Tokens', $aTokens);
102 Debug::printVar('WordLists', $aWordLists);
104 $oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
106 foreach ($aPhrases as $iPhrase => $oPhrase) {
107 $oPhrase->setWordSets($aWordLists[$iPhrase]->getWordSets($oValidTokens));
110 return $oValidTokens;
114 private function computeValidTokens($aTokens, $sNormQuery)
116 $oValidTokens = new TokenList();
118 if (!empty($aTokens)) {
119 $this->addTokensFromDB($oValidTokens, $aTokens, $sNormQuery);
121 // Try more interpretations for Tokens that could not be matched.
122 foreach ($aTokens as $sToken) {
123 if ($sToken[0] != ' ' && !$oValidTokens->contains($sToken)) {
124 if (preg_match('/^([0-9]{5}) [0-9]{4}$/', $sToken, $aData)) {
125 // US ZIP+4 codes - merge in the 5-digit ZIP code
126 $oValidTokens->addToken(
128 new Token\Postcode(null, $aData[1], 'us')
130 } elseif (preg_match('/^[0-9]+$/', $sToken)) {
131 // Unknown single word token with a number.
132 // Assume it is a house number.
133 $oValidTokens->addToken(
135 new Token\HouseNumber(null, trim($sToken))
142 return $oValidTokens;
146 private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
148 // Check which tokens we have, get the ID numbers
149 $sSQL = 'SELECT word_id, word_token, type, word,';
150 $sSQL .= " info->>'op' as operator,";
151 $sSQL .= " info->>'class' as class, info->>'type' as ctype,";
152 $sSQL .= " info->>'count' as count";
153 $sSQL .= ' FROM word WHERE word_token in (';
154 $sSQL .= join(',', $this->oDB->getDBQuotedList($aTokens)).')';
156 Debug::printSQL($sSQL);
158 $aDBWords = $this->oDB->getAll($sSQL, null, 'Could not get word tokens.');
160 foreach ($aDBWords as $aWord) {
161 $iId = (int) $aWord['word_id'];
162 $sTok = $aWord['word_token'];
164 switch ($aWord['type']) {
165 case 'C': // country name tokens
166 if ($aWord['word'] !== null) {
167 $oValidTokens->addToken(
169 new Token\Country($iId, $aWord['word'])
173 case 'H': // house number tokens
174 $oValidTokens->addToken($sTok, new Token\HouseNumber($iId, $aWord['word_token']));
176 case 'P': // postcode tokens
177 // Postcodes are not normalized, so they may have content
178 // that makes SQL injection possible. Reject postcodes
179 // that would need special escaping.
180 if ($aWord['word'] !== null
181 && pg_escape_string($aWord['word']) == $aWord['word']
183 $sNormPostcode = $this->normalizeString($aWord['word']);
184 if (strpos($sNormQuery, $sNormPostcode) !== false) {
185 $oValidTokens->addToken(
187 new Token\Postcode($iId, $aWord['word'], null)
192 case 'S': // tokens for classification terms (special phrases)
193 if ($aWord['class'] !== null && $aWord['ctype'] !== null) {
194 $oValidTokens->addToken($sTok, new Token\SpecialTerm(
198 (isset($aWord['operator'])) ? Operator::NEAR : Operator::NONE
202 case 'W': // full-word tokens
203 $oValidTokens->addToken($sTok, new Token\Word(
205 (int) $aWord['count'],
206 substr_count($aWord['word_token'], ' ')
209 case 'w': // partial word terms
210 $oValidTokens->addToken($sTok, new Token\Partial(
212 $aWord['word_token'],
213 (int) $aWord['count']