SpellLib.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. if (!defined('ENCHANT_MYSPELL')) {
  3. define('ENCHANT_MYSPELL', 1);
  4. }
  5. if (!defined('ENCHANT_ISPELL')) {
  6. define('ENCHANT_ISPELL', 2);
  7. }
  8. /**
  9. * Wrapper for the PHP Extension Enchant which provides basic spellchecking
  10. * @author Mark Scherer
  11. * @licence MIT
  12. *
  13. * 2012-02-17 ms
  14. */
  15. class SpellLib {
  16. const ENGINE_MYSPELL = ENCHANT_MYSPELL; # default engine
  17. const ENGINE_ISPELL = ENCHANT_ISPELL;
  18. /**
  19. * available engines
  20. */
  21. protected $_engines = array(self::ENGINE_MYSPELL => 'myspell', self::ENGINE_ISPELL => 'ispell');
  22. /**
  23. * available languages
  24. */
  25. protected $l_langs = array('en_GB', 'de_DE');
  26. protected $_Broker;
  27. protected $_Dict;
  28. //public $settings = array();
  29. public function __construct($options = array()) {
  30. if (!function_exists('enchant_broker_init')) {
  31. throw new InternalErrorException(__('Module %s not installed', 'Enchant'));
  32. }
  33. $this->_Broker = enchant_broker_init();
  34. $defaults = array(
  35. 'path' => VENDORS.'dictionaries'.DS,
  36. 'lang' => 'en_GB',
  37. 'engine' => self::ENGINE_MYSPELL
  38. );
  39. $defaults = array_merge($defaults, (array)Configure::read('Spell'));
  40. $options = array_merge($defaults, $options);
  41. if (!isset($this->_engines[$options['engine']])) {
  42. throw new InternalErrorException(__('Engine %s not found', (String) $options['engine']));
  43. }
  44. $engineFolder = $this->_engines[$options['engine']];
  45. enchant_broker_set_dict_path($this->_Broker, $options['engine'], $options['path'] . $engineFolder . DS);
  46. if (!enchant_broker_dict_exists($this->_Broker, $options['lang'])) {
  47. throw new InternalErrorException(__('Dictionary %s not found', $options['lang']));
  48. }
  49. $this->_Dict = enchant_broker_request_dict($this->_Broker, $options['lang']);
  50. }
  51. public function listDictionaries() {
  52. return enchant_broker_list_dicts($this->_Broker);
  53. }
  54. public function listBrokers() {
  55. return enchant_broker_describe($this->_Broker);
  56. }
  57. /**
  58. * @return bool $success
  59. */
  60. public function check($word) {
  61. return enchant_dict_check($this->_Dict, $word);
  62. }
  63. /**
  64. * @return array $listOfSuggestions
  65. */
  66. public function suggestions($word) {
  67. return enchant_dict_suggest($this->_Dict, $word);
  68. }
  69. public function __destruct() {
  70. enchant_broker_free($this->_Broker);
  71. }
  72. }