X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/b1e8db7ca7e5b134eca8255bebd130fe85774837..16268f92ccd11f0c11fe07b2a001b197af3a5b92:/lib/Geocode.php
diff --git a/lib/Geocode.php b/lib/Geocode.php
index 96ebbb13..cce38de9 100644
--- a/lib/Geocode.php
+++ b/lib/Geocode.php
@@ -313,12 +313,7 @@ class Geocode
if (count($aViewbox) != 4) {
userError("Bad parmater 'viewbox'. Expected 4 coordinates.");
}
- $this->setViewBox(array(
- $aViewbox[0],
- $aViewbox[3],
- $aViewbox[2],
- $aViewbox[1]
- ));
+ $this->setViewBox($aViewbox);
} else {
$aRoute = $oParams->getStringList('route');
$fRouteWidth = $oParams->getFloat('routewidth');
@@ -416,7 +411,10 @@ class Geocode
//$aPlaceIDs is an array with key: placeID and value: tiger-housenumber, if found, else -1
if (sizeof($aPlaceIDs) == 0) return array();
- $sLanguagePrefArraySQL = "ARRAY[".join(',', array_map("getDBQuoted", $this->aLangPrefOrder))."]";
+ $sLanguagePrefArraySQL = getArraySQL(
+ array_map("getDBQuoted",
+ $this->aLangPrefOrder)
+ );
// Get the details for display (is this a redundant extra step?)
$sPlaceIDs = join(',', array_keys($aPlaceIDs));
@@ -714,10 +712,15 @@ class Geocode
Score how good the search is so they can be ordered
*/
+ $iGlobalRank = 0;
+
foreach ($aPhrases as $iPhrase => $aPhrase) {
$aNewPhraseSearches = array();
- if ($bStructuredPhrases) $sPhraseType = $aPhraseTypes[$iPhrase];
- else $sPhraseType = '';
+ if ($bStructuredPhrases) {
+ $sPhraseType = $aPhraseTypes[$iPhrase];
+ } else {
+ $sPhraseType = '';
+ }
foreach ($aPhrase['wordsets'] as $iWordSet => $aWordset) {
// Too many permutations - too expensive
@@ -730,150 +733,60 @@ class Geocode
//echo "
$sToken";
$aNewWordsetSearches = array();
- foreach ($aWordsetSearches as $aCurrentSearch) {
+ foreach ($aWordsetSearches as $oCurrentSearch) {
//echo "";
- //var_dump($aCurrentSearch);
+ //var_dump($oCurrentSearch);
//echo "";
// If the token is valid
if (isset($aValidTokens[' '.$sToken])) {
+ // Recheck if the original word shows up in the query.
+ $bWordInQuery = false;
+ if (isset($aSearchTerm['word']) && $aSearchTerm['word']) {
+ $bWordInQuery = $this->normTerm($aSearchTerm['word'])) !== false;
+ }
foreach ($aValidTokens[' '.$sToken] as $aSearchTerm) {
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank']++;
- if (($sPhraseType == '' || $sPhraseType == 'country') && !empty($aSearchTerm['country_code']) && $aSearchTerm['country_code'] != '0') {
- if ($aSearch['sCountryCode'] === false) {
- $aSearch['sCountryCode'] = strtolower($aSearchTerm['country_code']);
- // Country is almost always at the end of the string - increase score for finding it anywhere else (optimisation)
- if (($iToken+1 != sizeof($aWordset) || $iPhrase+1 != sizeof($aPhrases))) {
- $aSearch['iSearchRank'] += 5;
- }
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- } elseif ($sPhraseType == 'postalcode' || ($aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'postcode')) {
- // We need to try the case where the postal code is the primary element (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode) so try both
- if ($aSearch['sPostcode'] === '' && $aSearch['sHouseNumber'] === '' &&
- isset($aSearchTerm['word_id']) && $aSearchTerm['word_id'] && strpos($sNormQuery, $this->normTerm($aSearchTerm['word'])) !== false) {
- // If we have structured search or this is the first term,
- // make the postcode the primary search element.
- if ($aSearch['sOperator'] === '' && ($sPhraseType == 'postalcode' || ($iToken == 0 && $iPhrase == 0))) {
- $aNewSearch = $aSearch;
- $aNewSearch['sOperator'] = 'postcode';
- $aNewSearch['aAddress'] = array_merge($aNewSearch['aAddress'], $aNewSearch['aName']);
- $aNewSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word'];
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aNewSearch;
- }
-
- // If we have a structured search or this is not the first term,
- // add the postcode as an addendum.
- if ($aSearch['sOperator'] !== 'postcode' && ($sPhraseType == 'postalcode' || sizeof($aSearch['aName']))) {
- $aSearch['sPostcode'] = $aSearchTerm['word'];
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- }
- } elseif (($sPhraseType == '' || $sPhraseType == 'street') && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house') {
- if ($aSearch['sHouseNumber'] === '' && $aSearch['sOperator'] !== 'postcode') {
- $aSearch['sHouseNumber'] = $sToken;
- // sanity check: if the housenumber is not mainly made
- // up of numbers, add a penalty
- if (preg_match_all("/[^0-9]/", $sToken, $aMatches) > 2) $aSearch['iSearchRank']++;
- // also housenumbers should appear in the first or second phrase
- if ($iPhrase > 1) $aSearch['iSearchRank'] += 1;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- /*
- // Fall back to not searching for this item (better than nothing)
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank'] += 1;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- */
+ $aNewSearches = $oCurrentSearch->extendWithFullTerm(
+ $aSearchTerm,
+ $bWordInQuery,
+ isset($aValidTokens[$sToken])
+ && strpos($sToken, ' ') === false,
+ $sPhraseType,
+ $iToken == 0 && $iPhrase == 0,
+ $iPhrase == 0,
+ $iToken + 1 == sizeof($aWordset)
+ && $iPhrase + 1 == sizeof($aPhrases),
+ $iGlobalRank
+ );
+
+ foreach ($aNewSearches as $oSearch) {
+ if ($oSearch->getRank() < $this->iMaxRank) {
+ $aNewWordsetSearches[] = $oSearch;
}
- } elseif ($sPhraseType == '' && $aSearchTerm['class'] !== '' && $aSearchTerm['class'] !== null) {
- // require a normalized exact match of the term
- // if we have the normalizer version of the query
- // available
- if ($aSearch['sClass'] === ''
- && ($sNormQuery === null || !($aSearchTerm['word'] && strpos($sNormQuery, $aSearchTerm['word']) === false))) {
- $aSearch['sClass'] = $aSearchTerm['class'];
- $aSearch['sType'] = $aSearchTerm['type'];
- if ($aSearchTerm['operator'] == '') {
- $aSearch['sOperator'] = sizeof($aSearch['aName']) ? 'name' : 'near';
- $aSearch['iSearchRank'] += 2;
- } else {
- $aSearch['sOperator'] = 'near'; // near = in for the moment
- }
-
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- } elseif (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
- if (sizeof($aSearch['aName'])) {
- if ((!$bStructuredPhrases || $iPhrase > 0) && $sPhraseType != 'country' && (!isset($aValidTokens[$sToken]) || strpos($sToken, ' ') !== false)) {
- $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- } else {
- $aCurrentSearch['aFullNameAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- $aSearch['iSearchRank'] += 1000; // skip;
- }
- } else {
- $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- //$aSearch['iNamePhrase'] = $iPhrase;
- }
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
}
}
}
// Look for partial matches.
// Note that there is no point in adding country terms here
- // because country are omitted in the address.
+ // because country is omitted in the address.
if (isset($aValidTokens[$sToken]) && $sPhraseType != 'country') {
// Allow searching for a word - but at extra cost
foreach ($aValidTokens[$sToken] as $aSearchTerm) {
- if (isset($aSearchTerm['word_id']) && $aSearchTerm['word_id']) {
- if ((!$bStructuredPhrases || $iPhrase > 0) && sizeof($aCurrentSearch['aName']) && strpos($sToken, ' ') === false) {
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank'] += 1;
- if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency) {
- $aSearch['aAddress'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- } elseif (isset($aValidTokens[' '.$sToken])) { // revert to the token version?
- $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- $aSearch['iSearchRank'] += 1;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- foreach ($aValidTokens[' '.$sToken] as $aSearchTermToken) {
- if (empty($aSearchTermToken['country_code'])
- && empty($aSearchTermToken['lat'])
- && empty($aSearchTermToken['class'])
- ) {
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank'] += 1;
- $aSearch['aAddress'][$aSearchTermToken['word_id']] = $aSearchTermToken['word_id'];
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- }
- } else {
- $aSearch['aAddressNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
- }
- }
-
- if (!sizeof($aCurrentSearch['aName']) || $aCurrentSearch['iNamePhrase'] == $iPhrase) {
- $aSearch = $aCurrentSearch;
- $aSearch['iSearchRank'] += 1;
- if (!sizeof($aCurrentSearch['aName'])) $aSearch['iSearchRank'] += 1;
- if (preg_match('#^[0-9]+$#', $sToken)) $aSearch['iSearchRank'] += 2;
- if ($aWordFrequencyScores[$aSearchTerm['word_id']] < CONST_Max_Word_Frequency) {
- $aSearch['aName'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- } else {
- $aSearch['aNameNonSearch'][$aSearchTerm['word_id']] = $aSearchTerm['word_id'];
- }
- $aSearch['iNamePhrase'] = $iPhrase;
- if ($aSearch['iSearchRank'] < $this->iMaxRank) $aNewWordsetSearches[] = $aSearch;
+ $aNewSearches = $oCurrentSearch->extendWithPartialTerm(
+ $aSearchTerm,
+ $bStructuredPhrases,
+ $iPhrase,
+ $aWordFrequencyScores,
+ isset($aValidTokens[' '.$sToken]) ? $aValidTokens[' '.$sToken] : array()
+ );
+
+ foreach ($aNewSearches as $oSearch) {
+ if ($oSearch->getRank() < $this->iMaxRank) {
+ $aNewWordsetSearches[] = $oSearch;
}
}
+
}
- } else {
- // Allow skipping a word - but at EXTREAM cost
- //$aSearch = $aCurrentSearch;
- //$aSearch['iSearchRank']+=100;
- //$aNewWordsetSearches[] = $aSearch;
}
}
// Sort and cut
@@ -898,9 +811,12 @@ class Geocode
// Re-group the searches by their score, junk anything over 20 as just not worth trying
$aGroupedSearches = array();
foreach ($aNewPhraseSearches as $aSearch) {
- if ($aSearch['iSearchRank'] < $this->iMaxRank) {
- if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
- $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
+ $iRank = $aSearch->getRank();
+ if ($iRank < $this->iMaxRank) {
+ if (!isset($aGroupedSearches[$iRank])) {
+ $aGroupedSearches[$iRank] = array();
+ }
+ $aGroupedSearches[$iRank][] = $aSearch;
}
}
ksort($aGroupedSearches);
@@ -915,6 +831,22 @@ class Geocode
//if (CONST_Debug) _debugDumpGroupedSearches($aGroupedSearches, $aValidTokens);
}
+
+ // Revisit searches, drop bad searches and give penalty to unlikely combinations.
+ $aGroupedSearches = array();
+ foreach ($aSearches as $oSearch) {
+ if (!$oSearch->isValidSearch()) {
+ continue;
+ }
+
+ $iRank = $oSearch->addToRank($iGlobalRank);
+ if (!isset($aGroupedSearches[$iRank]) {
+ $aGroupedSearches[$iRank] = array();
+ }
+ $aGroupedSearches[$iRank][] = $oSearch;
+ }
+ ksort($aGroupedSearches);
+
return $aGroupedSearches;
}
@@ -955,7 +887,10 @@ class Geocode
if (!$this->sQuery && !$this->aStructuredQuery) return array();
$sNormQuery = $this->normTerm($this->sQuery);
- $sLanguagePrefArraySQL = "ARRAY[".join(',', array_map("getDBQuoted", $this->aLangPrefOrder))."]";
+ $sLanguagePrefArraySQL = getArraySQL(
+ array_map("getDBQuoted",
+ $this->aLangPrefOrder)
+ );
$sCountryCodesSQL = false;
if ($this->aCountryCodes) {
$sCountryCodesSQL = join(',', array_map('addQuotes', $this->aCountryCodes));
@@ -998,76 +933,59 @@ class Geocode
$aSearchResults = array();
if ($sQuery || $this->aStructuredQuery) {
- // Start with a blank search
- $aSearches = array(
- array(
- 'iSearchRank' => 0,
- 'iNamePhrase' => -1,
- 'sCountryCode' => false,
- 'aName' => array(),
- 'aAddress' => array(),
- 'aFullNameAddress' => array(),
- 'aNameNonSearch' => array(),
- 'aAddressNonSearch' => array(),
- 'sOperator' => '',
- 'aFeatureName' => array(),
- 'sClass' => '',
- 'sType' => '',
- 'sHouseNumber' => '',
- 'sPostcode' => '',
- 'oNear' => $oNearPoint
- )
- );
-
- // Any 'special' terms in the search?
- $bSpecialTerms = false;
- preg_match_all('/\\[([\\w_]*)=([\\w_]*)\\]/', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
- foreach ($aSpecialTermsRaw as $aSpecialTerm) {
- $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
- if (!$bSpecialTerms) {
- $aNewSearches = array();
- foreach ($aSearches as $aSearch) {
- $aNewSearch = $aSearch;
- $aNewSearch['sClass'] = $aSpecialTerm[1];
- $aNewSearch['sType'] = $aSpecialTerm[2];
- $aNewSearches[] = $aNewSearch;
- }
+ // Start with a single blank search
+ $aSearches = array(new SearchDescription());
- $aSearches = $aNewSearches;
- $bSpecialTerms = true;
- }
+ if ($oNearPoint) {
+ $aSearches[0]->setNear($oNearPoint);
}
- preg_match_all('/\\[([\\w ]*)\\]/u', $sQuery, $aSpecialTermsRaw, PREG_SET_ORDER);
- if (isset($this->aStructuredQuery['amenity']) && $this->aStructuredQuery['amenity']) {
- $aSpecialTermsRaw[] = array('['.$this->aStructuredQuery['amenity'].']', $this->aStructuredQuery['amenity']);
- unset($this->aStructuredQuery['amenity']);
+ if ($sQuery) {
+ $sQuery = $aSearches[0]->extractKeyValuePairs($sQuery);
}
- foreach ($aSpecialTermsRaw as $aSpecialTerm) {
- $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
- if ($bSpecialTerms) {
- continue;
+ $sSpecialTerm = '';
+ if ($sQuery) {
+ preg_match_all(
+ '/\\[([\\w ]*)\\]/u',
+ $sQuery,
+ $aSpecialTermsRaw,
+ PREG_SET_ORDER
+ );
+ foreach ($aSpecialTermsRaw as $aSpecialTerm) {
+ $sQuery = str_replace($aSpecialTerm[0], ' ', $sQuery);
+ if (!$sSpecialTerm) {
+ $sSpecialTerm = $aSpecialTerm[1];
+ }
}
+ }
+ if (!$sSpecialTerm && $this->aStructuredQuery
+ && isset($this->aStructuredQuery['amenity'])) {
+ $sSpecialTerm = $this->aStructuredQuery['amenity'];
+ unset($this->aStructuredQuery['amenity']);
+ }
- $sToken = chksql($this->oDB->getOne("SELECT make_standard_name('".$aSpecialTerm[1]."') AS string"));
- $sSQL = 'SELECT * ';
- $sSQL .= 'FROM ( ';
- $sSQL .= ' SELECT word_id, word_token, word, class, type, country_code, operator';
- $sSQL .= ' FROM word ';
+ if ($sSpecialTerm && !$aSearches[0]->hasOperator()) {
+ $sSpecialTerm = pg_escape_string($sSpecialTerm);
+ $sToken = chksql(
+ $this->oDB->getOne("SELECT make_standard_name('$sSpecialTerm')"),
+ "Cannot decode query. Wrong encoding?"
+ );
+ $sSQL = 'SELECT class, type FROM word ';
$sSQL .= ' WHERE word_token in (\' '.$sToken.'\')';
- $sSQL .= ') AS x ';
- $sSQL .= ' WHERE (class is not null AND class not in (\'place\'))';
+ $sSQL .= ' AND class is not null AND class not in (\'place\')';
if (CONST_Debug) var_Dump($sSQL);
$aSearchWords = chksql($this->oDB->getAll($sSQL));
$aNewSearches = array();
- foreach ($aSearches as $aSearch) {
+ foreach ($aSearches as $oSearch) {
foreach ($aSearchWords as $aSearchTerm) {
- $aNewSearch = $aSearch;
- $aNewSearch['sClass'] = $aSearchTerm['class'];
- $aNewSearch['sType'] = $aSearchTerm['type'];
- $aNewSearches[] = $aNewSearch;
- $bSpecialTerms = true;
+ $oNewSearch = clone $oSearch;
+ $oNewSearch->setPoiSearch(
+ Operator::TYPE,
+ $aSearchTerm['class'],
+ $aSearchTerm['type'],
+ );
+ $aNewSearches[] = $oNewSearch;
}
}
$aSearches = $aNewSearches;
@@ -1116,14 +1034,10 @@ class Geocode
if (CONST_Debug) var_Dump($sSQL);
$aValidTokens = array();
- if (sizeof($aTokens)) {
- $aDatabaseWords = chksql(
- $this->oDB->getAll($sSQL),
- "Could not get word tokens."
- );
- } else {
- $aDatabaseWords = array();
- }
+ $aDatabaseWords = chksql(
+ $this->oDB->getAll($sSQL),
+ "Could not get word tokens."
+ );
$aPossibleMainWordIDs = array();
$aWordFrequencyScores = array();
foreach ($aDatabaseWords as $aToken) {
@@ -1191,10 +1105,10 @@ class Geocode
foreach ($aGroupedSearches as $aSearches) {
foreach ($aSearches as $aSearch) {
- if ($aSearch['iSearchRank'] < $this->iMaxRank) {
- if (!isset($aReverseGroupedSearches[$aSearch['iSearchRank']])) $aReverseGroupedSearches[$aSearch['iSearchRank']] = array();
- $aReverseGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
+ if (!isset($aReverseGroupedSearches[$aSearch->getRank()])) {
+ $aReverseGroupedSearches[$aSearch->getRank()] = array();
}
+ $aReverseGroupedSearches[$aSearch->getRank()][] = $aSearch;
}
}
@@ -1205,38 +1119,9 @@ class Geocode
// Re-group the searches by their score, junk anything over 20 as just not worth trying
$aGroupedSearches = array();
foreach ($aSearches as $aSearch) {
- if ($aSearch['iSearchRank'] < $this->iMaxRank) {
- if (!isset($aGroupedSearches[$aSearch['iSearchRank']])) $aGroupedSearches[$aSearch['iSearchRank']] = array();
- $aGroupedSearches[$aSearch['iSearchRank']][] = $aSearch;
- }
- }
- ksort($aGroupedSearches);
- }
-
- if (CONST_Search_TryDroppedAddressTerms && sizeof($this->aStructuredQuery) > 0) {
- $aCopyGroupedSearches = $aGroupedSearches;
- foreach ($aCopyGroupedSearches as $iGroup => $aSearches) {
- foreach ($aSearches as $iSearch => $aSearch) {
- $aReductionsList = array($aSearch['aAddress']);
- $iSearchRank = $aSearch['iSearchRank'];
- while (sizeof($aReductionsList) > 0) {
- $iSearchRank += 5;
- if ($iSearchRank > iMaxRank) break 3;
- $aNewReductionsList = array();
- foreach ($aReductionsList as $aReductionsWordList) {
- for ($iReductionWord = 0; $iReductionWord < sizeof($aReductionsWordList); $iReductionWord++) {
- $aReductionsWordListResult = array_merge(array_slice($aReductionsWordList, 0, $iReductionWord), array_slice($aReductionsWordList, $iReductionWord+1));
- $aReverseSearch = $aSearch;
- $aSearch['aAddress'] = $aReductionsWordListResult;
- $aSearch['iSearchRank'] = $iSearchRank;
- $aGroupedSearches[$iSearchRank][] = $aReverseSearch;
- if (sizeof($aReductionsWordListResult) > 0) {
- $aNewReductionsList[] = $aReductionsWordListResult;
- }
- }
- }
- $aReductionsList = $aNewReductionsList;
- }
+ if ($aSearch->getRank() < $this->iMaxRank) {
+ if (!isset($aGroupedSearches[$aSearch->getRank()])) $aGroupedSearches[$aSearch->getRank()] = array();
+ $aGroupedSearches[$aSearch->getRank()][] = $aSearch;
}
}
ksort($aGroupedSearches);
@@ -1262,443 +1147,89 @@ class Geocode
$iQueryLoop = 0;
foreach ($aGroupedSearches as $iGroupedRank => $aSearches) {
$iGroupLoop++;
- foreach ($aSearches as $aSearch) {
+ foreach ($aSearches as $oSearch) {
$iQueryLoop++;
$searchedHousenumber = -1;
if (CONST_Debug) echo "