From 830c78005a02e646e266adb67fbaf697c8acdcf8 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sat, 16 Jun 2012 13:09:31 -0600 Subject: [PATCH] add English spellcheck goodie with suggestions --- dist.ini | 1 + lib/DDG/Goodie/Spell.pm | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 lib/DDG/Goodie/Spell.pm diff --git a/dist.ini b/dist.ini index 6e43518cd..1c38d35f9 100644 --- a/dist.ini +++ b/dist.ini @@ -30,6 +30,7 @@ Convert::Color::Library = 0.03 Math::Round = 0.06 Convert::Morse = 0.05 Net::IDN::Encode = 2.003 +Text::Aspell = 0.09 [Prereqs / TestRequires] Test::More = 0.98 diff --git a/lib/DDG/Goodie/Spell.pm b/lib/DDG/Goodie/Spell.pm new file mode 100644 index 000000000..4cf530d80 --- /dev/null +++ b/lib/DDG/Goodie/Spell.pm @@ -0,0 +1,20 @@ +package DDG::Goodie::Spell; + +use DDG::Goodie; +use Text::Aspell; + +triggers start => "spell", "how to spell", "how do i spell", "spellcheck"; + +my $speller = Text::Aspell->new; +$speller->set_option('lang','en_US'); +$speller->set_option('sug-mode','fast'); + +handle remainder => sub { + return unless /^[\w']+$/; # only accept letters and ' (aspell handles contractions) + my $correct = $speller->check($_) ? "'\u$_' appears to be spelled right!" : "'\u$_' does not appear to be spelled correctly."; + my @suggestions = $speller->suggest($_); + my $sug = @suggestions ? "Suggestions: " . join(', ', @suggestions[0..5]) : "No suggestions."; + return "$correct $sug", html => "$correct
$sug"; +}; + +1;