[LON-CAPA-cvs] cvs: loncom /interface spellcheck.pm
foxr
foxr at source.lon-capa.org
Tue Aug 21 06:31:52 EDT 2012
foxr Tue Aug 21 10:31:52 2012 EDT
Added files:
/loncom/interface spellcheck.pm
Log:
Ajax helper for jquery-spellcheck.
Index: loncom/interface/spellcheck.pm
+++ loncom/interface/spellcheck.pm
# The LearningOnline Network
# Printout
#
# $Id: spellcheck.pm,v 1.1 2012/08/21 10:31:52 foxr Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# /home/httpd/html/adm/gpl.txt
# http://www.lon-capa.org/
#
#
package Apache::spellcheck;
use strict;
use Text::Aspell;
#------------------------------------------------------
#
# array_to_json - Take a Perl array of strings and convert
# it to JSON representation. Note that
# this could be done with the JSON package
# but in this application context it's just
# too trivial to bother.
# @param values - reference to an array.
#
# @return string - the JSON for the array.
#
sub array_to_json {
my $array_ref = shift;
my @array = @$array_ref;
# surround each array element in double quotes.
# ..and convert into a comma separated string:
for (my $i = 0; $i < scalar(@array); $i++) {
$array[$i] = '"' . $array[$i] . '"';
}
my $array_guts = join(', ', @array);
return '[' . $array_guts . ']';
}
#--------------------------------------------------------
#
# spell_check - Check the spellings of a set of white-space
# separated words. Output is a JSON array
# of the mis-spelled words.
#
#
# @param words - the words to check.
# @param lang - The language in which to run the spell checker.
#
# @return - The JSON array to print.
#
sub spell_check {
my ($words, $lang) = @_;
my $checker = Text::Aspell->new;
$checker->set_option('lang', $lang);
# Turn the words into an array:
my @word_list = split(/\s+/, $words);
my @mis_spelled;
foreach my $word (@word_list) {
if (!$checker->check($word)) {
push (@mis_spelled $word);
}
}
return &array_to_json(\@mis_spelled);
}
#-------------------------------------------------------
#
# suggest spellings for a mis-spelled word.
#
# @param word - The mis-spelled word.
# @param lang - The language in which to suggest.
#
# @return the JSON to output.
#
sub suggest_spellings {
my ($word, $lang) = @_;
my $checker = Text::Aspell->new;
$checker->set_option('lang', $lang);
@suggestions = $checker->suggest($word);
return &array_to_json(\@suggestions);
}
#--------------------------------------------------------
#
# Handler. We are given some query parameters that tell us
# what to do. Specifically:
# lang = The spellcheck language
# The Data is the text to check with no punctuation.
# we must respond with Json of the miss-spelled words.
# if the data is of the form suggest='word' we must
# return suggested spellings for "word"
# as a Json array.
sub handler {
my $r = shift;
# Figure out the language defaulting to english.
my $language = "en-US";
if ($r->param{'lang'}) {
$language = $r->param{'lang'};
}
# Regardless, response Content type: is application/json:
$h->header_out('Content-Type', 'application/json');
# Whether we are suggesting or spell checking
# depends on which of the suggest or text args are present:
my $data;
if ($h->args{'text'}) {
$data = &spell_check($h->args{'text'}, $language);
} eslif ($h->args{'suggest'}) {
$data = &suggest_spellings($h->args{'suggest'}, $language);
} else {
die "Invalid request";
}
$r->print($data);
}
More information about the LON-CAPA-cvs
mailing list