Created
November 22, 2014 14:30
-
-
Save lifeofguenter/3189d9ddfe543f1e880f to your computer and use it in GitHub Desktop.
sample hunspell/enchant usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Weheartwebsites\Spelling; | |
use Exception; | |
class Bee | |
{ | |
protected $broker; | |
protected $dict; | |
public function __construct($lang = null) | |
{ | |
$this->broker = enchant_broker_init(); | |
if ($this->broker === false) { | |
throw new Exception('unable to load enchant'); | |
} | |
if (!enchant_broker_set_dict_path($this->broker, ENCHANT_MYSPELL, __DIR__ . '/Dicts')) { | |
throw new Exception('unable to load dictionaries'); | |
} | |
if ($lang !== null) { | |
$this->setLang($lang); | |
} | |
} | |
public function __destruct() | |
{ | |
if (is_resource($this->dict)) { | |
enchant_broker_free_dict($this->dict); | |
} | |
if (is_resource($this->broker)) { | |
enchant_broker_free($this->broker); | |
} | |
} | |
public function setLang($lang) | |
{ | |
if (!enchant_broker_dict_exists($this->broker, $lang)) { | |
throw new Exception(sprintf('lang (%s) does not exist', $lang)); | |
} | |
$this->dict = enchant_broker_request_dict($this->broker, $lang); | |
} | |
public function check($word) | |
{ | |
return enchant_dict_check($this->dict, $word); | |
} | |
public function suggest($word) | |
{ | |
return enchant_dict_suggest($this->dict, $word); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment