File:
[LON-CAPA] /
loncom /
interface /
spellcheck.pm
Revision
1.4:
download - view:
text,
annotated -
select for diffs
Mon Sep 10 09:51:06 2012 UTC (11 years, 11 months ago) by
foxr
Branches:
MAIN
CVS tags:
version_2_12_X,
version_2_11_X,
version_2_11_5_msu,
version_2_11_5,
version_2_11_4_uiuc,
version_2_11_4_msu,
version_2_11_4,
version_2_11_3_uiuc,
version_2_11_3_msu,
version_2_11_3,
version_2_11_2_uiuc,
version_2_11_2_msu,
version_2_11_2_educog,
version_2_11_2,
version_2_11_1,
version_2_11_0_RC3,
version_2_11_0_RC2,
version_2_11_0_RC1,
version_2_11_0,
HEAD
bz838 - add spellchecking to <input type='text'> and <textarea>
1: # The LearningOnline Network
2: # Printout
3: #
4: # $Id: spellcheck.pm,v 1.4 2012/09/10 09:51:06 foxr Exp $
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 CGI qw(:standard);
30: use Apache::Constants qw(:common);
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:
79: my @word_list = split(/\+/, $words);
80:
81: my @mis_spelled;
82: foreach my $word (@word_list) {
83: if (!$checker->check($word)) {
84: push (@mis_spelled, $word);
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:
103: my @suggestions = $checker->suggest($word);
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;
120: my $raw_params;
121:
122: if ($r->method eq 'POST') {
123: $raw_params = $r->content();
124: } else {
125: $raw_params = $r->args();
126: }
127:
128:
129: my $query = CGI->new($raw_params);
130:
131: # Figure out the language defaulting to english.
132:
133: my $language = "en-US";
134: if ($query->param('lang')) {
135: $language = $query->param('lang');
136: }
137: # Regardless, response Content type: is application/json:
138:
139: $r->content_type( 'application/json');
140: $r->send_http_header;
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:
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);
152: } else {
153: $data = "[]"; # We seem to get this from time to time.
154: }
155: $r->print($data);
156:
157: return OK;
158: }
159:
160: 1;
161: __END__
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>