use strict ; use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use HTML::Template; use Data::Dumper; use Unicode::String; use lemma; my $q = new CGI; my $template = "bbc/lemma/wsillafu.tmpl"; my $tmpl = new HTML::Template( filename =>$template,associate => $q ); # Consider using an array to make getting and setting easier. # You could then use array slices to set them ;) my $errorchecker = 0; my $memoryoverloadchecker = 0; my $memorychecker = 0; my $dictionarychecker = 0; my $otherchecker = 0; my $memoryfinder = 0; print $q->header(); if ($q->param()) { my ( $spellingcheck, $error) = lookup($q->param("brawddeg")); # Is this pronounced brahtheg? $tmpl->param( spellingcheck => $spellingcheck ); ($errorchecker,$memoryfinder) = (1,1) if $error == 1; ($memoryoverloadchecker,$memoryfinder) = (1,1) if $error == -1; ($memorychecker, $memoryfinder) = (1,1) if $error == -2; ($dictionarychecker, $memoryfinder) = (1,1) if $error == -3; ($otherchecker,$memoryfinder) = (1,1) if $error == -4; } print $tmpl->output; if( $dictionarychecker ) { print "

Nid yw\'r geiriadur ar-lein ar gael. E-bostiwch meistr y wefan am gymorth \n"; } if( $otherchecker ){ print "

Mae gwall yn y gwirydd. E-bostwich meistr y wefan am gymorth \n"; } if( $memoryoverloadchecker ){ print "

Roedd mwy na 10 gwall yn y testun. Dim ond y 10 gwall cyntaf sydd wedi ei cywiro.
Cywirwch rhain cyn cario ymlaen i gywiro.\n"; } if( $memorychecker ) { print "

Mae'r gwirydd wedi rhedeg allan o gof. E-bostiwch meistr y wefan am gymorth.\n"; } if( $errorchecker ) { print "

Mae rhai gwallau yng nghorff y testun.
Dewiswch air â gynhigir yn y blwch tynnu i lawr i bob gair sydd wedi ei sillafu'n anghywir.
Yna gwagswch y botwm gwirio isod i gael eich testun wedi ei wirio.
\n"; print "
\n"; print "
\n"; } unless( $memoryfinder ) { print "

Nid oedd gwall yn y frawddeg\n"; } sub lookup { # Don't think you want a prototype as you had it ;) $_ = shift; # the @_ is redundant, it's implicit. my ($errors, $str) = lemma::GetCysillSpellingErrors($_, 10); my @fields = split ("([^<]*)", $str); my $error = @fields > 1; my @spellingcheck = (); return (\@spellingcheck, 'odd amount of values') unless @fields % 2; # Remove the last two items my $last_field = pop(@fields); pop(@fields); my ($key,$value); while ( ($key,$value,@fields) = @fields ) { $value =~ s/<[^>]*>//g; my @SuggestionList = split /,/, $value; # Consider Text::CSV here ;) # Saves a bit of space. my @suggestions = map{suggestion=> $_}@SuggestionList; # Create an anonymous hash push @spellingcheck, {text=> $key, thesuggestions=>\@suggestions}; } return ([ @spellingcheck,{text=> $last_field} ], $errors) ; }