Changeset 15 for trunk/lib/SpellCheck.inc.php
- Timestamp:
- Nov 13, 2005 4:51:22 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/SpellCheck.inc.php
r14 r15 7 7 * 8 8 * @author Quinn Comendant <quinn@strangecode.com> 9 * @version 1. 09 * @version 1.1 10 10 */ 11 11 12 12 /* Implementation example: 13 13 -------------------------------------------------------------------------------- 14 include '_config.inc.php';14 include_once dirname(__FILE__) . '/_config.inc.php'; 15 15 include 'codebase/lib/SpellCheck.inc.php'; 16 16 17 // Instantiate with language and optionally the path to the custom wordlist file. 18 $spell = new SpellCheck('en', '/tmp/my_custom_dict'); 17 // Instantiate with parameters. In this example we'll set the language and the path to the personal wordlist file. 18 $spell = new SpellCheck(array( 19 'language' => 'en', 20 'personal_wordlist' => '/tmp/my_custom_dict' 21 )); 22 23 // Just for the heck of it add a new word to persistent personal wordlist file. 24 $spell->add('mealworm'); 19 25 20 26 $text_to_check = 'donky rinds taste like mealworm paste'; 21 22 // Add new word to persistent custom wordlist file.23 $spell->add('mealworm');24 27 25 28 if (!$spell->checkString($text_to_check)) { 26 29 $suggestions = $spell->getStringSuggestions($text_to_check); 27 echo 'Spelling errors :';30 echo 'Spelling errors! Here are suggested alternatives:'; 28 31 print_r($suggestions); 29 32 } else { … … 39 42 40 43 var $_params = array( 41 'personal_wordlist' => '', 42 'skip_len' => 3, 44 'language' => 'en', 45 'personal_wordlist' => '', // Text file to save custom words to. 46 'skip_length' => 3, // Words with this many chars or less will not be checked. 43 47 'mode' => PSPELL_NORMAL, // PSPELL_FAST, PSPELL_NORMAL, or PSPELL_BAD_SPELLERS. 44 48 'highlight_start' => '<strong style="color:red;">', … … 53 57 /** 54 58 * Constructor. 55 */ 56 function SpellCheck($lang='en', $personal_wordlist=null) 57 { 58 $this->_pspell_cfg_handle = pspell_config_create($lang); 59 60 pspell_config_ignore($this->_pspell_cfg_handle, $skip_len); 61 pspell_config_mode($this->_pspell_cfg_handle, $mode); 62 63 if (isset($personal_wordlist)) { 64 if (!is_writable(dirname($personal_wordlist)) && !is_writable($personal_wordlist)) { 65 App::logMsg(sprintf('Personal wordlist file not writable: %s', $personal_wordlist), LOG_NOTICE, __FILE__, __LINE__); 59 * 60 * @param array $params Array of parameters (key => val pairs). 61 */ 62 function SpellCheck($params) 63 { 64 if (!is_array($params) || empty($params)) { 65 trigger_error('SpellCheck parameters not set properly', E_USER_ERROR); 66 } 67 68 $this->setParam($params); 69 70 $this->_pspell_cfg_handle = pspell_config_create($this->getParam('language')); 71 72 pspell_config_ignore($this->_pspell_cfg_handle, $this->getParam('skip_length')); 73 pspell_config_mode($this->_pspell_cfg_handle, $this->getParam('mode')); 74 75 if ('' != $this->getParam('personal_wordlist')) { 76 if (!is_writable(dirname($this->getParam('personal_wordlist'))) || !is_writable($this->getParam('personal_wordlist'))) { 77 App::logMsg(sprintf('Personal wordlist file not writable: %s', $this->getParam('personal_wordlist')), LOG_WARNING, __FILE__, __LINE__); 66 78 } else { 67 $this->setParam(array('personal_wordlist' => $personal_wordlist)); 68 pspell_config_personal($this->_pspell_cfg_handle, $personal_wordlist); 79 pspell_config_personal($this->_pspell_cfg_handle, $this->getParam('personal_wordlist')); 69 80 $this->_use_personal_wordlist = true; 70 App::logMsg(sprintf('Using personal wordlist: %s', $ personal_wordlist), LOG_DEBUG, __FILE__, __LINE__);81 App::logMsg(sprintf('Using personal wordlist: %s', $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__); 71 82 } 72 83 } … … 174 185 { 175 186 if ($this->_use_personal_wordlist) { 176 App::logMsg(sprintf('Added "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__); 177 return pspell_add_to_personal($this->_pspell_handle, $word); 187 if (pspell_add_to_personal($this->_pspell_handle, $word)) { 188 App::logMsg(sprintf('Added "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__); 189 return true; 190 } else { 191 App::logMsg(sprintf('Failed adding "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_ERR, __FILE__, __LINE__); 192 return false; 193 } 178 194 } 179 195 }
Note: See TracChangeset
for help on using the changeset viewer.