SpellLib.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. */
  14. class SpellLib {
  15. const ENGINE_MYSPELL = ENCHANT_MYSPELL; # default engine
  16. const ENGINE_ISPELL = ENCHANT_ISPELL;
  17. /**
  18. * Available engines
  19. */
  20. protected $_engines = array(self::ENGINE_MYSPELL => 'myspell', self::ENGINE_ISPELL => 'ispell');
  21. /**
  22. * Available languages
  23. */
  24. protected $_langs = array('en_GB', 'de_DE');
  25. protected $_Broker;
  26. protected $_Dict;
  27. //public $settings = array();
  28. public function __construct($options = array()) {
  29. if (!function_exists('enchant_broker_init')) {
  30. throw new InternalErrorException(__('Module %s not installed', 'Enchant'));
  31. }
  32. $this->_Broker = enchant_broker_init();
  33. $defaults = array(
  34. 'path' => VENDORS . 'dictionaries' . DS,
  35. 'lang' => 'en_GB',
  36. 'engine' => self::ENGINE_MYSPELL
  37. );
  38. $defaults = array_merge($defaults, (array)Configure::read('Spell'));
  39. $options = array_merge($defaults, $options);
  40. if (!isset($this->_engines[$options['engine']])) {
  41. throw new InternalErrorException(__('Engine %s not found', (string) $options['engine']));
  42. }
  43. $engineFolder = $this->_engines[$options['engine']];
  44. enchant_broker_set_dict_path($this->_Broker, $options['engine'], $options['path'] . $engineFolder . DS);
  45. if (!enchant_broker_dict_exists($this->_Broker, $options['lang'])) {
  46. throw new InternalErrorException(__('Dictionary %s not found', $options['lang']));
  47. }
  48. $this->_Dict = enchant_broker_request_dict($this->_Broker, $options['lang']);
  49. }
  50. public function listDictionaries() {
  51. return enchant_broker_list_dicts($this->_Broker);
  52. }
  53. public function listBrokers() {
  54. return enchant_broker_describe($this->_Broker);
  55. }
  56. /**
  57. * @return boolean Success
  58. */
  59. public function check($word) {
  60. return enchant_dict_check($this->_Dict, $word);
  61. }
  62. /**
  63. * @return array listOfSuggestions
  64. */
  65. public function suggestions($word) {
  66. return enchant_dict_suggest($this->_Dict, $word);
  67. }
  68. public function __destruct() {
  69. enchant_broker_free($this->_Broker);
  70. }
  71. }