Example #1
0
 /**
  * Spellchecks an array of words.
  *
  * @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1
  * @param Array $words Array of words to check.
  * @return Name/value object with arrays of suggestions.
  */
 public function getSuggestions($lang, $words)
 {
     $suggestions = array();
     $enchant = enchant_broker_init();
     $config = $this->getConfig();
     if (isset($config["enchant_dicts_path"])) {
         enchant_broker_set_dict_path($enchant, ENCHANT_MYSPELL, $config["enchant_dicts_path"]);
         enchant_broker_set_dict_path($enchant, ENCHANT_ISPELL, $config["enchant_dicts_path"]);
     }
     if (!enchant_broker_describe($enchant)) {
         throw new Exception("Enchant spellchecker not find any backends.");
     }
     $lang = $this->normalizeLangCode($enchant, $lang);
     if (enchant_broker_dict_exists($enchant, $lang)) {
         $dict = enchant_broker_request_dict($enchant, $lang);
         foreach ($words as $word) {
             if (!enchant_dict_check($dict, $word)) {
                 $suggs = enchant_dict_suggest($dict, $word);
                 if (!is_array($suggs)) {
                     $suggs = array();
                 }
                 $suggestions[$word] = $suggs;
             }
         }
         enchant_broker_free_dict($dict);
         enchant_broker_free($enchant);
     } else {
         enchant_broker_free($enchant);
         throw new Exception("Enchant spellchecker could not find dictionary for language: " . $lang);
     }
     return $suggestions;
 }
 function __construct($langCode = null)
 {
     wfProfileIn(__METHOD__);
     parent::__construct();
     // create a new broker object
     $this->broker = enchant_broker_init();
     // set path to directory with extra dictionaries
     // @see http://blog.iwanluijks.nl/2010/10/using-enchant-with-php-on-windows-part.html
     $ip = $this->app->getGlobal('IP');
     $this->path = "{$ip}/lib/dicts";
     enchant_broker_set_dict_path($this->broker, ENCHANT_MYSPELL, $this->path);
     enchant_broker_set_dict_path($this->broker, ENCHANT_ISPELL, $this->path);
     // try to load dictionary
     if (!empty($langCode)) {
         $this->load($langCode);
     }
     wfProfileOut(__METHOD__);
 }
Example #3
0
 public function __construct($options = array())
 {
     if (!function_exists('enchant_broker_init')) {
         throw new InternalErrorException(__('Module %s not installed', 'Enchant'));
     }
     $this->_Broker = enchant_broker_init();
     $defaults = array('path' => VENDORS . 'dictionaries' . DS, 'lang' => 'en_GB', 'engine' => self::ENGINE_MYSPELL);
     $defaults = am($defaults, (array) Configure::read('Spell'));
     $options = array_merge($defaults, $options);
     if (!isset($this->_engines[$options['engine']])) {
         throw new InternalErrorException(__('Engine %s not found', (string) $options['engine']));
     }
     $engineFolder = $this->_engines[$options['engine']];
     enchant_broker_set_dict_path($this->_Broker, $options['engine'], $options['path'] . $engineFolder . DS);
     if (!enchant_broker_dict_exists($this->_Broker, $options['lang'])) {
         throw new InternalErrorException(__('Dictionary %s not found', $options['lang']));
     }
     $this->_Dict = enchant_broker_request_dict($this->_Broker, $options['lang']);
 }
	/**
	 * Wiki-specific search suggestions using enchant library.
	 * Use SphinxSearch_setup.php to create the dictionary
	 */
	function suggestWithEnchant() {
		if (!function_exists('enchant_broker_init')) {
			return;
		}
		$broker = enchant_broker_init();
		enchant_broker_set_dict_path($broker, ENCHANT_MYSPELL, dirname( __FILE__ ));
		if ( enchant_broker_dict_exists( $broker, 'sphinx' ) ) {
			$dict = enchant_broker_request_dict( $broker, 'sphinx' );
			$suggestion_found = false;
			$full_suggestion = '';
			foreach ( $this->mTerms as $word ) {
				if ( !enchant_dict_check($dict, $word) ) {
					$suggestions = enchant_dict_suggest($dict, $word);
					while ( count( $suggestions ) ) {
						$candidate = array_shift( $suggestions );
						if ( strtolower($candidate) != strtolower($word) ) {
							$word = $candidate;
							$suggestion_found = true;
							break;
						}
					}
				}
				$full_suggestion .= $word . ' ';
			}
			enchant_broker_free_dict( $dict );
			if ($suggestion_found) {
				$this->mSuggestion = trim( $full_suggestion );
			}
		}
		enchant_broker_free( $broker );
	}