~ubuntu-branches/ubuntu/maverick/ilohamail/maverick

« back to all changes in this revision

Viewing changes to IlohaMail/include/spellcheck.inc

  • Committer: Bazaar Package Importer
  • Author(s): Joerg Jaspert
  • Date: 2004-02-04 13:44:37 UTC
  • Revision ID: james.westby@ubuntu.com-20040204134437-kz8j3ui2qa7oq8z2
Tags: upstream-0.8.12
ImportĀ upstreamĀ versionĀ 0.8.12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/////////////////////////////////////////////////////////
 
3
//      
 
4
//      include/spellcheck.inc
 
5
//
 
6
//      (C)Copyright 2003 Ryo Chijiiwa <Ryo@IlohaMail.org>
 
7
//
 
8
//      This file is part of IlohaMail, and released under GPL.
 
9
//      See COPYING, or http://www.fsf.org/copyleft/gpl.html
 
10
//
 
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
********************************************************/
 
19
 
 
20
function splchk_lang_supported($language){
 
21
        global $DICTIONARIES;
 
22
                
 
23
        if (!empty($DICTIONARIES[$language])) return $language;
 
24
        else return false;
 
25
}
 
26
 
 
27
function splchk_check($message, $language){
 
28
        global $ASPELL_PATH;
 
29
        
 
30
        $file = tempnam("/tmp", "imo_");
 
31
        $fp = fopen($file, "w");
 
32
        if ($fp){
 
33
                $lines = explode("\n", $message);
 
34
                $started = false;
 
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
 
39
                                $line = chop($line);
 
40
                                if (!empty($line)) $started = true; //we won't write leading empty lines
 
41
                                if ($started) fputs($fp, $line."\r\n");
 
42
                        }
 
43
                }
 
44
                fclose($fp);
 
45
        }
 
46
        
 
47
        //make sure language is supported
 
48
        $lang = splchk_lang_supported($language);
 
49
        if (!$lang) return false;
 
50
        
 
51
        //run command
 
52
        $command = $ASPELL_PATH." -a --language-tag=$lang < $file";
 
53
        $temp = exec($command, $output, $errorno);
 
54
        
 
55
        //remove file
 
56
        unlink($file);
 
57
        
 
58
        //check for error
 
59
        if (!is_array($output)){
 
60
                echo "Got $errorno $temp";
 
61
                return false;
 
62
        }
 
63
        
 
64
        //process
 
65
        $last_line = "";
 
66
        $line_num = 1;
 
67
        $words = array();
 
68
        $pos = array();
 
69
        while ( list($key, $line) = each($output) ){
 
70
                $line = chop($line);
 
71
                if ($line[0]=='&'){
 
72
                        $output[$key] = $line_num.":".$line;
 
73
                        list($pre,$post) = explode(": ", $line);
 
74
                        $pre_a = explode(" ", $pre);
 
75
                        $word = $pre_a[1];
 
76
                        $offset = $pre_a[3];
 
77
                        $candidates = explode(", ", $post);
 
78
                        $words[$line_num.":".$offset]=$candidates;
 
79
                        $pos[$line_num.":".$offset]=$word;
 
80
                }
 
81
                if (empty($line)) $line_num++;
 
82
                //if (empty($last_line) && empty($line)) $line_num++;
 
83
                $last_line = $line;
 
84
        }
 
85
        
 
86
        $result["words"] = $words;
 
87
        $result["pos"] = $pos;
 
88
        $result["output"] = implode("\n", $output);
 
89
        $result["command"] = $command;
 
90
        
 
91
        return $result;
 
92
}
 
93
 
 
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]";
 
100
 
 
101
        echo "<table>\n";
 
102
        $count = 1;
 
103
        //show list of unknown words and suggestions
 
104
        while ( list($offset, $word)=each($positions) ){
 
105
                echo "<tr>";
 
106
                echo "<td align=right>$word ( @ $offset):&nbsp;</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";
 
116
                $count++;
 
117
                echo "</td></tr>\n";
 
118
        }
 
119
        echo "</table>\n";
 
120
        echo '<input type="submit" name="correct_spelling" value="'.$str["correct"].'">';
 
121
        echo '<input type="submit" name="no_changes" value="'.$str["nochange"].'">';
 
122
}
 
123
 
 
124
function splchk_correct($message, $words, $offsets, $suggestions, $correct){
 
125
        
 
126
        //no errors, return without chagnes
 
127
        if (!is_array($words) || count($words)==0) return $message;
 
128
        
 
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);
 
136
                
 
137
                if ($word!=$correction){
 
138
                        list($line_num, $offset) = explode(":", $offsets[$num]);
 
139
                
 
140
                        $corr_a["word"] = $word;
 
141
                        $corr_a["correction"] = $correction;
 
142
                        $cq[$line_num][$offset] = $corr_a;
 
143
                        
 
144
                        echo $word." -> ".$correction."<br>\n";
 
145
                }
 
146
        }
 
147
        
 
148
        //if no corrections, return without changes
 
149
        if (!is_array($cq)) return $message;
 
150
        
 
151
        //chop up message, split leading empty lines from real content
 
152
        $lines_raw = explode("\n", $message);
 
153
        $started = false;
 
154
        while ( list($k,$line)=each($lines_raw) ){
 
155
                $line = chop($line);
 
156
                if (!empty($line)) $started = true;
 
157
                if ($started) $lines[] = $line;
 
158
                else $head[] = $line;
 
159
        }
 
160
 
 
161
        //process correction tree
 
162
        echo "<!-- Spellchecker debug output\n ";
 
163
        ksort($cq);
 
164
        reset($cq);
 
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
 
170
                krsort($a);
 
171
                reset($a);
 
172
                while (list($offset, $a2)=each($a)){
 
173
                        $word = $a2["word"];
 
174
                        $correction = $a2["correction"];
 
175
                        
 
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";
 
182
                        }else{
 
183
                                echo "\t$offset\t$word -> error: found $error instead of $word at offset\n";
 
184
                        }
 
185
                }
 
186
                echo "old line: \"".$lines[$line_num-1]."\"\n";
 
187
                echo "new line: \"".$line."\"\n";
 
188
                $lines[$line_num-1] = $line;
 
189
        }
 
190
        echo "//-->\n";
 
191
        
 
192
        if (is_array($head) && count($head)>0){
 
193
                $head_str = implode("\n", $head)."\n";
 
194
        }
 
195
        return $head_str.implode("\n", $lines);
 
196
}
 
197
 
 
198
?>
 
 
b'\\ No newline at end of file'