/**
  * Creates a new Pspell spell checker
  *
  * @param string $language the language used by this spell checker. This
  *                          should be a two-letter ISO 639 language code
  *                          followed by an optional two digit ISO 3166
  *                          country code separated by a dash or underscore.
  *                          For example, 'en', 'en-CA' and 'en_CA' are
  *                          valid languages.
  * @param string $personal_wordlist optional. The filename of the personal
  *                                   wordlist for this spell checker. If not
  *                                   specified, no personal wordlist is
  *                                   used. The personal wordlist may contain
  *                                   spellings for words that are correct
  *                                   but are not in the regular dictionary.
  *
  * @throws NateGoSearchException if the Pspell extension is not available.
  * @throws NateGoSearchtException if a dictionary in the specified language
  *                                could not be loaded.
  */
 public function __construct($language, $path_to_data = '', $repl_pairs = '', $personal_wordlist = '')
 {
     if (!extension_loaded('pspell')) {
         throw new NateGoSearchException('The Pspell PHP extension is ' . 'required for NateGoSearchPSpellSpellChecker.');
     }
     $config = pspell_config_create($language, '', '', 'utf-8');
     pspell_config_mode($config, PSPELL_FAST);
     if ($path_to_data != '') {
         pspell_config_data_dir($config, $path_to_data);
         pspell_config_dict_dir($config, $path_to_data);
     }
     if ($repl_pairs != '') {
         pspell_config_repl($config, $repl_pairs);
     }
     if ($personal_wordlist != '') {
         pspell_config_personal($config, $personal_wordlist);
         if (file_exists($personal_wordlist) && fileowner($personal_wordlist) == posix_getuid()) {
             // update permissions (-rw-rw----)
             chmod($personal_wordlist, 0666);
         }
         $this->personal_wordlist = $personal_wordlist;
     }
     $this->dictionary = pspell_new_config($config);
     if ($this->dictionary === false) {
         throw new NateGoSearchException(sprintf("Could not create Pspell dictionary with language '%s'.", $this->language));
     }
     $this->loadBlacklistedSuggestions();
 }
Ejemplo n.º 2
0
 public static function GetSpellSuggestionsWithPspell($strText, $strSpellLang)
 {
     if (!function_exists('pspell_new')) {
         return true;
     }
     if (file_exists(__DICTIONARY_PATH__ . '/' . $strSpellLang . '.dat')) {
         if (!defined('PSPELL_FAST')) {
             return self::GetSpellSuggestionsWithHunspell($strText, $strSpellLang);
         }
         if (!($pspell_config = pspell_config_create($strSpellLang, null, null, 'utf-8'))) {
             return self::GetSpellSuggestionsWithHunspell($strText, $strSpellLang);
         }
         if (!pspell_config_data_dir($pspell_config, __DICTIONARY_PATH__)) {
             return self::GetSpellSuggestionsWithHunspell($strText, $strSpellLang);
         }
         if (!pspell_config_dict_dir($pspell_config, __DICTIONARY_PATH__)) {
             return self::GetSpellSuggestionsWithHunspell($strText, $strSpellLang);
         }
         if (!($pspell_link = @pspell_new_config($pspell_config))) {
             return self::GetSpellSuggestionsWithHunspell($strText, $strSpellLang);
         }
     } else {
         if (file_exists('/usr/lib/aspell-0.60/' . $strSpellLang . '.dat')) {
             $strDictPath = '/usr/lib/aspell-0.60/';
             $pspell_link = pspell_new($strSpellLang, null, null, 'utf-8');
         } elseif (file_exists('/usr/lib64/aspell-0.60/' . $strSpellLang . '.dat')) {
             $strDictPath = '/usr/lib64/aspell-0.60/';
             $pspell_link = pspell_new($strSpellLang, null, null, 'utf-8');
         } else {
             return self::GetSpellSuggestionsWithHunspell($strText, $strSpellLang);
         }
     }
     $arrSuggestions = array();
     $arrCleanText = mb_split('\\s+', $strText);
     foreach ($arrCleanText as $strCleanText) {
         if (!pspell_check($pspell_link, trim($strCleanText))) {
             $suggestions = pspell_suggest($pspell_link, trim($strCleanText));
             if (in_array($strCleanText, $suggestions)) {
                 continue;
             }
             $arrSuggestions[$strCleanText] = array_slice($suggestions, 0, 3);
         }
     }
     return $arrSuggestions;
 }
 if (isset($_POST["document"])) {
     $document = $_POST["document"];
 }
 #    error_log(print_r($_POST, true));
 if (get_magic_quotes_gpc()) {
     $document = stripslashes($document);
 }
 $words = split(" ", $document);
 # myLog(" *** user: "******" *** language: " . $wgUser->getOption( 'language' ));
 $spellcheckext_language = "" . $wgUser->getOption('language');
 $pspell_config = pspell_config_create($spellcheckext_language);
 global $personalDictionaryLocation, $pspell_data_dir, $pspell_dict_dir;
 pspell_config_personal($pspell_config, $personalDictionaryLocation . "_" . $spellcheckext_language);
 if (strcmp($pspell_data_dir, "") != 0) {
     pspell_config_data_dir($pspell_config, $pspell_data_dir);
     error_log("setting the spellcheck data dir to " . $pspell_data_dir);
 }
 if (strcmp($pspell_dict_dir, "") != 0) {
     pspell_config_dict_dir($pspell_config, $pspell_dict_dir);
 }
 $pspell_link = pspell_new_config($pspell_config);
 $words = array_unique($words);
 $ret = "";
 foreach ($words as $word) {
     if (!is_numeric($word) && !pspell_check($pspell_link, $word)) {
         $suggestions = pspell_suggest($pspell_link, "{$word}");
         $formattedSuggestion = '';
         foreach ($suggestions as $suggestion) {
             $formattedSuggestion .= "<suggest>{$suggestion}</suggest>";
         }