X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/023f94b066c11628ecc87ca9876dbe5ec4136776..9a13086122199ba3785d9f329c2c428be41bb13f:/lib/Phrase.php diff --git a/lib/Phrase.php b/lib/Phrase.php index 23b9e3ca..7cf3f297 100644 --- a/lib/Phrase.php +++ b/lib/Phrase.php @@ -9,7 +9,7 @@ namespace Nominatim; */ class Phrase { - CONST MAX_DEPTH = 7; + const MAX_DEPTH = 7; // Complete phrase as a string. private $sPhrase; @@ -29,16 +29,35 @@ class Phrase $this->aWordSets = $this->createWordSets($this->aWords, 0); } + /** + * Return the element type of the phrase. + * + * @return string Pharse type if the phrase comes from a structured query + * or empty string otherwise. + */ public function getPhraseType() { return $this->sPhraseType; } + /** + * Return the array of possible segmentations of the phrase. + * + * @return string[][] Array of segmentations, each consisting of an + * array of terms. + */ public function getWordSets() { return $this->aWordSets; } + /** + * Add the tokens from this phrase to the given list of tokens. + * + * @param string[] $aTokens List of tokens to append. + * + * @return void + */ public function addTokens(&$aTokens) { foreach ($this->aWordSets as $aSet) { @@ -49,6 +68,11 @@ class Phrase } } + /** + * Invert the set of possible segmentations. + * + * @return void + */ public function invertWordSets() { $this->aWordSets = $this->createInverseWordSets($this->aWords, 0); @@ -59,7 +83,7 @@ class Phrase $aResult = array(array(join(' ', $aWords))); $sFirstToken = ''; if ($iDepth < Phrase::MAX_DEPTH) { - while (sizeof($aWords) > 1) { + while (count($aWords) > 1) { $sWord = array_shift($aWords); $sFirstToken .= ($sFirstToken?' ':'').$sWord; $aRest = $this->createWordSets($aWords, $iDepth + 1); @@ -72,12 +96,12 @@ class Phrase return $aResult; } - public function createInverseWordSets($aWords, $iDepth) + private function createInverseWordSets($aWords, $iDepth) { $aResult = array(array(join(' ', $aWords))); $sFirstToken = ''; if ($iDepth < Phrase::MAX_DEPTH) { - while (sizeof($aWords) > 1) { + while (count($aWords) > 1) { $sWord = array_pop($aWords); $sFirstToken = $sWord.($sFirstToken?' ':'').$sFirstToken; $aRest = $this->createInverseWordSets($aWords, $iDepth + 1); @@ -89,4 +113,14 @@ class Phrase return $aResult; } -}; + + public function debugInfo() + { + return array( + 'Type' => $this->sPhraseType, + 'Phrase' => $this->sPhrase, + 'Words' => $this->aWords, + 'WordSets' => $this->aWordSets + ); + } +}