1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Horde_SpellChecker_Aspell extends Horde_SpellChecker
16: {
17: 18: 19: 20: 21: 22: 23: 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:
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:
50: fwrite($pipes[0], '^' . $input);
51: fclose($pipes[0]);
52:
53:
54: $out = '';
55: while (!feof($pipes[1])) {
56: $out .= fread($pipes[1], 8192);
57: }
58: fclose($pipes[1]);
59:
60:
61: $err = '';
62: while (!feof($pipes[2])) {
63: $err .= fread($pipes[2], 8192);
64: }
65: fclose($pipes[2]);
66:
67:
68:
69: proc_close($process);
70:
71: if (strlen($out) === 0) {
72: throw new Horde_Exception('Spellcheck failed. Command line: ' . $this->_cmd());
73: }
74:
75:
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:
93: $bad[] = $word;
94: $suggestions[] = array();
95: break;
96:
97: case '&':
98:
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: 110: 111: 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: