Annotation of loncom/interface/spellcheck.pm, revision 1.4

1.1       foxr        1: # The LearningOnline Network
                      2: # Printout
                      3: #
1.4     ! foxr        4: # $Id: spellcheck.pm,v 1.3 2012/09/04 10:46:05 foxr Exp $
1.1       foxr        5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: # http://www.lon-capa.org/
                     26: #
                     27: #
                     28: package Apache::spellcheck;
1.2       foxr       29: use CGI qw(:standard);
                     30: use Apache::Constants qw(:common);
1.1       foxr       31: use strict;
                     32: use Text::Aspell;
                     33: 
                     34: #------------------------------------------------------
                     35: #
                     36: # array_to_json - Take a Perl array of strings and convert
                     37: #                 it to JSON representation.  Note that
                     38: #                 this could be done with the JSON package
                     39: #                 but in this application context it's just
                     40: #                 too trivial to bother.
                     41: # @param values - reference to an array.
                     42: #
                     43: # @return string - the JSON for the array.
                     44: #
                     45: sub array_to_json {
                     46:     my $array_ref = shift;
                     47:     my @array     = @$array_ref;
                     48: 
                     49:     # surround each array element in double quotes.
                     50:     # ..and convert into a comma separated string:
                     51: 
                     52:     for (my $i = 0; $i < scalar(@array); $i++) {
                     53: 	$array[$i] = '"' . $array[$i] . '"';
                     54:     }
                     55:     my $array_guts = join(', ', @array);
                     56:     return '[' . $array_guts . ']';
                     57: }
                     58: 
                     59: #--------------------------------------------------------
                     60: #
                     61: # spell_check -  Check the spellings of a set of white-space
                     62: #                separated words.  Output is a JSON array
                     63: #                of the mis-spelled words.
                     64: #
                     65: #
                     66: #  @param words - the words to check.
                     67: #  @param lang  - The language in which to run the spell checker.
                     68: #
                     69: #  @return - The JSON array to print.
                     70: #
                     71: sub spell_check {
                     72:     my ($words, $lang) = @_;
                     73:     
                     74:     my $checker = Text::Aspell->new;
                     75:     $checker->set_option('lang', $lang);
                     76: 
                     77:     #  Turn the words into an array:
                     78: 
1.3       foxr       79:     my @word_list = split(/\+/, $words);
1.1       foxr       80: 
                     81:     my @mis_spelled;
                     82:     foreach my $word (@word_list) {
                     83: 	if (!$checker->check($word)) {
1.2       foxr       84: 	    push (@mis_spelled, $word);
1.1       foxr       85: 	}
                     86:     }
                     87:     return &array_to_json(\@mis_spelled);
                     88: }
                     89: #-------------------------------------------------------
                     90: #
                     91: # suggest spellings for a mis-spelled word.
                     92: # 
                     93: # @param word - The mis-spelled word.
                     94: # @param lang - The language in which to suggest.
                     95: #
                     96: # @return the JSON to output.
                     97: #
                     98: sub suggest_spellings {
                     99:     my ($word, $lang) = @_;
                    100:     my $checker = Text::Aspell->new;
                    101:     $checker->set_option('lang', $lang);
                    102: 
1.2       foxr      103:     my @suggestions = $checker->suggest($word);
1.1       foxr      104: 
                    105:     return &array_to_json(\@suggestions);
                    106: }
                    107: #--------------------------------------------------------
                    108: #
                    109: #  Handler.  We are given some query parameters that tell us
                    110: #  what to do.  Specifically:
                    111: #    lang = The spellcheck language
                    112: #    The Data is the text to check with no punctuation.
                    113: #    we must respond with Json of the miss-spelled words.
                    114: #    if the data is of the form suggest='word' we must
                    115: #    return suggested spellings for "word"
                    116: #    as a Json array.
                    117: 
                    118: sub handler {
                    119:     my $r = shift;
1.2       foxr      120:     my $raw_params;
                    121: 
                    122:     if ($r->method eq 'POST') {
                    123: 	$raw_params = $r->content();
                    124:     } else {
                    125: 	$raw_params = $r->args();
                    126:     }
1.4     ! foxr      127: 
        !           128: 
1.2       foxr      129:     my $query = CGI->new($raw_params);
1.1       foxr      130: 
                    131:     # Figure out the language defaulting to english.
                    132: 
                    133:     my $language = "en-US";
1.2       foxr      134:     if ($query->param('lang')) {
                    135: 	$language = $query->param('lang');
1.1       foxr      136:     }
                    137:     #  Regardless, response Content type: is application/json:
                    138: 
1.2       foxr      139:     $r->content_type( 'application/json');
                    140:     $r->send_http_header;
1.1       foxr      141: 
                    142:     # Whether we are suggesting or spell checking
                    143:     # depends on which of the suggest or text args are present:
                    144: 
                    145: 
                    146:     my $data;
                    147: 
1.2       foxr      148:     if ($query->param('text')) {
                    149: 	$data =  &spell_check($query->param('text'), $language);
                    150:     } elsif ($query->param('suggest')) {
                    151: 	$data = &suggest_spellings($query->param('suggest'), $language);
1.1       foxr      152:     } else {
1.4     ! foxr      153: 	$data = "[]";		# We seem to get this from time to time.
1.1       foxr      154:     }
                    155:     $r->print($data);
                    156: 
1.2       foxr      157:     return OK;
1.1       foxr      158: }
1.2       foxr      159: 
                    160:     1;
                    161: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>