6 * Segment of a query string.
8 * The parts of a query strings are usually separated by commas.
14 // Complete phrase as a string.
16 // Element type for structured searches.
18 // Space-separated words of the phrase.
20 // Possible segmentations of the phrase.
24 public function __construct($sPhrase, $sPhraseType)
26 $this->sPhrase = trim($sPhrase);
27 $this->sPhraseType = $sPhraseType;
28 $this->aWords = explode(' ', $this->sPhrase);
29 $this->aWordSets = $this->createWordSets($this->aWords, 0);
32 public function getPhraseType()
34 return $this->sPhraseType;
37 public function getWordSets()
39 return $this->aWordSets;
42 public function addTokens(&$aTokens)
44 foreach ($this->aWordSets as $aSet) {
45 foreach ($aSet as $sWord) {
46 $aTokens[' '.$sWord] = ' '.$sWord;
47 $aTokens[$sWord] = $sWord;
52 public function invertWordSets()
54 $this->aWordSets = $this->createInverseWordSets($this->aWords, 0);
57 private function createWordSets($aWords, $iDepth)
59 $aResult = array(array(join(' ', $aWords)));
61 if ($iDepth < Phrase::MAX_DEPTH) {
62 while (sizeof($aWords) > 1) {
63 $sWord = array_shift($aWords);
64 $sFirstToken .= ($sFirstToken?' ':'').$sWord;
65 $aRest = $this->createWordSets($aWords, $iDepth + 1);
66 foreach ($aRest as $aSet) {
67 $aResult[] = array_merge(array($sFirstToken), $aSet);
75 public function createInverseWordSets($aWords, $iDepth)
77 $aResult = array(array(join(' ', $aWords)));
79 if ($iDepth < Phrase::MAX_DEPTH) {
80 while (sizeof($aWords) > 1) {
81 $sWord = array_pop($aWords);
82 $sFirstToken = $sWord.($sFirstToken?' ':'').$sFirstToken;
83 $aRest = $this->createInverseWordSets($aWords, $iDepth + 1);
84 foreach ($aRest as $aSet) {
85 $aResult[] = array_merge(array($sFirstToken), $aSet);