2
/////////////////////////////////////////////////////////
4
// include/spellcheck.inc
6
// (C)Copyright 2003 Ryo Chijiiwa <Ryo@IlohaMail.org>
8
// This file is part of IlohaMail, and released under GPL.
9
// See COPYING, or http://www.fsf.org/copyleft/gpl.html
11
/////////////////////////////////////////////////////////
12
/********************************************************
13
PURPOSE: spell check functions
14
PRE-CONDITIONS: $SPELL_LANGS, $ASPELL_PATH
15
COMMENTS: requires aspell
16
CREDIT: Inspired by Chris Snyder's spellcheckphp
17
http://chxo.com/scripts/spellcheck.php
18
********************************************************/
20
function splchk_lang_supported($language){
23
if (!empty($DICTIONARIES[$language])) return $language;
27
function splchk_check($message, $language){
30
$file = tempnam("/tmp", "imo_");
31
$fp = fopen($file, "w");
33
$lines = explode("\n", $message);
35
if (is_array($lines)){
36
while ( list($k, $line) = each($lines) ){
37
if (ereg("^>", $line)) $line = ""; //ignore quoted lines
38
if (!ereg("[a-zA-Z ]", $line)) $line = ""; //ignore lines that contain no text
40
if (!empty($line)) $started = true; //we won't write leading empty lines
41
if ($started) fputs($fp, $line."\r\n");
47
//make sure language is supported
48
$lang = splchk_lang_supported($language);
49
if (!$lang) return false;
52
$command = $ASPELL_PATH." -a --language-tag=$lang < $file";
53
$temp = exec($command, $output, $errorno);
59
if (!is_array($output)){
60
echo "Got $errorno $temp";
69
while ( list($key, $line) = each($output) ){
72
$output[$key] = $line_num.":".$line;
73
list($pre,$post) = explode(": ", $line);
74
$pre_a = explode(" ", $pre);
77
$candidates = explode(", ", $post);
78
$words[$line_num.":".$offset]=$candidates;
79
$pos[$line_num.":".$offset]=$word;
81
if (empty($line)) $line_num++;
82
//if (empty($last_line) && empty($line)) $line_num++;
86
$result["words"] = $words;
87
$result["pos"] = $pos;
88
$result["output"] = implode("\n", $output);
89
$result["command"] = $command;
94
function splchk_showform($positions, $words, $str){
95
if (empty($str["correct"])) $str["correct"] = "Correct Spelling"; //13
96
if (empty($str["nochange"])) $str["nochange"] = "No Changes"; //14
97
if (empty($str["ignore"])) $str["ignore"] = "ignore"; //17
98
if (empty($str["delete"])) $str["delete"] = "delete"; //18
99
if (empty($str["formname"])) $str["formname"] = "form[0]";
103
//show list of unknown words and suggestions
104
while ( list($offset, $word)=each($positions) ){
106
echo "<td align=right>$word ( @ $offset): </td>\n<td>\n";
107
echo "<input type=\"hidden\" name=\"words[$count]\" value=\"$word\">\n";
108
echo "<input type=\"hidden\" name=\"offsets[$count]\" value=\"$offset\">\n";
109
echo "<select name=\"suggestions[$count]\" onChange=\"document.".$str["formname"].".correct$count.value=this.value;\">\n";
110
echo "<option value=\"$word\">$word [".$str["ignore"]."]\n";
111
echo "<option value=\"\">[".$str["delete"]."]\n";
112
$a = $words[$offset];
113
while (list($k, $alt_word)=each($a)) echo "<option value=\"$alt_word\">$alt_word\n";
114
echo "</select></td>\n<td>\n";
115
echo "<input type=\"text\" name=\"correct$count\" value=\"$word\" size=20><br>\n";
120
echo '<input type="submit" name="correct_spelling" value="'.$str["correct"].'">';
121
echo '<input type="submit" name="no_changes" value="'.$str["nochange"].'">';
124
function splchk_correct($message, $words, $offsets, $suggestions, $correct){
126
//no errors, return without chagnes
127
if (!is_array($words) || count($words)==0) return $message;
129
//build correction tree, with line number as main key
130
//offset as secondary key
131
while (list($num,$word)=each($words)){
132
$correction = $correct[$num];
133
$correction2 = $suggestions[$num];
134
if (empty($correction)) $correction = $correction2;
135
$correction = stripslashes($correction);
137
if ($word!=$correction){
138
list($line_num, $offset) = explode(":", $offsets[$num]);
140
$corr_a["word"] = $word;
141
$corr_a["correction"] = $correction;
142
$cq[$line_num][$offset] = $corr_a;
144
echo $word." -> ".$correction."<br>\n";
148
//if no corrections, return without changes
149
if (!is_array($cq)) return $message;
151
//chop up message, split leading empty lines from real content
152
$lines_raw = explode("\n", $message);
154
while ( list($k,$line)=each($lines_raw) ){
156
if (!empty($line)) $started = true;
157
if ($started) $lines[] = $line;
158
else $head[] = $line;
161
//process correction tree
162
echo "<!-- Spellchecker debug output\n ";
165
while ( list($line_num, $a)=each($cq) ){
166
$line = chop($lines[$line_num-1]);
167
echo "line: $line_num\n";
168
//handle corrections in line in reverse order so
169
//offsets won't get screwed up by prior corrections
172
while (list($offset, $a2)=each($a)){
174
$correction = $a2["correction"];
176
$before = substr($line, 0, $offset);
177
$error = substr($line, $offset, strlen($word));
178
$after = substr($line, $offset+strlen($word));
179
if (strcmp($error, $word)==0){
180
$line = $before.$correction.$after; //validate error
181
echo "\t$offset\t$word -> $correction\n";
183
echo "\t$offset\t$word -> error: found $error instead of $word at offset\n";
186
echo "old line: \"".$lines[$line_num-1]."\"\n";
187
echo "new line: \"".$line."\"\n";
188
$lines[$line_num-1] = $line;
192
if (is_array($head) && count($head)>0){
193
$head_str = implode("\n", $head)."\n";
195
return $head_str.implode("\n", $lines);
b'\\ No newline at end of file'