1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
<?php
/**
* Aspell interface
*
* This library gives full access to aspell's pipe interface. Optionally it
* provides some of the functions from the pspell PHP extension by wrapping
* them to calls to the aspell binary.
*
* It can be simply dropped into code written for the pspell extension like
* the following
*
* if(!function_exists('pspell_suggest')){
* define('PSPELL_COMP',1);
* require_once ("pspell_comp.php");
* }
*
* Define the path to the aspell binary like this if needed:
*
* define('ASPELL_BIN','/path/to/aspell');
*
* @author Andreas Gohr <andi@splitbrain.org>
* @todo Not all pspell functions are supported
*
*/
// path to your aspell binary
if(!defined('ASPELL_BIN')) define('ASPELL_BIN','aspell');
if(!defined('PSPELL_FAST')) define(PSPELL_FAST,1); # Fast mode (least number of suggestions)
if(!defined('PSPELL_NORMAL')) define(PSPELL_NORMAL,2); # Normal mode (more suggestions)
if(!defined('PSPELL_BAD_SPELLERS')) define(PSPELL_BAD_SPELLERS,3); # Slow mode (a lot of suggestions)
if(!defined('ASPELL_ULTRA')) define(ASPELL_ULTRA,4); # Ultra fast mode (not available in Pspell!)
/**
* You can define PSPELL_COMP to use this class as drop in replacement
* for the pspell extension
*/
if(defined('PSPELL_COMP')){
// spelling is not supported by aspell and ignored
function pspell_config_create($language, $spelling=null, $jargon=null, $encoding='iso8859-1'){
return new Aspell($language, $jargon, $encoding);
}
function pspell_config_mode(&$config, $mode){
return $config->setMode($mode);
}
function pspell_new_config(&$config){
return $config;
}
function pspell_check(&$dict,$word){
return $dict->check($word);
}
function pspell_suggest(&$dict, $word){
return $dict->suggest($word);
}
}
/**
* Class to interface aspell
*
* Needs PHP >= 4.3.0
*/
class Aspell{
var $language = null;
var $jargon = null;
var $encoding = 'iso8859-1';
var $mode = PSPELL_NORMAL;
var $args='';
/**
* Constructor. Works like pspell_config_create()
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function Aspell($language, $jargon=null, $encoding='iso8859-1'){
$this->language = $language;
$this->jargon = $jargon;
$this->encoding = $encoding;
$this->_prepareArgs();
}
/**
* Set the spelling mode like pspell_config_mode()
*
* Mode can be PSPELL_FAST, PSPELL_NORMAL, PSPELL_BAD_SPELLER or ASPELL_ULTRA
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function setMode($mode){
if(!in_array($mode,array(PSPELL_FAST,PSPELL_NORMAL,PSPELL_BAD_SPELLER,ASPELL_ULTRA))){
$mode = PSPELL_NORMAL;
}
$this->mode = $mode;
$this->_prepareArgs();
return $mode;
}
/**
* Prepares the needed arguments for the call to the aspell binary
*
* No need to call this directly
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function _prepareArgs(){
$this->args = '';
if($this->language){
$this->args .= ' --lang='.escapeshellarg($this->language);
}else{
return false; // no lang no spell
}
if($this->jargon){
$this->args .= ' --jargon='.escapeshellarg($this->jargon);
}
if($this->encoding){
$this->args .= ' --encoding='.escapeshellarg($this->encoding);
}
switch ($this->mode){
case PSPELL_FAST:
$this->args .= ' --sug-mode=fast';
break;
case PSPELL_BAD_SPELLERS:
$this->args .= ' --sug-mode=bad-spellers';
break;
case ASPELL_ULTRA:
$this->args .= ' --sug-mode=ultra';
break;
default:
$this->args .= ' --sug-mode=normal';
}
return true;
}
/**
* Pipes a text to aspell
*
* This opens a bidirectional pipe to the aspell binary, writes
* the given text to STDIN and returns STDOUT and STDERR
*
* You have full access to aspell's pipe mode here - this means you need
* quote your lines yourself read the aspell manual for more info
*
* @author Andreas Gohr <andi@splitbrain.org>
* @link http://aspell.sf.net/man-html/Through-A-Pipe.html
*/
function runAspell($text,&$out,&$err){
if(empty($text)) return true;
//prepare file descriptors
$descspec = array(
0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
2 => array('pipe', 'w') // stderr is a file to write to
);
$process = proc_open(ASPELL_BIN.' -a'.$this->args, $descspec, $pipes);
if ($process) {
//write to stdin
fwrite($pipes[0],$text);
fclose($pipes[0]);
//read stdout
while (!feof($pipes[1])) {
$out .= fread($pipes[1], 8192);
}
fclose($pipes[1]);
//read stderr
while (!feof($pipes[2])) {
$err .= fread($pipes[2], 8192);
}
fclose($pipes[2]);
if(proc_close($process) != 0){
//something went wrong
$err = "Aspell returned an error: $err";
return false;
}
return true;
}
//opening failed
$err = "Could not run Aspell '".ASPELL_BIN."'";
return false;
}
/**
* Checks a single word for correctness
*
* @returns array of suggestions or true on correct spelling
* @author Andreas Gohr <andi@splitbrain.org>
*/
function suggest($word){
if($this->runAspell("^$word",$out,$err)){
//parse output
$lines = split("\n",$out);
foreach ($lines as $line){
$line = trim($line);
if(empty($line)) continue; // empty line
if($line[0] == '@') continue; // comment
if($line[0] == '*') return true; // no mistakes made
if($line[0] == '#') return array(); // mistake but no suggestions
if($line[0] == '&'){
$line = preg_replace('/&.*?: /','',$line);
return split(', ',$line);
}
}
}
return array();
}
/**
* Check if a word is mispelled like pspell_check
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function check($word){
if(is_array($this->suggest($word))){
return false;
}else{
return true;
}
}
}
//Setup VIM: ex: et ts=4 enc=utf-8 :
|