Commit 08d86ca8 authored by Bas de Nooijer's avatar Bas de Nooijer

Improved spellcheck result models and added a spellcheck example script

parent f8899d2b
<?php
require('init.php');
htmlHeader();
// create a client instance
$client = new Solarium_Client($config);
// get a select query instance
$query = $client->createSelect();
$query->setRows(0);
// add distributed search settings
// see http://wiki.apache.org/solr/DistributedSearch#Distributed_Search_Example for setting up two solr instances
$spellcheck = $query->getSpellcheck();
$spellcheck->setQuery('delll ultrashar');
$spellcheck->setBuild(true);
$spellcheck->setCollate(true);
$spellcheck->setExtendedResults(true);
$spellcheck->setCollateExtendedResults(true);
// this executes the query and returns the result
$resultset = $client->select($query);
$spellcheckResult = $resultset->getSpellcheck();
echo '<h1>Correctly spelled?</h1>';
if ($spellcheckResult->getCorrectlySpelled()) {
echo 'yes';
}else{
echo 'no';
}
echo '<h1>Suggestions</h1>';
foreach($spellcheckResult as $suggestion) {
echo 'NumFound: '.$suggestion->getNumFound().'<br/>';
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/>';
echo '<hr/>';
}
$collation = $spellcheckResult->getCollation();
echo '<h1>Collation</h1>';
echo 'Query: '.$collation->getQuery().'<br/>';
echo 'Hits: '.$collation->getHits().'<br/>';
echo 'Corrections:<br/>';
foreach($collation->getCorrections() as $input => $correction) {
echo $input . ' => ' . $correction .'<br/>';
}
htmlFooter();
\ No newline at end of file
...@@ -50,9 +50,10 @@ ...@@ -50,9 +50,10 @@
</ul> </ul>
<li><a href="2.1.5.4-dismax.php">2.1.5.4 Dismax</a></li> <li><a href="2.1.5.4-dismax.php">2.1.5.4 Dismax</a></li>
<li><a href="2.1.5.5-edismax.php">2.1.5.5 Edismax</a></li> <li><a href="2.1.5.5-edismax.php">2.1.5.5 Edismax</a></li>
<li><a href="2.1.5.6-grouping-by-field.php">2.1.5.5 Grouping by field</a></li> <li><a href="2.1.5.6-grouping-by-field.php">2.1.5.6 Grouping by field</a></li>
<li><a href="2.1.5.7-grouping-by-query.php">2.1.5.6 Grouping by query</a></li> <li><a href="2.1.5.7-grouping-by-query.php">2.1.5.7 Grouping by query</a></li>
<li><a href="2.1.5.8-distributed-search.php">2.1.5.6 Distributed search (sharding)</a></li> <li><a href="2.1.5.8-distributed-search.php">2.1.5.8 Distributed search (sharding)</a></li>
<li><a href="2.1.5.9-spellcheck.php">2.1.5.9 Spellcheck</a></li>
</ul> </ul>
<li><a href="2.1.6-helper-functions.php">2.1.6 Helper functions</a></li> <li><a href="2.1.6-helper-functions.php">2.1.6 Helper functions</a></li>
<li><a href="2.1.7-query-reuse.php">2.1.7 Query re-use</a></li> <li><a href="2.1.7-query-reuse.php">2.1.7 Query re-use</a></li>
......
...@@ -51,21 +51,109 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck ...@@ -51,21 +51,109 @@ class Solarium_Client_ResponseParser_Select_Component_Spellcheck
* @param Solarium_Query_Select $query * @param Solarium_Query_Select $query
* @param Solarium_Query_Select_Component_Spellcheck $spellcheck * @param Solarium_Query_Select_Component_Spellcheck $spellcheck
* @param array $data * @param array $data
* @return Solarium_Result_Select_Spellcheck * @return Solarium_Result_Select_Spellcheck|null
*/ */
public function parse($query, $spellcheck, $data) public function parse($query, $spellcheck, $data)
{ {
$results = array(); $results = array();
if (isset($data['spellcheck'])) { if (isset($data['spellcheck']['suggestions']) && is_array($data['spellcheck']['suggestions']) && count($data['spellcheck']['suggestions']) > 0) {
$spellcheckResults = $data['spellcheck']; $spellcheckResults = $data['spellcheck']['suggestions'];
foreach ($spellcheckResults AS $key => $result) {
$results[$key] = new Solarium_Result_Select_Spellcheck_Result( $suggestions = array();
$result $correctlySpelled = null;
); $collation = null;
$index = 0;
while (isset($spellcheckResults[$index]) && isset($spellcheckResults[$index+1])) {
$key = $spellcheckResults[$index];
$value = $spellcheckResults[$index+1];
switch ($key) {
case 'correctlySpelled':
$correctlySpelled = $value;
break;
case 'collation':
$collation = $this->_parseCollation($value);
break;
default:
$suggestions[] = $this->_parseSuggestion($key, $value);
}
$index +=2;
}
return new Solarium_Result_Select_Spellcheck($suggestions, $collation, $correctlySpelled);
} else {
return null;
}
}
protected function _parseCollation($values)
{
if (is_string($values)) {
return new Solarium_Result_Select_Spellcheck_Collation($values, null, array());
} else {
$query = null;
$hits = null;
$correctionResult = null;
$index = 0;
while (isset($values[$index]) && isset($values[$index+1])) {
$key = $values[$index];
$value = $values[$index+1];
switch ($key) {
case 'collationQuery':
$query = $value;
break;
case 'hits':
$hits = $value;
break;
case 'misspellingsAndCorrections':
$correctionResult = $value;
break;
}
$index +=2;
}
$corrections = array();
if ($correctionResult !== null) {
$index = 0;
while (isset($correctionResult[$index]) && isset($correctionResult[$index+1])) {
$input = $correctionResult[$index];
$correction = $correctionResult[$index+1];
$corrections[$input] = $correction;
$index += 2;
} }
} }
return new Solarium_Result_Select_Spellcheck($results); return new Solarium_Result_Select_Spellcheck_Collation($query, $hits, $corrections);
}
}
protected function _parseSuggestion($key, $value)
{
$numFound = (isset($value['numFound'])) ? $value['numFound'] : null;
$startOffset = (isset($value['startOffset'])) ? $value['startOffset'] : null;
$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'];
}
return new Solarium_Result_Select_Spellcheck_Suggestion(
$numFound, $startOffset, $endOffset, $originalFrequency, $word, $frequency
);
} }
} }
\ No newline at end of file
...@@ -46,46 +46,84 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable ...@@ -46,46 +46,84 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
{ {
/** /**
* Result array * Suggestions array
* *
* @var array * @var array
*/ */
protected $_results; protected $_suggestions;
/**
* Collation object
*
* @var Solarium_Result_Select_Spellcheck_Collation
*/
protected $_collation;
/**
* @var boolean
*/
protected $_correctlySpelled;
/** /**
* Constructor * Constructor
* *
* @param array $results * @param array $suggestions
* @param Solarium_Result_Select_Spellcheck_Collation $collation
* @param boolean $correctlySpelled
* @return void * @return void
*/ */
public function __construct($results) public function __construct($suggestions, $collation, $correctlySpelled)
{
$this->_suggestions = $suggestions;
$this->_collation = $collation;
$this->_correctlySpelled = $correctlySpelled;
}
/**
* Get the collation result
*
* @return Solarium_Result_Select_Spellcheck_Collation
*/
public function getCollation()
{
return $this->_collation;
}
/**
* Get correctly spelled status
*
* Only available if ExtendedResults was enabled in your query
*
* @return bool
*/
public function getCorrectlySpelled()
{ {
$this->_results = $results; return $this->_correctlySpelled;
} }
/** /**
* Get a result by key * Get a result by key
* *
* @param mixed $key * @param mixed $key
* @return Solarium_Result_Select_Highlighting_Result|null * @return Solarium_Result_Select_Highlighting_Suggestion|null
*/ */
public function getResult($key) public function getSuggestion($key)
{ {
if (isset($this->_results[$key])) { if (isset($this->_suggestions[$key])) {
return $this->_results[$key]; return $this->_suggestions[$key];
} else { } else {
return null; return null;
} }
} }
/** /**
* Get all results * Get all suggestions
* *
* @return array * @return array
*/ */
public function getResults() public function getSuggestions()
{ {
return $this->_results; return $this->_suggestions;
} }
/** /**
...@@ -95,7 +133,7 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable ...@@ -95,7 +133,7 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
*/ */
public function getIterator() public function getIterator()
{ {
return new ArrayIterator($this->_results); return new ArrayIterator($this->_suggestions);
} }
/** /**
...@@ -105,6 +143,6 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable ...@@ -105,6 +143,6 @@ class Solarium_Result_Select_Spellcheck implements IteratorAggregate, Countable
*/ */
public function count() public function count()
{ {
return count($this->_results); return count($this->_suggestions);
} }
} }
\ No newline at end of file
...@@ -37,73 +37,98 @@ ...@@ -37,73 +37,98 @@
*/ */
/** /**
* Select component spellcheck result item * Select component spellcheck collation result
* *
* @package Solarium * @package Solarium
* @subpackage Result * @subpackage Result
*/ */
class Solarium_Result_Select_Spellcheck_Result implements IteratorAggregate, Countable class Solarium_Result_Select_Spellcheck_Collation implements IteratorAggregate, Countable
{ {
/** /**
* Fields array * @var string
* */
protected $_query;
/**
* @var int
*/
protected $_hits;
/**
* @var array * @var array
*/ */
protected $_fields; protected $_corrections;
/** /**
* Constructor * Constructor
* *
* @param array $fields * @param string $query
* @return void * @param int|null $hits
* @param array $corrections
*/ */
public function __construct($fields) public function __construct($query, $hits, $corrections)
{ {
$this->_fields = $fields; $this->_query = $query;
$this->_hits = $hits;
$this->_corrections = $corrections;
} }
/** /**
* Get highlights for all fields * Get query string
* *
* @return array * @return string
*/ */
public function getFields() public function getQuery()
{ {
return $this->_fields; return $this->_query;
} }
/** /**
* Get highlights for a single field * Get hit count
* *
* @return array * Only available if ExtendedResults was enabled in your query
*
* @return int|null
*/ */
public function getField($key) public function getHits()
{ {
if (isset($this->_fields[$key])) { return $this->_hits;
return $this->_fields[$key];
} else {
return array();
} }
/**
* Get all corrrections
*
* Only available if ExtendedResults was enabled in your query
*
* @return array
*/
public function getCorrections()
{
return $this->_corrections;
} }
/** /**
* IteratorAggregate implementation * IteratorAggregate implementation
* *
* Only available if ExtendedResults was enabled in your query
*
* @return ArrayIterator * @return ArrayIterator
*/ */
public function getIterator() public function getIterator()
{ {
return new ArrayIterator($this->_fields); return new ArrayIterator($this->_corrections);
} }
/** /**
* Countable implementation * Countable implementation
* *
* Only available if ExtendedResults was enabled in your query
*
* @return int * @return int
*/ */
public function count() public function count()
{ {
return count($this->_fields); return count($this->_corrections);
} }
} }
\ No newline at end of file
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Result
*/
/**
* Select component spellcheck suggestion result
*
* @package Solarium
* @subpackage Result
*/
class Solarium_Result_Select_Spellcheck_Suggestion
{
/**
* Constructor
*
* @param int $numFound
* @param int $startOffset
* @param int $endOffset
* @param int $originalFrequency
* @param string $word
* @param int $frequency
*/
public function __construct($numFound, $startOffset, $endOffset, $originalFrequency, $word, $frequency)
{
$this->_numFound = $numFound;
$this->_startOffset = $startOffset;
$this->_endOffset = $endOffset;
$this->_originalFrequency = $originalFrequency;
$this->_word = $word;
$this->_frequency = $frequency;
}
/**
* Get numFound value
*
* @return int
*/
public function getNumFound()
{
return $this->_numFound;
}
/**
* Get startOffset value
*
* @return int
*/
public function getStartOffset()
{
return $this->_startOffset;
}
/**
* Get endOffset value
*
* @return int
*/
public function getEndOffset()
{
return $this->_endOffset;
}
/**
* Get originalFrequency value
*
* Only available if CollateExtendedResults was enabled in your query
*
* @return int
*/
public function getOriginalFrequency()
{
return $this->_originalFrequency;
}
/**
* Get word
*
* @return string
*/
public function getWord()
{
return $this->_word;
}
/**
* Get frequency value
*
* Only available if CollateExtendedResults was enabled in your query
*
* @return int
*/
public function getFrequency()
{
return $this->_frequency;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment