Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
sample hunspell/enchant usage
<?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