| [ PHPXref.com ] | [ Generated: Sun Jul 20 19:19:21 2008 ] | [ Parasite 1.0.2 ] |
| [ Index ] [ Variables ] [ Functions ] [ Classes ] [ Constants ] [ Statistics ] | ||
[Summary view] [Print] [Text view]
1 <?php 2 3 class SpellChecker { 4 var $command; 5 var $text; 6 var $errors = array(); 7 function SpellChecker($command = 'ispell') { 8 $cwd = getcwd(); 9 $this->command = $command." --home-dir=$cwd"; 10 } 11 function check($text) { 12 // Spell checks text, returns int number of errors found 13 $this->text = $text; 14 // Create temporary file, write text to that file 15 // Important: First, ensure every line starts with ^ to avoid special ispell commands 16 // see http://savannah.gnu.org/download/aspell/manual/user/6_Writing.html 17 $text = '^'.implode("\n^", explode("\n", $text)); 18 $tmp = tempnam('/tmp', 'spl'); 19 $fp = fopen($tmp, 'w'); 20 fwrite($fp, $text); 21 fclose($fp); 22 // Run the spell checking command, capture output 23 $output = `{$this->command} -a < $tmp`; 24 # print '<pre>'; print($output); print '</pre>'; 25 // Delete the temporary file 26 unlink($tmp); 27 // Parse output and populate errors array 28 $num_errors = preg_match_all('|^& (\w+) \d+ \d+: (.*?)$|m', $output, $matches); 29 // This regexp picks up words for which ispell made no suggestions (different pattern) 30 $extra_errors = preg_match_all('|^# (\w+)( ).*$|m', $output, $matches2); 31 // Merge the arrays 32 for ($i = 0; $i < $extra_errors; $i++) { 33 $matches[0][] = $matches2[0][$i]; 34 $matches[1][] = $matches2[1][$i]; 35 $matches[2][] = $matches2[2][$i]; 36 } 37 $num_errors += $extra_errors; 38 for ($i = 0; $i < $num_errors; $i++) { 39 $word = $matches[1][$i]; 40 // Check that word has not already been found as an error 41 if (in_array($word, array_keys($this->errors))) { 42 continue; 43 } 44 $suggestions = explode(', ', $matches[2][$i]); 45 $this->errors[$word] = $suggestions; 46 } 47 return $num_errors; 48 } 49 function getErrors() { 50 return $this->errors; 51 /* errors is an array with the following structure: 52 Array 53 ( 54 [speling] => Array 55 ( 56 [0] => spelling 57 [1] => spewing 58 [2] => spiling 59 ) 60 61 [twwo] => Array 62 ( 63 [0] => two 64 ) 65 ) 66 */ 67 } 68 } 69 70 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| [ Powered by PHPXref - Served by Debian GNU/Linux ] |