Skip to content

Instantly share code, notes, and snippets.

@lifeofguenter
Created November 22, 2014 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifeofguenter/3189d9ddfe543f1e880f to your computer and use it in GitHub Desktop.
Save lifeofguenter/3189d9ddfe543f1e880f to your computer and use it in GitHub Desktop.
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