Annotation of loncom/interface/spellcheck.pm, revision 1.1
1.1 ! foxr 1: # The LearningOnline Network
! 2: # Printout
! 3: #
! 4: # $Id:$
! 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;
! 29: use strict;
! 30: use Text::Aspell;
! 31:
! 32: #------------------------------------------------------
! 33: #
! 34: # array_to_json - Take a Perl array of strings and convert
! 35: # it to JSON representation. Note that
! 36: # this could be done with the JSON package
! 37: # but in this application context it's just
! 38: # too trivial to bother.
! 39: # @param values - reference to an array.
! 40: #
! 41: # @return string - the JSON for the array.
! 42: #
! 43: sub array_to_json {
! 44: my $array_ref = shift;
! 45: my @array = @$array_ref;
! 46:
! 47: # surround each array element in double quotes.
! 48: # ..and convert into a comma separated string:
! 49:
! 50: for (my $i = 0; $i < scalar(@array); $i++) {
! 51: $array[$i] = '"' . $array[$i] . '"';
! 52: }
! 53: my $array_guts = join(', ', @array);
! 54: return '[' . $array_guts . ']';
! 55: }
! 56:
! 57: #--------------------------------------------------------
! 58: #
! 59: # spell_check - Check the spellings of a set of white-space
! 60: # separated words. Output is a JSON array
! 61: # of the mis-spelled words.
! 62: #
! 63: #
! 64: # @param words - the words to check.
! 65: # @param lang - The language in which to run the spell checker.
! 66: #
! 67: # @return - The JSON array to print.
! 68: #
! 69: sub spell_check {
! 70: my ($words, $lang) = @_;
! 71:
! 72: my $checker = Text::Aspell->new;
! 73: $checker->set_option('lang', $lang);
! 74:
! 75: # Turn the words into an array:
! 76:
! 77: my @word_list = split(/\s+/, $words);
! 78:
! 79: my @mis_spelled;
! 80: foreach my $word (@word_list) {
! 81: if (!$checker->check($word)) {
! 82: push (@mis_spelled $word);
! 83: }
! 84: }
! 85: return &array_to_json(\@mis_spelled);
! 86: }
! 87: #-------------------------------------------------------
! 88: #
! 89: # suggest spellings for a mis-spelled word.
! 90: #
! 91: # @param word - The mis-spelled word.
! 92: # @param lang - The language in which to suggest.
! 93: #
! 94: # @return the JSON to output.
! 95: #
! 96: sub suggest_spellings {
! 97: my ($word, $lang) = @_;
! 98: my $checker = Text::Aspell->new;
! 99: $checker->set_option('lang', $lang);
! 100:
! 101: @suggestions = $checker->suggest($word);
! 102:
! 103: return &array_to_json(\@suggestions);
! 104: }
! 105: #--------------------------------------------------------
! 106: #
! 107: # Handler. We are given some query parameters that tell us
! 108: # what to do. Specifically:
! 109: # lang = The spellcheck language
! 110: # The Data is the text to check with no punctuation.
! 111: # we must respond with Json of the miss-spelled words.
! 112: # if the data is of the form suggest='word' we must
! 113: # return suggested spellings for "word"
! 114: # as a Json array.
! 115:
! 116: sub handler {
! 117: my $r = shift;
! 118:
! 119: # Figure out the language defaulting to english.
! 120:
! 121: my $language = "en-US";
! 122: if ($r->param{'lang'}) {
! 123: $language = $r->param{'lang'};
! 124: }
! 125: # Regardless, response Content type: is application/json:
! 126:
! 127: $h->header_out('Content-Type', 'application/json');
! 128:
! 129: # Whether we are suggesting or spell checking
! 130: # depends on which of the suggest or text args are present:
! 131:
! 132:
! 133: my $data;
! 134:
! 135: if ($h->args{'text'}) {
! 136: $data = &spell_check($h->args{'text'}, $language);
! 137: } eslif ($h->args{'suggest'}) {
! 138: $data = &suggest_spellings($h->args{'suggest'}, $language);
! 139: } else {
! 140: die "Invalid request";
! 141: }
! 142: $r->print($data);
! 143:
! 144: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>