Show ispell.pl syntax highlighted
#!/usr/bin/perl -w
use strict;
use Text::Aspell;
my $speller = new Text::Aspell;
$speller->set_option("lang", "en_US");
$speller->set_option("sug-mode", "fast");
my @nearmisses;
my $word;
my $test;
my @fixes = ();
my ($v, $s, $e);
my $line = 0;
while(<STDIN>) {
$v = $_;
chomp $v;
@fixes = ();
# Find the mistakes
while(scalar($v =~ m/\b([-a-z]+)\b/ig)) {
$word = $1;
$s = pos($v) - length($word);
$e = pos($v) - 1;
if(!$speller->check($word)) {
@nearmisses = $speller->suggest($word);
push @fixes, { a => $s, b => $e, word => $word, corrections => [ @nearmisses ] };
}
}
# Create the HTML correction selection stuff
@fixes = sort { $b->{a} <=> $a->{a} } @fixes;
my $html = $v;
for my $fix (@fixes)
{
my $box;
my $cname = "c_${line}_$fix->{a}_$fix->{b}";
$box = qq(<select name="$cname">\n);
$box .= qq(\t<option value="$fix->{word}">$fix->{word}</option>\n);
for my $c (@{$fix->{corrections}}) {
$box .= qq(\t<option value="$c">$c</option>\n);
}
$box .= qq(</select>\n);
$html = substr($html, 0, $fix->{a}) . $box . substr($html, $fix->{b}+1,length($html) - $fix->{b});
}
print "HTML Correction code:\n$html\n";
$line++;
# Fix the line from R to L (using corrections[0] as the "choice"
@fixes = sort { $b->{a} <=> $a->{a} } @fixes;
my $text = $v;
for my $fix (@fixes)
{
if(ref($fix->{corrections}) eq "ARRAY") {
$text = substr($text, 0, $fix->{a}) . $fix->{corrections}[0] . substr($text, $fix->{b}+1,length($text) - $fix->{b});
} else {
$text = substr($text, 0, $fix->{a}) . $fix->{word} . substr($v, $fix->{b}+1,length($text) - $fix->{b});
}
}
print "Correction (assuming selected first alternate): $text\n";
}
See more files for this project here