I want to add spell checking to my Perl program. Looks like Text::Aspell should do what I need, but it only offers a function to check single words.
use strict;
use warnings;
use Text::Aspell;
my $input = "This doesn't look too bad. Me&you. with/without. 1..2..3..go!";
my $aspell = Text::Aspell->new();
$aspell->set_option('lang', 'en');
print "$input: ", $aspell->check($input), "\n";
This prints:
This doesn't look too bad. Me&you. with/without. 1..2..3..go!: 0
So clearly it does only take single words, then how do I separate a text into words? A simple split
at white space:
foreach my $word (split /\s/, $input) {
next unless($word =~ /\w/);
print "$word: ", $aspell->check($word), "\n";
}
This gets problems with punctuation marks that don't have white space:
This: 1
doesn't: 1
look: 1
too: 1
bad.: 0
Me&you.: 0
with/without.: 0
1..2..3..go!: 0
I guess I could mention the punctuation characters:
foreach my $word (split qr{[,.;!:\s#"\?&%@\(\)\[\]/\d]}, $input) {
next unless($word =~ /\w/);
print "$word: ", $aspell->check($word), "\n";
}
This gets reasonable output:
This: 1
doesn't: 1
look: 1
too: 1
bad: 1
Me: 1
you: 1
with: 1
without: 1
go: 1
but seems clumsy and I'm wondering if there is an easier (less code for me to write, less brittle) way.
How do I spell check a text?