#!/usr/bin/perl -w ################################################## ## ## External speller binary for gnuheter.com ## ## Takes a string and finds all missspelled ## words and wraps them in a span tag with ## suggestions in the title attrib and ## style class set to "speling". ## ################################################## use strict; use Lingua::Ispell qw( :all ); use Getopt::Long; $Lingua::Ispell::path = "/usr/bin/aspell"; my ($dictionary, $html, $file, $string); my $ret = GetOptions ('d=s', \$dictionary, "html+", \$html, "file=s", \$file, "s=s", \$string ); unless (defined $dictionary) { $dictionary = "english"; } unless (defined $string) { if (defined $file || -s $file) { open(FILE, "$file"); $string = join('',); close(FILE); } else { print "WHA ($string)?\n"; exit; } } my $html_result .= spell($string, $dictionary); print $html_result; sub spell { my $string = shift() || ""; my $dictionary = shift() || "english"; my $result = "Huh? No input.
\n"; my $result_string = $string; Lingua::Ispell::use_dictionary($dictionary); # Lingua::Ispell::allow_compounds(1); $string =~ s/[\n\r]/ /g; $string =~ s/<[^>]+>//g; ## warn ($string); my %checked; foreach my $r ( spellcheck( $string ) ) { next if (defined $checked{$r->{term}}); $checked{$r->{term}} = 1; if ( $r->{'type'} eq 'ok' ) { # as in the case of 'hello' # $result .= "'$r->{'term'}' was found in the dictionary.\n"; } elsif ( $r->{'type'} eq 'root' ) { # as in the case of 'hacking' $result .= "'$r->{'term'}' can be formed from root '$r->{'root'}'\n"; } elsif ( $r->{'type'} eq 'miss' ) { # as in the case of 'perl' my $desc = "" . $r->{term} . ""; $result_string =~ s/$r->{term}/$desc/g; # $result .= "'$r->{'term'}' was not found in the dictionary;\n"; # $result .= "Near misses: $r->{'misses'}\n"; } elsif ( $r->{'type'} eq 'guess' ) { # as in the case of 'salmoning' $result .= "'$r->{'term'}' was not found in the dictionary;\n"; $result .= "Root/affix Guesses: $r->{'guesses'}\n"; } elsif ( $r->{'type'} eq 'compound' ) { # as in the case of 'fruithammer' $result .= "'$r->{'term'}' is a valid compound word.\n"; } elsif ( $r->{'type'} eq 'none' ) { # as in the case of 'shrdlu' $result .= "No match for term '$r->{'term'}'\n"; } # and numbers are skipped entirely, as in the case of 42. } return "$result_string"; }