1 |
<? |
2 |
|
3 |
class C_AFFIX { |
4 |
var $lang; |
5 |
var $flag; |
6 |
var $sub; |
7 |
var $add; |
8 |
var $regexp; |
9 |
} |
10 |
|
11 |
class C_NORM_FORM { |
12 |
var $word; |
13 |
var $lang; |
14 |
var $flag; |
15 |
} |
16 |
|
17 |
// --------------------------------------------------- |
18 |
// load_affix() |
19 |
// --------------------------------------------------- |
20 |
function load_affix() { |
21 |
global $DEBUG; |
22 |
|
23 |
global $ispellmode; |
24 |
|
25 |
global $affix_file; |
26 |
global $affix_array; |
27 |
global $normalized_array; |
28 |
|
29 |
$affix_array=array(); |
30 |
$normalized_array=array(); |
31 |
|
32 |
$i=0; |
33 |
if ($ispellmode=='db') { |
34 |
$query="SELECT flag,lang,mask,find,repl |
35 |
FROM affix |
36 |
WHERE type='s'"; |
37 |
|
38 |
if($DEBUG) echo "load_affix(): ",$query,"<BR><HR>"; |
39 |
|
40 |
if (!($res=db_query($query))) print_error_local('Query error: '.$query."\n<BR>".db_error()); |
41 |
|
42 |
while ($row=db_fetchrow($res)) { |
43 |
$affix_array[$i] = new C_AFFIX; |
44 |
$affix_array[$i]->flag=trim($row[0]); |
45 |
$affix_array[$i]->lang=trim($row[1]); |
46 |
$affix_array[$i]->regexp=trim($row[2]); |
47 |
$affix_array[$i]->add=trim($row[3]); |
48 |
$affix_array[$i]->sub=trim($row[4]); |
49 |
$i++; |
50 |
} |
51 |
db_freeresult($res); |
52 |
} elseif ($ispellmode=='text') { |
53 |
reset($affix_file); |
54 |
|
55 |
while (list($lang,$file)=each($affix_file)) { |
56 |
$fd=fopen($file,"r"); |
57 |
if (! $fd) { |
58 |
print ("Load affix error: cannot open file $file"); |
59 |
exit_local(1); |
60 |
} |
61 |
|
62 |
$suffixes=0; |
63 |
while (! feof($fd)) { |
64 |
$str=strtolower(trim(fgets($fd,1024))); |
65 |
|
66 |
if (preg_match("/^#|^[\s\t\n\r]*$/",$str)) continue; |
67 |
|
68 |
if (ereg("^suffixes",$str)) { |
69 |
$suffixes=1; |
70 |
continue; |
71 |
} |
72 |
|
73 |
if ($suffixes != 1) continue; |
74 |
|
75 |
if (preg_match("/^flag[\s\t]+\*{0,1}(.):/i",$str,$param)) { |
76 |
$flag=trim($param[1]); |
77 |
continue; |
78 |
} |
79 |
|
80 |
if (preg_match("/^[\s\t]*([^>#]+)>[\s\t]+-([^\,\s\t]+),([^\s\t]+)/",$str,$param)) { |
81 |
$regexp=trim($param[1]); |
82 |
$add_str=trim($param[2]); |
83 |
$sub_str=trim($param[3]); |
84 |
|
85 |
if ($sub_str == '-') $sub_str=''; |
86 |
|
87 |
$regexp=str_replace(" ","",$regexp); |
88 |
$add_str=str_replace(" ","",$add_str); |
89 |
$sub_str=str_replace(" ","",$sub_str); |
90 |
|
91 |
$affix_array[$i] = new C_AFFIX; |
92 |
|
93 |
$affix_array[$i]->flag=$flag; |
94 |
$affix_array[$i]->lang=$lang; |
95 |
$affix_array[$i]->regexp=$regexp; |
96 |
$affix_array[$i]->add=$add_str; |
97 |
$affix_array[$i]->sub=$sub_str; |
98 |
$i++; |
99 |
} elseif (preg_match("/^[\s\t]*([^>#]+)>[\s\t]+([^\s\t\#]+)/",$str,$param)) { |
100 |
$regexp=trim($param[1]); |
101 |
$add_str=''; |
102 |
$sub_str=trim($param[2]); |
103 |
|
104 |
$regexp=str_replace(" ","",$regexp); |
105 |
$sub_str=str_replace(" ","",$sub_str); |
106 |
|
107 |
$affix_array[$i] = new C_AFFIX; |
108 |
|
109 |
$affix_array[$i]->flag=$flag; |
110 |
$affix_array[$i]->lang=$lang; |
111 |
$affix_array[$i]->regexp=$regexp; |
112 |
$affix_array[$i]->add=$add_str; |
113 |
$affix_array[$i]->sub=$sub_str; |
114 |
$i++; |
115 |
} |
116 |
} |
117 |
fclose($fd); |
118 |
} |
119 |
} |
120 |
} |
121 |
|
122 |
// --------------------------------------------------- |
123 |
// normalize_word($word) |
124 |
// --------------------------------------------------- |
125 |
function normalize_word($word) { |
126 |
global $DEBUG; |
127 |
|
128 |
global $affix_array; |
129 |
global $normalized_array; |
130 |
|
131 |
global $final_word; |
132 |
|
133 |
$j=0; |
134 |
|
135 |
for($i=0; $i<count($affix_array); $i++) { |
136 |
$sub=$affix_array[$i]->sub; |
137 |
if (strlen($word)<strlen($sub)) continue; |
138 |
|
139 |
if ($sub != '') { |
140 |
if (strcmp($sub,substr($word,strlen($word)-strlen($sub),strlen($sub)))!=0) continue; |
141 |
$temp_word=eregi_replace("$sub$",$affix_array[$i]->add,$word); |
142 |
} else { |
143 |
$temp_word=$word.$affix_array[$i]->add; |
144 |
} |
145 |
|
146 |
if (eregi($affix_array[$i]->regexp,$temp_word)) { |
147 |
if ($DEBUG) echo "<font color=#a00000>Possible norm form</font> \"$temp_word\" for \"$word\":". |
148 |
" <font color=#a00000>-</font> ".$affix_array[$i]->sub. |
149 |
" <font color=#a00000>+</font> ".$affix_array[$i]->add. |
150 |
" <font color=#a00000>exp</font> ".$affix_array[$i]->regexp. |
151 |
" <font color=#a00000>fl</font> ".$affix_array[$i]->flag. |
152 |
" <font color=#a00000>lng</font> ".$affix_array[$i]->lang. |
153 |
"<br><hr>\n"; |
154 |
|
155 |
$normalized_array["$word"][$j]=new C_NORM_FORM; |
156 |
$normalized_array["$word"][$j]->word=$temp_word; |
157 |
$normalized_array["$word"][$j]->flag=$affix_array[$i]->flag; |
158 |
$normalized_array["$word"][$j]->lang=$affix_array[$i]->lang; |
159 |
$j++; |
160 |
} |
161 |
} |
162 |
|
163 |
$final_word["$word"][0]=$word; |
164 |
} |
165 |
|
166 |
// --------------------------------------------------- |
167 |
// check_words() |
168 |
// --------------------------------------------------- |
169 |
function check_words() { |
170 |
global $DEBUG; |
171 |
|
172 |
global $ispellmode; |
173 |
global $spell_file; |
174 |
global $grep; |
175 |
|
176 |
global $final_word; |
177 |
global $normalized_array; |
178 |
|
179 |
reset ($normalized_array); |
180 |
while (list($word,$value)=each($normalized_array)) { |
181 |
for ($j=0; $j<count($value); $j++) { |
182 |
$norm_word=$normalized_array[$word][$j]->word; |
183 |
$norm_lang=$normalized_array[$word][$j]->lang; |
184 |
$norm_flag=$normalized_array[$word][$j]->flag; |
185 |
|
186 |
if ($ispellmode=='db') { |
187 |
$query="SELECT flag |
188 |
FROM spell |
189 |
WHERE word='$norm_word' |
190 |
AND lang='$norm_lang'"; |
191 |
|
192 |
if($DEBUG) echo "check_words(): ",$query,"<BR><HR>"; |
193 |
|
194 |
if (!($res=db_query($query))) print_error_local('Query error: '.$query."\n<BR>".db_error()); |
195 |
|
196 |
while ($row=db_fetchrow($res)) { |
197 |
$flag=trim($row[0]); |
198 |
if ($flag == '') continue; |
199 |
if (ereg($norm_flag,$flag)) { |
200 |
$final_word[$word][]=$norm_word; |
201 |
if ($DEBUG) echo "check_words(): ","<font color=#0000d0>Norm form found:</font> \"$norm_word\" for \"$word\"<br><hr>\n"; |
202 |
} |
203 |
} |
204 |
db_freeresult($res); |
205 |
} elseif ($ispellmode=='text') { |
206 |
reset ($spell_file); |
207 |
while (list($lang,$lang_file)=each($spell_file)) { |
208 |
if ($lang != $norm_lang) continue; |
209 |
for ($i=0;$i<count($lang_file);$i++) { |
210 |
$file=$lang_file[$i]; |
211 |
$pipe="$grep -i \"^$norm_word/\" $file"; |
212 |
if ($DEBUG) echo "check_words(): ",$pipe."<br><hr>"; |
213 |
|
214 |
$fd=popen($pipe,"r"); |
215 |
|
216 |
if (! $fd) { |
217 |
print "Cannot pipe to $grep<br>\n"; |
218 |
exit_local(1); |
219 |
} |
220 |
|
221 |
while (! feof($fd)) { |
222 |
$str=trim(strtolower(fgets($fd,1024))); |
223 |
if ($str=='') continue; |
224 |
$str_array=explode("/",$str); |
225 |
|
226 |
if (($str_array[0]==$norm_word)&& |
227 |
(ereg($norm_flag,$str_array[1]))) { |
228 |
$final_word[$word][]=$norm_word; |
229 |
if ($DEBUG) echo "check_words(): ","<font color=#0000d0>Norm form found:</font> \"$norm_word\" for \"$word\"<br><hr>\n"; |
230 |
} |
231 |
} // while |
232 |
|
233 |
pclose($fd); |
234 |
} // for |
235 |
} |
236 |
} // if |
237 |
} // for |
238 |
} // while |
239 |
} |
240 |
|
241 |
?> |
242 |
|