[207] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * SpellCheck.inc.php |
---|
| 4 | * code by strangecode :: www.strangecode.com :: this document contains copyrighted information |
---|
| 5 | * |
---|
| 6 | * Interface to PHP's pspell functions. |
---|
| 7 | * |
---|
| 8 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 9 | * @version 1.0 |
---|
| 10 | */ |
---|
| 11 | class SpellCheck { |
---|
| 12 | |
---|
| 13 | // Default parameters. |
---|
| 14 | var $_params = array( |
---|
| 15 | 'language' => 'en', |
---|
| 16 | 'personal_wordlist' => '', |
---|
| 17 | 'skip_length' => 3, |
---|
| 18 | 'mode' => PSPELL_NORMAL, // PSPELL_FAST, PSPELL_NORMAL, or PSPELL_BAD_SPELLERS. |
---|
| 19 | 'highlight_start' => '<strong style="color:red;">', |
---|
| 20 | 'highlight_end' => '</strong>', |
---|
| 21 | ); |
---|
| 22 | |
---|
| 23 | var $_pspell_cfg_handle; |
---|
| 24 | var $_pspell_handle; |
---|
| 25 | var $_use_personal_wordlist = false; |
---|
| 26 | var $_errors = array(); |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Constructor. |
---|
| 30 | * |
---|
| 31 | * @param array $params Array of parameters (key => val pairs). |
---|
| 32 | */ |
---|
| 33 | function SpellCheck($params) |
---|
| 34 | { |
---|
| 35 | if (!is_array($params) || empty($params)) { |
---|
| 36 | trigger_error('SpellCheck parameters not set properly', E_USER_ERROR); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | $this->setParam($params); |
---|
| 40 | |
---|
| 41 | $this->_pspell_cfg_handle = pspell_config_create($this->getParam('language')); |
---|
| 42 | |
---|
| 43 | pspell_config_ignore($this->_pspell_cfg_handle, $this->getParam('skip_length')); |
---|
| 44 | pspell_config_mode($this->_pspell_cfg_handle, $this->getParam('mode')); |
---|
| 45 | |
---|
| 46 | if ('' != $this->getParam('personal_wordlist')) { |
---|
| 47 | if (!file_exists($this->getParam('personal_wordlist')) || !is_writable($this->getParam('personal_wordlist'))) { |
---|
| 48 | logMsg(sprintf('Personal wordlist file does not exist or is not writable: %s', $this->getParam('personal_wordlist')), LOG_WARNING, __FILE__, __LINE__); |
---|
| 49 | } else { |
---|
| 50 | pspell_config_personal($this->_pspell_cfg_handle, $this->getParam('personal_wordlist')); |
---|
| 51 | $this->_use_personal_wordlist = true; |
---|
| 52 | logMsg(sprintf('Using personal wordlist: %s', $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | $this->_pspell_handle = pspell_new_config($this->_pspell_cfg_handle); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Set (or overwrite existing) parameters by passing an array of new parameters. |
---|
| 61 | * |
---|
| 62 | * @access public |
---|
| 63 | * |
---|
| 64 | * @param array $params Array of parameters (key => val pairs). |
---|
| 65 | */ |
---|
| 66 | function setParam($params=null) |
---|
| 67 | { |
---|
| 68 | if (isset($params) && is_array($params)) { |
---|
| 69 | // Merge new parameters with old overriding only those passed. |
---|
| 70 | $this->_params = array_merge($this->_params, $params); |
---|
| 71 | } else { |
---|
| 72 | logMsg(sprintf('Supplied argument is not an array: %s', $params), LOG_WARNING, __FILE__, __LINE__); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * Return the value of a parameter. |
---|
| 78 | * |
---|
| 79 | * @access public |
---|
| 80 | * @param string $param The key of the parameter to return. |
---|
| 81 | * @return mixed Parameter value. |
---|
| 82 | */ |
---|
| 83 | function getParam($param) |
---|
| 84 | { |
---|
| 85 | return $this->_params[$param]; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Check whether any errors have been triggered. |
---|
| 90 | * |
---|
| 91 | * @return bool True if any errors were found, false otherwise. |
---|
| 92 | */ |
---|
| 93 | function anyErrors() |
---|
| 94 | { |
---|
| 95 | return (sizeof($this->_errors) > 0); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * Reset the error list. |
---|
| 100 | */ |
---|
| 101 | function resetErrorList() |
---|
| 102 | { |
---|
| 103 | $this->_errors = array(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Check one word. |
---|
| 108 | * |
---|
| 109 | * @access public |
---|
| 110 | * @param string $word |
---|
| 111 | * @return bool True if word is correct. |
---|
| 112 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 113 | * @version 1.0 |
---|
| 114 | * @since 09 Jun 2005 18:23:51 |
---|
| 115 | */ |
---|
| 116 | function check($word) |
---|
| 117 | { |
---|
| 118 | if (pspell_check($this->_pspell_handle, $word)) { |
---|
| 119 | return true; |
---|
| 120 | } else { |
---|
| 121 | $this->_errors[] = $word; |
---|
| 122 | return false; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * Suggest the correct spelling for one misspelled word. |
---|
| 128 | * |
---|
| 129 | * @access public |
---|
| 130 | * @param string $word |
---|
| 131 | * @return array Word suggestions. |
---|
| 132 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 133 | * @version 1.0 |
---|
| 134 | * @since 09 Jun 2005 18:23:51 |
---|
| 135 | */ |
---|
| 136 | function suggest($word) |
---|
| 137 | { |
---|
| 138 | return pspell_suggest($this->_pspell_handle, $word); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
| 142 | * Add a word to a personal list. |
---|
| 143 | * |
---|
| 144 | * @access public |
---|
| 145 | * @param string $word |
---|
| 146 | * @return array Word suggestions. |
---|
| 147 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 148 | * @version 1.0 |
---|
| 149 | * @since 09 Jun 2005 18:23:51 |
---|
| 150 | */ |
---|
| 151 | function add($word) |
---|
| 152 | { |
---|
| 153 | if ($this->_use_personal_wordlist) { |
---|
| 154 | if (pspell_add_to_personal($this->_pspell_handle, $word)) { |
---|
| 155 | logMsg(sprintf('Added "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 156 | return true; |
---|
| 157 | } else { |
---|
| 158 | logMsg(sprintf('Failed adding "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_ERR, __FILE__, __LINE__); |
---|
| 159 | return false; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * Save personal list to file. |
---|
| 166 | * |
---|
| 167 | * @access public |
---|
| 168 | * @param string $word |
---|
| 169 | * @return array Word suggestions. |
---|
| 170 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 171 | * @version 1.0 |
---|
| 172 | * @since 09 Jun 2005 18:23:51 |
---|
| 173 | */ |
---|
| 174 | function save() |
---|
| 175 | { |
---|
| 176 | if ($this->_use_personal_wordlist) { |
---|
| 177 | if (pspell_save_wordlist($this->_pspell_handle)) { |
---|
| 178 | logMsg(sprintf('Saved personal wordlist: %s', $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 179 | return true; |
---|
| 180 | } else { |
---|
| 181 | logMsg(sprintf('Failed saving personal wordlist: %s', $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 182 | return false; |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /** |
---|
| 188 | * Returns an array of suggested words for each mispelled word in the given text. |
---|
| 189 | * The first word of the returned array is the (possibly) misspelled word. |
---|
| 190 | * |
---|
| 191 | * @access public |
---|
| 192 | * @param string $string String to get suggestions for. |
---|
| 193 | * @return mixed Array of suggested words or false if none. |
---|
| 194 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 195 | * @version 1.0 |
---|
| 196 | * @since 09 Jun 2005 21:29:49 |
---|
| 197 | */ |
---|
| 198 | function getStringSuggestions($string) |
---|
| 199 | { |
---|
| 200 | $corrections = array(); |
---|
| 201 | $words = preg_split('/([\W]+?)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
| 202 | // Remove non-word elements. |
---|
| 203 | $words = preg_grep('/\w+/', $words); |
---|
| 204 | |
---|
| 205 | if (is_array($words) && !empty($words)) { |
---|
| 206 | foreach ($words as $i => $word) { |
---|
| 207 | if (!$this->check($word)) { |
---|
| 208 | $corrections[$i] = $this->suggest($word); |
---|
| 209 | // Keep the original spelling as one of the suggestions. |
---|
| 210 | array_unshift($corrections[$i], $word); |
---|
| 211 | array_unique($corrections[$i]); |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | if (is_array($corrections) && !empty($corrections)) { |
---|
| 216 | return $corrections; |
---|
| 217 | } else { |
---|
| 218 | return false; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | /** |
---|
| 223 | * Checks all words in a given string. |
---|
| 224 | * |
---|
| 225 | * @access public |
---|
| 226 | * @param string $string String to check. |
---|
| 227 | * @return void |
---|
| 228 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 229 | * @version 1.0 |
---|
| 230 | * @since 09 Jun 2005 22:11:27 |
---|
| 231 | */ |
---|
| 232 | function checkString($string) |
---|
| 233 | { |
---|
| 234 | $errors = array(); |
---|
| 235 | $words = preg_split('/([\W]+?)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
| 236 | // Remove non-word elements. |
---|
| 237 | $check_words = preg_grep('/\w+/', $words); |
---|
| 238 | |
---|
| 239 | if (is_array($check_words) && !empty($check_words)) { |
---|
| 240 | foreach ($check_words as $i => $word) { |
---|
| 241 | if (!$this->check($word)) { |
---|
| 242 | $errors[] = $word; |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | } |
---|
| 246 | if (empty($errors)) { |
---|
| 247 | return true; |
---|
| 248 | } else { |
---|
| 249 | $this->_errors = $errors + $this->_errors; |
---|
| 250 | return false; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | /** |
---|
| 255 | * Returns a given string with misspelled words highlighted. |
---|
| 256 | * |
---|
| 257 | * @access public |
---|
| 258 | * @param string $string Text to highlight. |
---|
| 259 | * @return string Highlighted text. |
---|
| 260 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 261 | * @version 1.0 |
---|
| 262 | * @since 09 Jun 2005 21:29:49 |
---|
| 263 | */ |
---|
| 264 | function getStringHighlighted($string, $show_footnote=false) |
---|
| 265 | { |
---|
| 266 | $words = preg_split('/([\W]+?)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
| 267 | $check_words = preg_grep('/\w+/', $words); |
---|
| 268 | $cnt = 0; |
---|
| 269 | if (is_array($check_words) && !empty($check_words)) { |
---|
| 270 | foreach ($check_words as $i => $word) { |
---|
| 271 | if (!$this->check($word)) { |
---|
| 272 | $footnote = $show_footnote ? '<sup>' . ++$cnt . '</sup>' : ''; |
---|
| 273 | $words[$i] = $this->getParam('highlight_start') . $word . $this->getParam('highlight_end') . $footnote; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | return join('', $words); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | /** |
---|
| 281 | * Prints the HTML for correcting all mispellings found in the text of one $_FORM element. |
---|
| 282 | * |
---|
| 283 | * @access public |
---|
| 284 | * @param string $form_name Name of the form to check. |
---|
| 285 | * @return void |
---|
| 286 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 287 | * @version 1.0 |
---|
| 288 | * @since 09 Jun 2005 21:29:49 |
---|
| 289 | */ |
---|
| 290 | function printCorrectionForm($form_name) |
---|
| 291 | { |
---|
| 292 | ?> |
---|
| 293 | <input name="<?php echo $form_name ?>" type="hidden" value="<?php echo oTxt(getFormData($form_name)) ?>" /> |
---|
| 294 | <?php |
---|
| 295 | |
---|
| 296 | $form_words = $this->getStringSuggestions(getFormData($form_name)); |
---|
| 297 | if (is_array($form_words) && !empty($form_words)) { |
---|
| 298 | ?><ol><?php |
---|
| 299 | foreach ($form_words as $i => $words) { |
---|
| 300 | ?> |
---|
| 301 | <li> |
---|
[289] | 302 | <select name="spelling_suggestions[<?php echo $form_name ?>][<?php echo $i ?>]" onchange="document.forms[0].elements['spelling_corrections[<?php echo $form_name ?>][<?php echo $i ?>]'].value = this.value;"> |
---|
[207] | 303 | <?php $original_word = array_shift($words); ?> |
---|
| 304 | <option value="<?php echo $original_word ?>">(<?php echo $original_word ?>)</option> |
---|
| 305 | <?php |
---|
| 306 | |
---|
| 307 | foreach ($words as $suggestion) { |
---|
| 308 | ?> |
---|
| 309 | <option value="<?php echo $suggestion ?>"><?php echo $suggestion ?></option> |
---|
| 310 | <?php |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | ?> |
---|
| 314 | </select> |
---|
| 315 | <input type="text" name="spelling_corrections[<?php echo $form_name ?>][<?php echo $i ?>]" value="<?php echo oTxt($original_word) ?>" size="20"> |
---|
| 316 | <?php if ($this->_use_personal_wordlist) { ?> |
---|
| 317 | <input name="save_to_personal_wordlist[]" type="checkbox" value="<?php echo $i ?>" /><?php echo _("Learn spelling") ?> |
---|
| 318 | <?php } ?> |
---|
| 319 | </li> |
---|
| 320 | <?php |
---|
| 321 | } |
---|
| 322 | ?></ol><?php |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | /** |
---|
| 327 | * Tests if any form spelling corrections have been submitted. |
---|
| 328 | * |
---|
| 329 | * @access public |
---|
| 330 | * @return bool True if form spelling has been checked. |
---|
| 331 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 332 | * @version 1.0 |
---|
| 333 | * @since 09 Jun 2005 23:15:35 |
---|
| 334 | */ |
---|
| 335 | function anyFormCorrections() |
---|
| 336 | { |
---|
| 337 | return (false !== getFormData('spelling_suggestions', false)) || (false !== getFormData('spelling_corrections', false)); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | /** |
---|
| 341 | * Replace the misspelled words in the text of a specified form with the corrections. |
---|
| 342 | * |
---|
| 343 | * @access public |
---|
| 344 | * @param string $form_name Name of form to apply corrections to. |
---|
| 345 | * @return string Corrected form text. |
---|
| 346 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 347 | * @version 1.0 |
---|
| 348 | * @since 09 Jun 2005 23:18:51 |
---|
| 349 | */ |
---|
| 350 | function applyFormCorrections($form_name) |
---|
| 351 | { |
---|
| 352 | $form_words = preg_split('/([\W]+?)/', getFormData($form_name), -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
| 353 | $suggestions = getFormData('spelling_suggestions'); |
---|
| 354 | $corrections = getFormData('spelling_corrections'); |
---|
| 355 | |
---|
| 356 | $form_words = array_diff($corrections[$form_name], array('')) + $suggestions[$form_name] + $form_words; |
---|
| 357 | ksort($form_words); |
---|
| 358 | |
---|
| 359 | if ($this->_use_personal_wordlist) { |
---|
| 360 | $save_to_personal_wordlist = getFormData('save_to_personal_wordlist'); |
---|
| 361 | if (is_array($save_to_personal_wordlist) && !empty($save_to_personal_wordlist)) { |
---|
| 362 | foreach ($save_to_personal_wordlist as $cust) { |
---|
| 363 | $this->add($form_words[$cust]); |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | $this->save(); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | if (is_array($form_words) && !empty($form_words)) { |
---|
| 370 | return join('', $form_words); |
---|
| 371 | } else { |
---|
| 372 | return getFormData($form_name); |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | } // End. |
---|
| 377 | |
---|
| 378 | ?> |
---|