| 1 |
#!/usr/bin/perl -w |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# (C) 2002-2013 Agustin Martin Domingo <[email protected]> |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 |
|
| 20 |
|
| 21 |
sub usage { |
| 22 |
print "ispellaff2myspell: A program to convert ispell affix tables to myspell format |
| 23 |
(C) 2002-2013 Agustin Martin Domingo <agustin.martin\@hispalinux.es> License: GPL2+ |
| 24 |
|
| 25 |
Usage: |
| 26 |
ispellaff2myspell [options] <affixfile> |
| 27 |
|
| 28 |
Options: |
| 29 |
--affixfile=s Affix file |
| 30 |
--bylocale Use current locale setup for upper/lowercase |
| 31 |
conversion |
| 32 |
--charset=s Use specified charset for upper/lowercase |
| 33 |
conversion (defaults to latin1) |
| 34 |
--debug Print debugging info |
| 35 |
--extraflags Allow some non alphabetic flags |
| 36 |
--lowercase=s Lowercase string |
| 37 |
--myheader=s Header file |
| 38 |
--printcomments Print commented lines in output |
| 39 |
--replacements=s Replacements file |
| 40 |
--split=i Split flags with more that i entries |
| 41 |
--uppercase=s Uppercase string |
| 42 |
--wordlist=s Still unused |
| 43 |
|
| 44 |
Currently allowed valued for charset are: latin1, latin2, latin3 |
| 45 |
|
| 46 |
This script does not create the dict file. Something like |
| 47 |
|
| 48 |
( echo `cat mydict.words+ | wc -l`; cat mydict.words+ ) > mydict.dict |
| 49 |
|
| 50 |
should do the work, with mydict.words+ being the ispell munched wordlist |
| 51 |
|
| 52 |
"; |
| 53 |
exit; |
| 54 |
} |
| 55 |
|
| 56 |
sub debugprint { |
| 57 |
if ( $debug ){ |
| 58 |
print STDERR "@_"; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
sub shipoutflag{ |
| 63 |
my $flag_entries=scalar @flag_array; |
| 64 |
|
| 65 |
if ( $flag_entries != 0 ){ |
| 66 |
if ( $split ){ |
| 67 |
while ( @flag_array ){ |
| 68 |
my @flag_subarray=splice(@flag_array,0,$split); |
| 69 |
my $subflag_entries=scalar @flag_subarray; |
| 70 |
if ( scalar @flag_array ){ |
| 71 |
print "$myaffix $flagname $flagcombine $subflag_entries S\n"; |
| 72 |
} else { |
| 73 |
print "$myaffix $flagname $flagcombine $subflag_entries\n"; |
| 74 |
} |
| 75 |
print join("\n",@flag_subarray); |
| 76 |
print "\n\n"; |
| 77 |
} |
| 78 |
} else { |
| 79 |
print "$myaffix $flagname $flagcombine $flag_entries\n"; |
| 80 |
print join("\n",@flag_array); |
| 81 |
print "\n\n"; |
| 82 |
} |
| 83 |
} |
| 84 |
@flag_array=(); |
| 85 |
$flagname=''; |
| 86 |
$flagcombine=''; |
| 87 |
} |
| 88 |
|
| 89 |
sub mylc{ |
| 90 |
my $inputstring=shift; |
| 91 |
my $outputstring; |
| 92 |
|
| 93 |
if ( $bylocale ){ |
| 94 |
{ |
| 95 |
use locale; |
| 96 |
$outputstring = lc $inputstring; |
| 97 |
} |
| 98 |
} else { |
| 99 |
if ( $charset eq "latin0" ){ |
| 100 |
$lowercase='a-z\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\370\371\372\373\374\375\376\275\250\270'; |
| 101 |
$uppercase='A-Z\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\330\331\332\333\334\335\336\274\246\264'; |
| 102 |
} elsif ( $charset eq "latin1" ){ |
| 103 |
$lowercase='a-z\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\370\371\372\373\374\375\376'; |
| 104 |
$uppercase='A-Z\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\330\331\332\333\334\335\336'; |
| 105 |
} elsif ( $charset eq "latin2" ){ |
| 106 |
$lowercase='a-z\261\263\265\266\271\272\273\274\276\277\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\370\371\372\373\374\375\376'; |
| 107 |
$uppercase='A-Z\241\243\245\246\251\252\253\254\256\257\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\330\331\332\333\334\335\336'; |
| 108 |
} elsif ( $charset eq "latin3" ){ |
| 109 |
$lowercase='a-z\261\266\271\272\273\274\277\340\341\342\344\345\346\347\350\351\352\353\354\355\356\357\361\362\363\364\365\366\367\370\371\372\373\374\375\376'; |
| 110 |
$uppercase='A-Z\241\246\251\252\253\254\257\300\301\302\304\305\306\307\310\311\312\313\314\315\316\317\321\322\323\324\325\326\327\330\331\332\333\334\335\336'; |
| 111 |
# } elsif ( $charset eq "other_charset" ){ |
| 112 |
# die "latin2 still unimplemented"; |
| 113 |
} else { |
| 114 |
if ( not $lowercase and not $uppercase ){ |
| 115 |
die "Unsupported charset [$charset] |
| 116 |
|
| 117 |
use explicitly --lowercase=string and --uppercase=string |
| 118 |
options. Remember that both string must match exactly, but |
| 119 |
case changed. |
| 120 |
"; |
| 121 |
} |
| 122 |
} |
| 123 |
$outputstring=$inputstring; |
| 124 |
eval "\$outputstring=~tr/$uppercase/$lowercase/"; |
| 125 |
} |
| 126 |
return $outputstring; |
| 127 |
} |
| 128 |
|
| 129 |
sub validate_flag (){ |
| 130 |
my $flag = shift; |
| 131 |
if ($flag=~m/[a-zA-Z]+/){ |
| 132 |
return $flag; |
| 133 |
} elsif ( $hasextraflags ){ |
| 134 |
foreach ( keys %theextraflags ){ |
| 135 |
if ($flag =~ m/^$_/){ |
| 136 |
$flag =~ s/^$_//; |
| 137 |
return $flag; |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
return ''; |
| 142 |
} |
| 143 |
|
| 144 |
sub process_replacements{ |
| 145 |
my $file = shift; |
| 146 |
my @replaces = (); |
| 147 |
|
| 148 |
open (REPLACE,"< $file") || |
| 149 |
die "Error: Could not open replacements file: $file\n"; |
| 150 |
while (<REPLACE>){ |
| 151 |
next unless m/^REP[\s\t]*\D.*/; |
| 152 |
next if m/^REP\s+[0-9]+/; |
| 153 |
s/\015\012//; |
| 154 |
s/\015//; |
| 155 |
chomp; |
| 156 |
push @replaces, $_; |
| 157 |
} |
| 158 |
close REPLACE; |
| 159 |
my $number = scalar @replaces; |
| 160 |
print "REP $number\n"; |
| 161 |
foreach ( @replaces ){ |
| 162 |
print $_ . "\n"; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
# ----------------------------------------------------------- |
| 167 |
# Now the progran start, after the functions are defined |
| 168 |
# ----------------------------------------------------------- |
| 169 |
|
| 170 |
use Getopt::Long; |
| 171 |
|
| 172 |
# Initializing option values |
| 173 |
$affixfile = ''; |
| 174 |
$bylocale = ''; |
| 175 |
$charset = ''; |
| 176 |
$debug = ''; |
| 177 |
$lowercase = ''; |
| 178 |
$myheader = ''; |
| 179 |
$printcomments = ''; |
| 180 |
$replacements = ''; |
| 181 |
$split = ''; |
| 182 |
$uppercase = ''; |
| 183 |
$wordlist = ''; |
| 184 |
$hasextraflags = ''; |
| 185 |
@flag_array = (); |
| 186 |
%theextraflags = (); |
| 187 |
# Initializing root values |
| 188 |
$rootremove = "0"; |
| 189 |
$rootname = ''; |
| 190 |
$addtoroot = ''; |
| 191 |
$comment = ''; |
| 192 |
# Initializing flag values |
| 193 |
$flagname = ''; |
| 194 |
$flagcombine = ''; |
| 195 |
$inflags = ''; |
| 196 |
|
| 197 |
GetOptions ('affixfile=s' => \$affixfile, |
| 198 |
'bylocale' => \$bylocale, |
| 199 |
'charset=s' => \$charset, |
| 200 |
'debug' => \$debug, |
| 201 |
'extraflags:s' => sub { |
| 202 |
$hasextraflags = 1; |
| 203 |
shift; |
| 204 |
$theflag = shift; |
| 205 |
$theextraflags{$theflag}++ if $theflag}, |
| 206 |
'lowercase=s' => \$lowercase, |
| 207 |
'myheader=s' => \$myheader, |
| 208 |
'printcomments' => \$printcomments, |
| 209 |
'replacements=s'=> \$replacements, |
| 210 |
'split=i' => \$split, |
| 211 |
'uppercase=s' => \$uppercase, |
| 212 |
'wordlist=s' => \$wordlist) or usage; |
| 213 |
|
| 214 |
if ( not $affixfile ){ |
| 215 |
$affixfile=shift or usage; |
| 216 |
} |
| 217 |
|
| 218 |
if ( $charset and ( $lowercase or $uppercase )){ |
| 219 |
die "Error: charset and lowercase/uppercase options |
| 220 |
are incompatible. Use either charset or lowercase/uppercase options to |
| 221 |
specify the patterns |
| 222 |
" |
| 223 |
} elsif ( not $lowercase and not $uppercase and not $charset ){ |
| 224 |
$charset="latin1"; |
| 225 |
} |
| 226 |
|
| 227 |
if ( scalar(keys %theextraflags) == 0 && $hasextraflags ){ |
| 228 |
$theextraflags{"\\\\"}++; |
| 229 |
} |
| 230 |
|
| 231 |
debugprint "$affixfile $charset"; |
| 232 |
|
| 233 |
open (AFFIXFILE,"< $affixfile") || |
| 234 |
die "Error: Could not open affix file: $affixfile"; |
| 235 |
|
| 236 |
if ( $myheader ){ |
| 237 |
my $myspell_header=`cat $myheader`; |
| 238 |
print $myspell_header . "\n"; |
| 239 |
} |
| 240 |
|
| 241 |
while (<AFFIXFILE>){ |
| 242 |
chomp; |
| 243 |
if (/^\s*\#.*/){ |
| 244 |
debugprint "Ignoring line $.\n"; |
| 245 |
print "$_\n" if $printcomments; |
| 246 |
} elsif (/^\s*$/){ |
| 247 |
debugprint "Ignoring line $.\n"; |
| 248 |
} elsif (/^\s*prefixes/){ |
| 249 |
debugprint "Prefixes starting in line $.\n"; |
| 250 |
$affix="PFX"; |
| 251 |
} elsif (/^\s*suffixes/){ |
| 252 |
debugprint "Suffixes starting in line $.\n"; |
| 253 |
$affix="SFX"; |
| 254 |
} elsif (/^[\s\t]*flag.*/){ |
| 255 |
next if not $affix; # In case we are still in the preamble |
| 256 |
shipoutflag if $inflags; |
| 257 |
$inflags="yes"; |
| 258 |
s/^[\s\t]*flag[\s\t]*//; |
| 259 |
s/[\s\t]*:.*$//; |
| 260 |
debugprint "Found flag $_ in line $.\n"; |
| 261 |
|
| 262 |
if (/\*/){ |
| 263 |
s/[\*\s]//g; |
| 264 |
$flagcombine="Y"; |
| 265 |
debugprint "Flag renamed to $_ with combine=$flagcombine\n"; |
| 266 |
} else { |
| 267 |
$flagcombine="N"; |
| 268 |
} |
| 269 |
|
| 270 |
if ( $flagname = &validate_flag($_) ){ |
| 271 |
$myaffix = $affix; |
| 272 |
} else { |
| 273 |
$myaffix = "\# $affix"; |
| 274 |
$flagname = $_; |
| 275 |
print STDERR "Ignoring invalid flag $flagname in line $.\n"; |
| 276 |
} |
| 277 |
} elsif ( $affix and $inflags ) { |
| 278 |
($rootname,@comments) = split('#',$_); |
| 279 |
$comment = '# ' . join('#',@comments); |
| 280 |
|
| 281 |
$rootname =~ s/\s*//g; |
| 282 |
$rootname = mylc $rootname; |
| 283 |
($rootname,$addtoroot) = split('>',$rootname); |
| 284 |
|
| 285 |
if ( $addtoroot =~ s/^\-//g ){ |
| 286 |
($rootremove,$addtoroot) = split(',',$addtoroot); |
| 287 |
$addtoroot = "0" unless $addtoroot; |
| 288 |
$addtoroot = "0" if ( $addtoroot eq "-"); |
| 289 |
} else { |
| 290 |
$rootremove = "0"; |
| 291 |
} |
| 292 |
$addtoroot =~ s/\\\-/\-/g; # prefix ANTI\- to anti- |
| 293 |
|
| 294 |
if ( $rootname eq '.' && $rootremove ne "0" ){ |
| 295 |
$rootname = $rootremove; |
| 296 |
} |
| 297 |
|
| 298 |
debugprint "$rootname, $addtoroot, $rootremove\n"; |
| 299 |
if ( $printcomments ){ |
| 300 |
$affix_line=sprintf("%s %s %-5s %-11s %-24s %s", |
| 301 |
$myaffix, $flagname, $rootremove, |
| 302 |
$addtoroot, $rootname, $comment); |
| 303 |
} else { |
| 304 |
$affix_line=sprintf("%s %s %-5s %-11s %s", |
| 305 |
$myaffix, $flagname, $rootremove, |
| 306 |
$addtoroot, $rootname); |
| 307 |
} |
| 308 |
$rootremove = "0"; |
| 309 |
$rootname = ''; |
| 310 |
$addtoroot = ''; |
| 311 |
$comment = ''; |
| 312 |
@comments = (); |
| 313 |
push @flag_array,$affix_line; |
| 314 |
debugprint "$affix_line\n"; |
| 315 |
} else { |
| 316 |
# |
| 317 |
} |
| 318 |
} |
| 319 |
shipoutflag; |
| 320 |
|
| 321 |
close AFFIXFILE; |
| 322 |
|
| 323 |
if ( $replacements ){ |
| 324 |
&process_replacements($replacements); |
| 325 |
} |
| 326 |
|
| 327 |
__END__ |
| 328 |
|
| 329 |
=encoding UTF-8 |
| 330 |
|
| 331 |
=head1 NAME |
| 332 |
|
| 333 |
B<ispellaff2myspell> - A program to convert ispell affix tables to myspell format. |
| 334 |
|
| 335 |
=head1 SYNOPSIS |
| 336 |
|
| 337 |
ispellaff2myspell [options] <affixfile> --myheader your_header |
| 338 |
|
| 339 |
Options: |
| 340 |
|
| 341 |
--affixfile=s Affix file |
| 342 |
--bylocale Use current locale setup for upper/lowercase |
| 343 |
conversion |
| 344 |
--charset=s Use specified charset for upper/lowercase |
| 345 |
conversion (defaults to latin1) |
| 346 |
--debug Print debugging info |
| 347 |
--extraflags=s Allow some non alphabetic flags |
| 348 |
--lowercase=s Lowercase string |
| 349 |
--myheader=s Header file |
| 350 |
--printcomments Print commented lines in output |
| 351 |
--replacements=s Replacements file |
| 352 |
--split=i Split flags with more that i entries |
| 353 |
--uppercase=s Uppercase string |
| 354 |
|
| 355 |
=head1 DESCRIPTION |
| 356 |
|
| 357 |
B<ispellaff2myspell> is a script that will convert ispell affix tables |
| 358 |
to myspell format in a more or less successful way. |
| 359 |
|
| 360 |
This script does not create the dict file. Something like |
| 361 |
|
| 362 |
( echo `cat mydict.words+ | wc -l`; cat mydict.words+ ) > mydict.dict |
| 363 |
|
| 364 |
should do the work, with mydict.words+ being the munched wordlist |
| 365 |
|
| 366 |
=head1 OPTIONS |
| 367 |
|
| 368 |
=over 8 |
| 369 |
|
| 370 |
=item B<--affixfile=s> |
| 371 |
|
| 372 |
Affix file. You can put it directly in the command line. |
| 373 |
|
| 374 |
=item B<--bylocale> |
| 375 |
|
| 376 |
Use current locale setup for upper/lowercase conversion. Make sure |
| 377 |
that the selected locale match the dictionary one, or you might get |
| 378 |
into trouble. |
| 379 |
|
| 380 |
=item B<--charset=s> |
| 381 |
|
| 382 |
Use specified charset for upper/lowercase conversion (defaults to latin1). |
| 383 |
Currently allowed values for charset are: latin0, latin1, latin2, latin3. |
| 384 |
|
| 385 |
=item B<--debug> |
| 386 |
|
| 387 |
Print some debugging info. |
| 388 |
|
| 389 |
=item B<--extraflags:s> |
| 390 |
|
| 391 |
Allows some non alphabetic flags. |
| 392 |
|
| 393 |
When invoked with no value the supported flags are currently those |
| 394 |
corresponding to chars represented with the escape char B<\> as |
| 395 |
first char. B<\> will be stripped. |
| 396 |
|
| 397 |
When given with the flag prefix will allow that flag and strip the |
| 398 |
given prefix. Be careful when giving the prefix to properly escape chars, |
| 399 |
e.g. you will need B<-e "\\\\"> or B<-e '\\'> for flags like B<\[> to be stripped to |
| 400 |
B<[>. Otherwise you might even get errors. Use B<-e "^"> to allow all |
| 401 |
flags and pass them unmodified. |
| 402 |
|
| 403 |
You will need a call to -e for each flag type, e.g., |
| 404 |
B<-e "\\\\" -e "~\\\\"> (or B<-e '\\' -e '~\\'>). |
| 405 |
|
| 406 |
When a prefix is explicitly set, the default value (anything starting by B<\>) |
| 407 |
is disabled and you need to enable it explicitly as in previous example. |
| 408 |
|
| 409 |
=item B<--lowercase=s> |
| 410 |
|
| 411 |
Lowercase string. Manually set the string of lowercase chars. This |
| 412 |
requires B<--uppercase> having exactly that string but uppercase. |
| 413 |
|
| 414 |
=item B<--myheader=s> |
| 415 |
|
| 416 |
Header file. The myspell aff header. You need to write it |
| 417 |
manually. This can contain everything you want to be before the affix table |
| 418 |
|
| 419 |
=item B<--printcomments> |
| 420 |
|
| 421 |
Print commented lines in output. |
| 422 |
|
| 423 |
=item B<--replacements=file> |
| 424 |
|
| 425 |
Add a pre-defined replacements table taken from 'file' to the .aff file. |
| 426 |
Will skip lines not beginning with REP, and set the replacements number |
| 427 |
appropriately. |
| 428 |
|
| 429 |
=item B<--split=i> |
| 430 |
|
| 431 |
Split flags with more that i entries. This can be of interest for flags |
| 432 |
having a lot of entries. Will split the flag in chunks containing B<i> |
| 433 |
entries. |
| 434 |
|
| 435 |
=item B<--uppercase=s> |
| 436 |
|
| 437 |
Uppercase string. Manually set the sring of uppercase chars. This |
| 438 |
requires B<--lowercase> having exactly that string but lowercase. |
| 439 |
|
| 440 |
=back |
| 441 |
|
| 442 |
If your encoding is currently unsupported you can send me a separate file |
| 443 |
with the two strings of lower and uppercase chars. Note that they must |
| 444 |
match exactly but case changed. It will look something like |
| 445 |
|
| 446 |
$lowercase='a-zàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ'; |
| 447 |
$uppercase='A-ZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ'; |
| 448 |
|
| 449 |
A safer alternative against accidental recoding is to use octal codes for |
| 450 |
non 7bit chars. Above strings would then look like |
| 451 |
|
| 452 |
$lowercase='a-z\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\370\371\372\373\374\375\376'; |
| 453 |
$uppercase='A-Z\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\330\331\332\333\334\335\336'; |
| 454 |
|
| 455 |
=head1 SEE ALSO |
| 456 |
|
| 457 |
The OpenOffice.org Lingucomponent Project home page |
| 458 |
|
| 459 |
L<http://lingucomponent.openoffice.org/index.html> |
| 460 |
|
| 461 |
and the document |
| 462 |
|
| 463 |
L<http://lingucomponent.openoffice.org/affix.readme> |
| 464 |
|
| 465 |
that provides information about the basics of the myspell affix file format. |
| 466 |
|
| 467 |
You can also take a look at |
| 468 |
|
| 469 |
/usr/share/doc/libmyspell-dev/affix.readme.gz |
| 470 |
/usr/share/doc/libmyspell-dev/README.compoundwords |
| 471 |
/usr/share/doc/libmyspell-dev/README.replacetable |
| 472 |
|
| 473 |
in your Debian system. |
| 474 |
|
| 475 |
=head1 AUTHORS |
| 476 |
|
| 477 |
Agustin Martin <[email protected]> |
| 478 |
|
| 479 |
=cut |