version 1.1, 2012/08/21 10:31:52
|
version 1.3, 2012/09/04 10:46:05
|
Line 26
|
Line 26
|
# |
# |
# |
# |
package Apache::spellcheck; |
package Apache::spellcheck; |
|
use CGI qw(:standard); |
|
use Apache::Constants qw(:common); |
use strict; |
use strict; |
use Text::Aspell; |
use Text::Aspell; |
|
|
Line 74 sub spell_check {
|
Line 76 sub spell_check {
|
|
|
# Turn the words into an array: |
# Turn the words into an array: |
|
|
my @word_list = split(/\s+/, $words); |
my @word_list = split(/\+/, $words); |
|
|
my @mis_spelled; |
my @mis_spelled; |
foreach my $word (@word_list) { |
foreach my $word (@word_list) { |
if (!$checker->check($word)) { |
if (!$checker->check($word)) { |
push (@mis_spelled $word); |
push (@mis_spelled, $word); |
} |
} |
} |
} |
return &array_to_json(\@mis_spelled); |
return &array_to_json(\@mis_spelled); |
Line 98 sub suggest_spellings {
|
Line 100 sub suggest_spellings {
|
my $checker = Text::Aspell->new; |
my $checker = Text::Aspell->new; |
$checker->set_option('lang', $lang); |
$checker->set_option('lang', $lang); |
|
|
@suggestions = $checker->suggest($word); |
my @suggestions = $checker->suggest($word); |
|
|
return &array_to_json(\@suggestions); |
return &array_to_json(\@suggestions); |
} |
} |
Line 115 sub suggest_spellings {
|
Line 117 sub suggest_spellings {
|
|
|
sub handler { |
sub handler { |
my $r = shift; |
my $r = shift; |
|
my $raw_params; |
|
|
|
if ($r->method eq 'POST') { |
|
$raw_params = $r->content(); |
|
} else { |
|
$raw_params = $r->args(); |
|
} |
|
my $query = CGI->new($raw_params); |
|
|
# Figure out the language defaulting to english. |
# Figure out the language defaulting to english. |
|
|
my $language = "en-US"; |
my $language = "en-US"; |
if ($r->param{'lang'}) { |
if ($query->param('lang')) { |
$language = $r->param{'lang'}; |
$language = $query->param('lang'); |
} |
} |
# Regardless, response Content type: is application/json: |
# Regardless, response Content type: is application/json: |
|
|
$h->header_out('Content-Type', 'application/json'); |
$r->content_type( 'application/json'); |
|
$r->send_http_header; |
|
|
# Whether we are suggesting or spell checking |
# Whether we are suggesting or spell checking |
# depends on which of the suggest or text args are present: |
# depends on which of the suggest or text args are present: |
Line 132 sub handler {
|
Line 143 sub handler {
|
|
|
my $data; |
my $data; |
|
|
if ($h->args{'text'}) { |
if ($query->param('text')) { |
$data = &spell_check($h->args{'text'}, $language); |
$data = &spell_check($query->param('text'), $language); |
} eslif ($h->args{'suggest'}) { |
} elsif ($query->param('suggest')) { |
$data = &suggest_spellings($h->args{'suggest'}, $language); |
$data = &suggest_spellings($query->param('suggest'), $language); |
} else { |
} else { |
die "Invalid request"; |
die "Invalid request"; |
} |
} |
$r->print($data); |
$r->print($data); |
|
|
|
return OK; |
} |
} |
|
|
|
1; |
|
__END__ |