Overview

Packages

  • SpellChecker

Classes

  • Horde_SpellChecker
  • Horde_SpellChecker_Aspell
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The Horde_SpellChecker_aspell:: class provides a driver for the 'aspell'
  4:  * program.
  5:  *
  6:  * Copyright 2005-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file COPYING for license information (LGPL). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
 10:  *
 11:  * @author  Chuck Hagenbuch <chuck@horde.org>
 12:  * @author  Michael Slusarz <slusarz@horde.org>
 13:  * @package SpellChecker
 14:  */
 15: class Horde_SpellChecker_Aspell extends Horde_SpellChecker
 16: {
 17:     /**
 18:      * TODO
 19:      *
 20:      * @param string $text  TODO
 21:      *
 22:      * @return array  TODO
 23:      * @throws Horde_Exception
 24:      */
 25:     public function spellCheck($text)
 26:     {
 27:         if ($this->_html) {
 28:             $input = strtr($text, "\n", ' ');
 29:         } else {
 30:             $words = $this->_getWords($text);
 31:             if (!count($words)) {
 32:                 return array('bad' => array(), 'suggestions' => array());
 33:             }
 34:             $input = implode(' ', $words);
 35:         }
 36: 
 37:         // Descriptor array.
 38:         $descspec = array(
 39:             0 => array('pipe', 'r'),
 40:             1 => array('pipe', 'w'),
 41:             2 => array('pipe', 'w')
 42:         );
 43: 
 44:         $process = proc_open($this->_cmd(), $descspec, $pipes);
 45:         if (!is_resource($process)) {
 46:             throw new Horde_Exception('Spellcheck failed. Command line: ' . $this->_cmd());
 47:         }
 48: 
 49:         // The '^' character tells aspell to spell check the entire line.
 50:         fwrite($pipes[0], '^' . $input);
 51:         fclose($pipes[0]);
 52: 
 53:         // Read stdout.
 54:         $out = '';
 55:         while (!feof($pipes[1])) {
 56:             $out .= fread($pipes[1], 8192);
 57:         }
 58:         fclose($pipes[1]);
 59: 
 60:         // Read stderr.
 61:         $err = '';
 62:         while (!feof($pipes[2])) {
 63:             $err .= fread($pipes[2], 8192);
 64:         }
 65:         fclose($pipes[2]);
 66: 
 67:         // We can't rely on the return value of proc_close:
 68:         // http://bugs.php.net/bug.php?id=29123
 69:         proc_close($process);
 70: 
 71:         if (strlen($out) === 0) {
 72:             throw new Horde_Exception('Spellcheck failed. Command line: ' . $this->_cmd());
 73:         }
 74: 
 75:         // Parse output.
 76:         $bad = $suggestions = array();
 77:         $lines = explode("\n", $out);
 78:         foreach ($lines as $line) {
 79:             $line = trim($line);
 80:             if (empty($line)) {
 81:                 continue;
 82:             }
 83: 
 84:             @list(,$word,) = explode(' ', $line, 3);
 85: 
 86:             if ($this->_inLocalDictionary($word) || in_array($word, $bad)) {
 87:                 continue;
 88:             }
 89: 
 90:             switch ($line[0]) {
 91:             case '#':
 92:                 // Misspelling with no suggestions.
 93:                 $bad[] = $word;
 94:                 $suggestions[] = array();
 95:                 break;
 96: 
 97:             case '&':
 98:                 // Suggestions.
 99:                 $bad[] = $word;
100:                 $suggestions[] = array_slice(explode(', ', substr($line, strpos($line, ':') + 2)), 0, $this->_maxSuggestions);
101:                 break;
102:             }
103:         }
104: 
105:         return array('bad' => $bad, 'suggestions' => $suggestions);
106:     }
107: 
108:     /**
109:      * Create the command line string.
110:      *
111:      * @return string  The command to run.
112:      */
113:     protected function _cmd()
114:     {
115:         $args = '--encoding=UTF-8';
116: 
117:         switch ($this->_suggestMode) {
118:         case self::SUGGEST_FAST:
119:             $args .= ' --sug-mode=fast';
120:             break;
121: 
122:         case self::SUGGEST_SLOW:
123:             $args .= ' --sug-mode=bad-spellers';
124:             break;
125: 
126:         default:
127:             $args .= ' --sug-mode=normal';
128:         }
129: 
130:         $args .= ' --lang=' . escapeshellarg($this->_locale);
131: 
132:         if ($this->_html) {
133:             $args .= ' -H';
134:         }
135: 
136:         return 'aspell -a ' . $args;
137:     }
138: 
139: }
140: 
API documentation generated by ApiGen