k47.cz
mastodon twitter RSS
bandcamp explorer 0xDEADBEEF

PHP Objektová obálka pspell

— k47 (CC by-nc-sa)

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);

Tady 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;


	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;
	}

	function setSpelling($spelling) {
		$this->spelling = $spelling;
		$this->changed = true;
	}

	function setJargon($jargon) {
		$this->jargon = $jargon;
		$this->changed = true;
	}

	function setEncoding($encoding) {
		$this->encoding = $encoding;
		$this->changed = true;
	}

	function setMode($mode) {
		$this->mode = $mode;
		$this->changed = true;
	}

	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);
	}


	function check($word) {
		return pspell_check($this->getLink(), $word);
	}

	function checkWords(array $words) {
		$checked = array();
		foreach ( $words as $key => $word ) {
			$checked[ $key ] = $this->check(trim($word));
		}
		return $checked;
	}

	function checkText($text) {
		throw new NotImplementedException();
	}

	function suggest($word, $maxSuggestions = null) {
		$suggestions = pspell_suggest($this->getLink(), $word);
		return array_slice($suggestions, 0, (int)$maxSuggestions);
	}

	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;
	}
}
píše k47, ascii@k47.cz