namespace Nominatim;
+require_once(CONST_LibDir.'/SimpleWordList.php');
+
class Tokenizer
{
private $oDB;
private $oNormalizer;
private $oTransliterator;
- private $aCountryRestriction;
public function __construct(&$oDB)
{
public function checkStatus()
{
- $sSQL = "SELECT word_id FROM word limit 1";
+ $sSQL = 'SELECT word_id FROM word WHERE word_id is not null limit 1';
$iWordID = $this->oDB->getOne($sSQL);
if ($iWordID === false) {
- throw new Exception('Query failed', 703);
+ throw new \Exception('Query failed', 703);
}
if (!$iWordID) {
- throw new Exception('No value', 704);
+ throw new \Exception('No value', 704);
}
}
- public function setCountryRestriction($aCountries)
- {
- $this->aCountryRestriction = $aCountries;
- }
-
-
public function normalizeString($sTerm)
{
if ($this->oNormalizer === null) {
$sNormQuery .= ','.$this->normalizeString($oPhrase->getPhrase());
$sPhrase = $this->makeStandardWord($oPhrase->getPhrase());
Debug::printVar('Phrase', $sPhrase);
- if (strlen($sPhrase) > 0) {
- $aWords = explode(' ', $sPhrase);
- Tokenizer::addTokens($aTokens, $aWords);
- $aWordLists[] = $aWords;
- } else {
- $aWordLists[] = array();
- }
+
+ $oWordList = new SimpleWordList($sPhrase);
+ $aTokens = array_merge($aTokens, $oWordList->getTokens());
+ $aWordLists[] = $oWordList;
}
Debug::printVar('Tokens', $aTokens);
$oValidTokens = $this->computeValidTokens($aTokens, $sNormQuery);
foreach ($aPhrases as $iPhrase => $oPhrase) {
- $oPhrase->computeWordSets($aWordLists[$iPhrase], $oValidTokens);
+ $oPhrase->setWordSets($aWordLists[$iPhrase]->getWordSets($oValidTokens));
}
return $oValidTokens;
private function addTokensFromDB(&$oValidTokens, $aTokens, $sNormQuery)
{
// Check which tokens we have, get the ID numbers
- $sSQL = 'SELECT word_id, word_token, type,';
- $sSQL .= " info->>'cc' as country, info->>'postcode' as postcode,";
+ $sSQL = 'SELECT word_id, word_token, type, word,';
$sSQL .= " info->>'op' as operator,";
$sSQL .= " info->>'class' as class, info->>'type' as ctype,";
$sSQL .= " info->>'count' as count";
switch ($aWord['type']) {
case 'C': // country name tokens
- if ($aWord['country'] !== null
- && (!$this->aCountryRestriction
- || in_array($aWord['country'], $this->aCountryRestriction))
- ) {
- $oValidTokens->addToken($sTok, new Token\Country($iId, $aWord['country']));
+ if ($aWord['word'] !== null) {
+ $oValidTokens->addToken(
+ $sTok,
+ new Token\Country($iId, $aWord['word'])
+ );
}
break;
case 'H': // house number tokens
// Postcodes are not normalized, so they may have content
// that makes SQL injection possible. Reject postcodes
// that would need special escaping.
- if ($aWord['postcode'] !== null
- && pg_escape_string($aWord['postcode']) == $aWord['postcode']
+ if ($aWord['word'] !== null
+ && pg_escape_string($aWord['word']) == $aWord['word']
) {
- $sNormPostcode = $this->normalizeString($aWord['postcode']);
+ $sNormPostcode = $this->normalizeString($aWord['word']);
if (strpos($sNormQuery, $sNormPostcode) !== false) {
- $oValidTokens->addToken($sTok, new Token\Postcode($iId, $aWord['postcode'], null));
+ $oValidTokens->addToken(
+ $sTok,
+ new Token\Postcode($iId, $aWord['word'], null)
+ );
}
}
break;
$iId,
$aWord['class'],
$aWord['ctype'],
- (isset($aWord['op'])) ? Operator::NEAR : Operator::NONE
+ (isset($aWord['operator'])) ? Operator::NEAR : Operator::NONE
));
}
break;
}
}
}
-
-
- /**
- * Add the tokens from this phrase to the given list of tokens.
- *
- * @param string[] $aTokens List of tokens to append.
- *
- * @return void
- */
- private static function addTokens(&$aTokens, $aWords)
- {
- $iNumWords = count($aWords);
-
- for ($i = 0; $i < $iNumWords; $i++) {
- $sPhrase = $aWords[$i];
- $aTokens[$sPhrase] = $sPhrase;
-
- for ($j = $i + 1; $j < $iNumWords; $j++) {
- $sPhrase .= ' '.$aWords[$j];
- $aTokens[$sPhrase] = $sPhrase;
- }
- }
- }
}