Changeset 503
- Timestamp:
- Feb 10, 2015 12:03:25 AM (9 years ago)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/1.1dev/lib/SpellCheck.inc.php
r434 r503 20 20 'highlight_end' => '</strong>', 21 21 ); 22 22 23 23 var $_pspell_cfg_handle; 24 24 var $_pspell_handle; 25 25 var $_use_personal_wordlist = false; 26 26 var $_errors = array(); 27 27 28 28 /** 29 29 * Constructor. … … 85 85 return $this->_params[$param]; 86 86 } 87 87 88 88 /** 89 89 * Check whether any errors have been triggered. … … 103 103 $this->_errors = array(); 104 104 } 105 105 106 106 /** 107 107 * Check one word. … … 123 123 } 124 124 } 125 125 126 126 /** 127 127 * Suggest the correct spelling for one misspelled word. … … 138 138 return pspell_suggest($this->_pspell_handle, $word); 139 139 } 140 140 141 141 /** 142 142 * Add a word to a personal list. … … 154 154 if (pspell_add_to_personal($this->_pspell_handle, $word)) { 155 155 logMsg(sprintf('Added "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_DEBUG, __FILE__, __LINE__); 156 return true; 156 return true; 157 157 } else { 158 158 logMsg(sprintf('Failed adding "%s" to personal wordlist: %s', $word, $this->getParam('personal_wordlist')), LOG_ERR, __FILE__, __LINE__); … … 161 161 } 162 162 } 163 163 164 164 /** 165 165 * Save personal list to file. … … 184 184 } 185 185 } 186 186 187 187 /** 188 188 * Returns an array of suggested words for each misspelled word in the given text. … … 221 221 } 222 222 } 223 223 224 224 /** 225 225 * Checks all words in a given string. … … 255 255 } 256 256 } 257 257 258 258 /** 259 259 * Returns a given string with misspelled words highlighted. … … 267 267 */ 268 268 function getStringHighlighted($string, $show_footnote=false) 269 { 269 { 270 270 // Split words on punctuation except apostrophes (this regex is used in several places in this class). 271 271 // http://stackoverflow.com/questions/790596/split-a-text-into-single-words … … 279 279 $footnote = $show_footnote ? '<sup style="color:#999;">' . ++$cnt . '</sup>' : ''; 280 280 $words[$i] = $this->getParam('highlight_start') . $word . $this->getParam('highlight_end') . $footnote; 281 $string = preg_replace( "/\b$word\b/", $words[$i], $string);281 $string = preg_replace(sprintf('/\b%s\b/', preg_quote($word, '/')), $words[$i], $string); 282 282 } 283 283 } 284 284 } 285 285 return $string; 286 // return join(' ', $words); 287 } 288 286 } 287 289 288 /** 290 289 * Prints the HTML for correcting all misspellings found in the text of one $_FORM element. … … 302 301 <input name="<?php echo $form_name ?>" type="hidden" value="<?php echo oTxt(getFormData($form_name)) ?>" /> 303 302 <?php 304 303 305 304 $form_words = $this->getStringSuggestions(getFormData($form_name)); 306 305 if (is_array($form_words) && !empty($form_words)) { … … 315 314 <option value="<?php echo $original_word ?>">(<?php echo $original_word ?>)</option> 316 315 <?php 317 316 318 317 foreach ($words as $suggestion) { 319 318 ?> … … 321 320 <?php 322 321 } 323 322 324 323 ?> 325 324 </select> … … 334 333 } 335 334 } 336 335 337 336 /** 338 337 * Tests if any form spelling corrections have been submitted. … … 348 347 return (false !== getFormData('spelling_suggestions', false)) || (false !== getFormData('spelling_corrections', false)); 349 348 } 350 349 351 350 /** 352 351 * Replace the misspelled words in the text of a specified form with the corrections. -
trunk/lib/SpellCheck.inc.php
r502 r503 4 4 * For details visit the project site: <http://trac.strangecode.com/codebase/> 5 5 * Copyright 2001-2012 Strangecode, LLC 6 * 6 * 7 7 * This file is part of The Strangecode Codebase. 8 8 * … … 11 11 * Free Software Foundation, either version 3 of the License, or (at your option) 12 12 * any later version. 13 * 13 * 14 14 * The Strangecode Codebase is distributed in the hope that it will be useful, but 15 15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 16 16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 17 * details. 18 * 18 * 19 19 * You should have received a copy of the GNU General Public License along with 20 20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. … … 141 141 { 142 142 $app =& App::getInstance(); 143 143 144 144 if (array_key_exists($param, $this->_params)) { 145 145 return $this->_params[$param]; … … 267 267 { 268 268 $corrections = array(); 269 // Split words on punctuation except apostrophes 269 // Split words on punctuation except apostrophes (this regex is used in several places in this class). 270 270 // http://stackoverflow.com/questions/790596/split-a-text-into-single-words 271 $words = preg_split("/((^\p{P}+)|(\p{P}*\s+\p{P}*)|[\p{Pd}-ââ]+|(\+|(\p{P}+$))/", $string); 272 // Remove non-word elements. 273 $words = preg_grep('/\w+/', $words); 274 271 $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}ââ-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE); 275 272 if (is_array($words) && !empty($words)) { 273 // Remove non-word elements. 274 $words = preg_grep('/\w+/', $words); 275 $words = array_map('strip_tags', $words); 276 276 foreach ($words as $i => $word) { 277 277 if (!$this->check($word)) { … … 303 303 { 304 304 $errors = array(); 305 $words = preg_split('/([\W]+?)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); 306 // Remove non-word elements. 307 $check_words = preg_grep('/\w+/', $words); 308 309 if (is_array($check_words) && !empty($check_words)) { 310 foreach ($check_words as $i => $word) { 305 // Split words on punctuation except apostrophes (this regex is used in several places in this class). 306 // http://stackoverflow.com/questions/790596/split-a-text-into-single-words 307 $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}ââ-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE); 308 if (is_array($words) && !empty($words)) { 309 // Remove non-word elements. 310 $words = preg_grep('/\w+/', $words); 311 $words = array_map('strip_tags', $words); 312 foreach ($words as $i => $word) { 311 313 if (!$this->check($word)) { 312 314 $errors[] = $word; … … 334 336 public function getStringHighlighted($string, $show_footnote=false) 335 337 { 336 $words = preg_split('/([\W]+?)/', $string, -1, PREG_SPLIT_DELIM_CAPTURE); 337 $check_words = preg_grep('/\w+/', $words); 338 // Split words on punctuation except apostrophes (this regex is used in several places in this class). 339 // http://stackoverflow.com/questions/790596/split-a-text-into-single-words 340 $words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}ââ-]+|(?:\p{P}+$))/", $string, -1, PREG_SPLIT_DELIM_CAPTURE); 338 341 $cnt = 0; 339 if (is_array($check_words) && !empty($check_words)) { 340 foreach ($check_words as $i => $word) { 342 if (is_array($words) && !empty($words)) { 343 $words = preg_grep('/\w+/', $words); 344 $words = array_map('strip_tags', $words); 345 foreach ($words as $i => $word) { 341 346 if (!$this->check($word)) { 342 $footnote = $show_footnote ? '<sup>' . ++$cnt . '</sup>' : ''; 343 $words[$i] = $this->getParam('highlight_start') . $word . $this->getParam('highlight_end') . $footnote; 347 $footnote = $show_footnote ? '<sup style="color:#999;">' . ++$cnt . '</sup>' : ''; 348 $words[$i] = $this->getParam('highlight_start') . $word . $this->getParam('highlight_end') . $footnote; 349 $string = preg_replace(sprintf('/\b%s\b/', preg_quote($word, '/')), $words[$i], $string); 344 350 } 345 351 } 346 352 } 347 return join('', $words);353 return $string; 348 354 } 349 355 … … 361 367 { 362 368 ?> 363 <input name="<?php echo $form_name ?>" type="hidden" value="<?php echo getFormData($form_name) ?>" />369 <input name="<?php echo $form_name ?>" type="hidden" value="<?php echo oTxt(getFormData($form_name)) ?>" /> 364 370 <?php 365 371 … … 370 376 ?> 371 377 <li> 372 <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;"> 378 <label style="color:#999;"><sub style="vertical-align:text-top;"><?php echo $j++; ?></sub></label> 379 <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;"> 373 380 <?php $original_word = array_shift($words); ?> 374 381 <option value="<?php echo $original_word ?>">(<?php echo $original_word ?>)</option> … … 383 390 ?> 384 391 </select> 385 <input type="text" name="spelling_corrections[<?php echo $form_name ?>][<?php echo $i ?>]" value="<?php echo $original_word ?>" class="sc-small" />392 <input type="text" name="spelling_corrections[<?php echo $form_name ?>][<?php echo $i ?>]" value="<?php echo oTxt($original_word) ?>" size="20"> 386 393 <?php if ($this->_use_personal_wordlist) { ?> 387 394 <input name="save_to_personal_wordlist[]" type="checkbox" value="<?php echo $i ?>" /><?php echo _("Learn spelling") ?> … … 420 427 public function applyFormCorrections($form_name) 421 428 { 422 $form_words = preg_split('/([\W]+?)/', getFormData($form_name), -1, PREG_SPLIT_DELIM_CAPTURE); 429 // Split words on punctuation except apostrophes (this regex is used in several places in this class). 430 // http://stackoverflow.com/questions/790596/split-a-text-into-single-words 431 $form_words = preg_split("/((?:^\p{P}+)|(?:\p{P}*\s+\p{P}*)|[\p{Pd}ââ-]+|(?:\p{P}+$))/", getFormData($form_name), -1, PREG_SPLIT_DELIM_CAPTURE); 423 432 $suggestions = getFormData('spelling_suggestions'); 424 433 $corrections = getFormData('spelling_corrections');
Note: See TracChangeset
for help on using the changeset viewer.