Kdo programoval v PHP ví, že většina rozšíření používá
procedurální API a jenom některá z rozšíření nabízejí pěkný
objektový přístup. Bohužel funkce kontroly pravopisu pspell nejsou jedním
z nich. Nabízí se několik možností, jak používat pspell
v objektově orientované aplikaci: používat procedury, což není ono;
napsat si vlastní objektovou obálku, nebo použít tuto.
Použití je intuitivní:
//vytvoříme instanci SpellCheckeru
//konstruktor má parametry odpovídající parametrům funkce pspell_config_create
$spellCheker = new SpellChecker('cz');
//nebudeme kontrolovat slova kratší než 3 znaky
$spellCheker->ignoreWordShorterThan(3);
//zkontroluje jedno slovo, vrátí TRUE/FALSE podle toho, jestli je napsané správně
$ok = $spellCheker->check('nejneobdělávavatelnější');
//hromadná kontrola slov
//parametrem funkce checkWords je pole slov
//vrací pole stejně velké pole boolean hodnot, které udávají, jestli bylo slovo
//s daným klíčem ve vstupním poli správné nebo ne
$ok = $spellCheker->checkWords(array('hele', 'vole', 'kde', 'mám', 'káru'));
//navrhne 4 varianty slova (navrhuje i v případě, že je slovo napsáno správně)
$spellCheker->suggest('tryčko', 4);
//navrhne 4 varianty pro všechna špatně zapsaná slova v poli $mnohoSlov
$spellCheker->suggestWords($mnohoSlov, 4);
A tady už je kód:
class SpellChecker extends Object {
private $link = NULL;
private $changed = FALSE;
private $lang;
private $spelling = NULL;
private $jargon = NULL;
private $encoding = 'utf-8';
private $mode = 0;
private $ignoreWordShorterThan = NULL;
public function __construct( $lang, $spelling = NULL, $jargon = NULL, $encoding = 'utf-8', $mode = 0 )
{
$this->lang = $lang;
$this->spelling = $spelling;
$this->jargon = $jargon;
$this->encoding = $encoding;
$this->mode = $mode;
}
public function setSpelling( $spelling )
{
$this->spelling = $spelling;
$this->changed = TRUE;
}
public function setJargon( $jargon )
{
$this->jargon = $jargon;
$this->changed = TRUE;
}
public function setEncoding( $encoding )
{
$this->encoding = $encoding;
$this->changed = TRUE;
}
public function setMode( $mode )
{
$this->mode = $mode;
$this->changed = TRUE;
}
public function ignoreWordShorterThan( $characters )
{
$this->ignoreWordShorterThan = $characters;
$this->changed = TRUE;
}
private function getLink()
{
if( $this->link === NULL || $this->changed === TRUE ){
$this->link = $this->buildLink();
}
return $this->link;
}
private function buildLink()
{
$config = pspell_config_create( $this->lang, $this->spelling, $this->jargon, $this->encoding );
if( $this->ignoreWordShorterThan !== NULL ) {
pspell_config_ignore( $config, $this->ignoreWordShorterThan );
}
pspell_config_mode( $config, $this->mode );
return pspell_new_config( $config );
}
public function check($word)
{
return pspell_check( $this->getLink(), $word );
}
public function checkWords(array $words)
{
$checked = array();
foreach ( $words as $key => $word )
{
$checked[ $key ] = $this->check( trim($word) );
}
return $checked;
}
public function checkText($text)
{
throw new NotImplementedException();
}
public function suggest($word, $maxSuggestions = NULL)
{
$suggestions = pspell_suggest( $this->getLink(), $word );
return array_slice( $suggestions, 0, (int)$maxSuggestions );
}
public function suggestWords(array $words, $maxSuggestions = NULL)
{
$checked = $this->checkWords( $words );
$suggestions = array();
foreach( $words as $key => $word )
{
$suggestions[$key] = ( $checked[$key] !== TRUE ? $this->suggest($word, $maxSuggestions) : NULL );
}
return $suggestions;
}
}


Jmenuji se K., v síti také známý jako
komentáře