[LON-CAPA-cvs] cvs: loncom /interface loncommon.pm

foxr foxr at source.lon-capa.org
Tue Aug 7 06:52:17 EDT 2012


foxr		Tue Aug  7 10:52:17 2012 EDT

  Modified files:              
    /loncom/interface	loncommon.pm 
  Log:
  BZ 838 - Added aspell enabled check_spelling sub.  Note that if the 
           invocation of aspell fails, the words are assumed to be 
           correct.  This can happen for 2. reasons:
          1. aspell is not installed.
          2. The requested langauge dictionaries are not installed on the
             server.
  
  
  
  
Index: loncom/interface/loncommon.pm
diff -u loncom/interface/loncommon.pm:1.1089 loncom/interface/loncommon.pm:1.1090
--- loncom/interface/loncommon.pm:1.1089	Tue Aug  7 09:25:39 2012
+++ loncom/interface/loncommon.pm	Tue Aug  7 10:52:17 2012
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common routines
 #
-# $Id: loncommon.pm,v 1.1089 2012/08/07 09:25:39 foxr Exp $
+# $Id: loncommon.pm,v 1.1090 2012/08/07 10:52:17 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -3015,6 +3015,45 @@
     untie %thesaurus_db;
     return @Words;
 }
+###############################################################
+#
+#  Spell checking
+#
+
+=pod
+
+=head1 Spell checking
+
+=over 4
+
+=item * &check_spelling($wordlist $language)
+
+Takes a string containing words and feeds it to an external
+spellcheck program via a pipeline. Returns a string containing
+them mis-spelled words.
+
+Parameters:
+
+=over 4
+
+=item - $wordlist
+
+String that will be fed into the spellcheck program.
+
+=item - $language
+
+Language string that specifies the language for which the spell
+check will be performed.
+
+=back
+
+=back
+
+Note: This sub assumes that aspell is installed.
+
+
+=cut
+
 
 =pod
 
@@ -3022,6 +3061,45 @@
 
 =cut
 
+sub check_spelling {
+    my ($wordlist, $language) = @_;
+
+    # Format the command.  If $language is null then
+    # don't request a language  - Note that's dangerous
+    # because there's no assurance the server is running the intended default
+    # language.
+
+    my $langswitch = '';
+    if ($language) {
+	$langswitch = "--lang=$language";
+    }
+
+    my $aspell_command = "aspell -a $language";
+    my $full_command   = "echo $wordlist | $aspell_command";
+    
+    my $ispell_result  = `$full_command`;
+
+    # The result is several lines of text.
+    # the first line will start with @(#).  Other wise
+    # There's an error.  With an error our fallback is to declare 
+    # all the words are correctly spelled (return empty string).
+
+    my @misspellings;
+    my @lines = split(/\n/, $ispell_result);
+    my $heading = shift(@lines);	# header
+    if ($heading =~ /^\@\(#\) /) {
+	foreach my $word   (split(/\s+/, $wordlist)) {
+	    my $spellok = pop(@lines);
+	    if (!($spellok =~ /^\*/)) {
+		push(@misspellings, $word);
+	    }
+	}
+	return join(' ', (@misspellings)); # empty if all words ok.
+    } else {
+	return "";
+    }
+}
+
 # -------------------------------------------------------------- Plaintext name
 =pod
 




More information about the LON-CAPA-cvs mailing list