Skip to content

Commit

Permalink
Merge branch 'feature/spellcheck-bugfix' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
basdenooijer committed Feb 11, 2013
2 parents ea0232e + 3a1b345 commit 068ebe8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 25 deletions.
11 changes: 8 additions & 3 deletions examples/2.1.5.9-spellcheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

// add spellcheck settings
$spellcheck = $query->getSpellcheck();
$spellcheck->setQuery('delll ultrashar');
$spellcheck->setQuery('tes');
$spellcheck->setCount(10);
$spellcheck->setBuild(true);
$spellcheck->setCollate(true);
$spellcheck->setExtendedResults(true);
Expand All @@ -35,8 +36,12 @@
echo 'StartOffset: '.$suggestion->getStartOffset().'<br/>';
echo 'EndOffset: '.$suggestion->getEndOffset().'<br/>';
echo 'OriginalFrequency: '.$suggestion->getOriginalFrequency().'<br/>';
echo 'Frequency: '.$suggestion->getFrequency().'<br/>';
echo 'Word: '.$suggestion->getWord().'<br/>';
foreach ($suggestion->getWords() as $word) {
echo '-----<br/>';
echo 'Frequency: '.$word['freq'].'<br/>';
echo 'Word: '.$word['word'].'<br/>';
}

echo '<hr/>';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,19 @@ protected function parseSuggestion($key, $value)
$endOffset = (isset($value['endOffset'])) ? $value['endOffset'] : null;
$originalFrequency = (isset($value['origFreq'])) ? $value['origFreq'] : null;

if (is_string($value['suggestion'][0])) {
$word = $value['suggestion'][0];
$frequency = null;
} else {
$word = $value['suggestion'][0]['word'];
$frequency = $value['suggestion'][0]['freq'];
$words = array();
foreach($value['suggestion'] as $suggestion) {
if (is_string($suggestion)) {
$suggestion = array(
'word' => $suggestion,
'freq' => null,
);
}
$words[] = $suggestion;
}

return new Suggestion(
$numFound, $startOffset, $endOffset, $originalFrequency, $word, $frequency
$numFound, $startOffset, $endOffset, $originalFrequency, $words
);
}
}
36 changes: 27 additions & 9 deletions library/Solarium/QueryType/Select/Result/Spellcheck/Suggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,15 @@ class Suggestion
* @param int $startOffset
* @param int $endOffset
* @param int $originalFrequency
* @param string $word
* @param int $frequency
* @param array $words
*/
public function __construct($numFound, $startOffset, $endOffset, $originalFrequency, $word, $frequency)
public function __construct($numFound, $startOffset, $endOffset, $originalFrequency, $words)
{
$this->numFound = $numFound;
$this->startOffset = $startOffset;
$this->endOffset = $endOffset;
$this->originalFrequency = $originalFrequency;
$this->word = $word;
$this->frequency = $frequency;
$this->words = $words;
}

/**
Expand Down Expand Up @@ -107,13 +105,28 @@ public function getOriginalFrequency()
}

/**
* Get word
* Get first word
*
* @return string
* @return string|null
*/
public function getWord()
{
return $this->word;
$word = reset($this->words);
if (isset($word['word'])) {
return $word['word'];
} else {
return $word;
}
}

/**
* Get all words (and frequencies)
*
* @return array
*/
public function getWords()
{
return $this->words;
}

/**
Expand All @@ -125,7 +138,12 @@ public function getWord()
*/
public function getFrequency()
{
return $this->frequency;
$word = reset($this->words);
if (isset($word['freq'])) {
return $word['freq'];
} else {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public function testParseSingleCollation()
'suggestion' => array (
0 => array (
'word' => 'ultrasharp',
'freq' => 2
),
1 => array (
'word' => 'ultrasharpy',
'freq' => 1
),
),
Expand All @@ -209,6 +213,9 @@ public function testParseSingleCollation()
$result = $this->parser->parse($this->query, null, $data);
$collations = $result->getCollations();
$this->assertEquals('dell ultrasharp', $collations[0]->getQuery());

$words = $result->getSuggestion(1)->getWords();
$this->assertEquals(array('word' => 'ultrasharpy', 'freq' => 1), $words[1]);
}

public function testParseNoData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,27 @@ class SuggestionTest extends \PHPUnit_Framework_TestCase
*/
protected $result;

protected $numFound, $startOffset, $endOffset, $originalFrequency, $word, $frequency;
protected $numFound, $startOffset, $endOffset, $originalFrequency, $words, $frequency;

public function setUp()
{
$this->numFound = 1;
$this->startOffset = 2;
$this->endOffset = 3;
$this->originalFrequency = 4;
$this->word = 'dummyword';
$this->frequency = 5;
$this->words = array(
array(
'word' => 'dummyword',
'freq' => 5
),
array(
'word' => 'secondword',
'freq' => 1
)
);

$this->result = new Suggestion(
$this->numFound, $this->startOffset, $this->endOffset, $this->originalFrequency, $this->word, $this->frequency
$this->numFound, $this->startOffset, $this->endOffset, $this->originalFrequency, $this->words
);
}

Expand All @@ -78,12 +86,17 @@ public function testGetOriginalFrequency()

public function testGetWord()
{
$this->assertEquals($this->word, $this->result->getWord());
$this->assertEquals($this->words[0]['word'], $this->result->getWord());
}

public function testGetFrequency()
{
$this->assertEquals($this->frequency, $this->result->getFrequency());
$this->assertEquals($this->words[0]['freq'], $this->result->getFrequency());
}

public function testGetWords()
{
$this->assertEquals($this->words, $this->result->getWords());
}

}

0 comments on commit 068ebe8

Please sign in to comment.