script/groo-ga.pl untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod n/a totaltotal 0.0% 0 / 131 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 groo-ga.pl - Check the grammar of Irish language OpenOffice.org documents
6  
7 =head1 SYNOPSIS
8  
9 B<groo-ga.pl> I<filetocheck[.sxw]> I<errorfile[.sxw]>
10  
11 =head1 DESCRIPTION
12  
13 This script checks the grammar of the Irish language OpenOffice.org
14 document given as the first argument, and creates a new document
15 with the errors highlighted and annotated with appropriate messages.
16  
17 =head1 REQUIRES
18  
19 Perl 5.8, Lingua::GA::Gramadoir
20  
21 =head1 SEE ALSO
22  
23 =over 4
24  
25 =item *
26 L<http://borel.slu.edu/gramadoir/>
27  
28 =item *
29 L<Lingua::GA::Gramadoir>
30  
31 =item *
32 L<perl(1)>
33  
34 =back
35  
36 =head1 AUTHOR
37  
38 Kevin P. Scannell, E<lt>kscanne@gmail.comE<gt>.
39  
40 =head1 COPYRIGHT AND LICENSE
41  
42 Copyright (C) 2004 Kevin P. Scannell
43  
44 This is free software; you can redistribute it and/or modify
45 it under the same terms as Perl itself, either Perl version 5.8.2 or,
46 at your option, any later version of Perl 5 you may have available.
47  
48 =cut
49  
50 use strict;
51 use warnings;
52 use utf8;
53  
54 use Archive::Zip qw( :ERROR_CODES);
55 use Lingua::GA::Gramadoir;
56 use Encode qw(decode encode);
57  
58 my $debug = 0;
59  
60 if (scalar @ARGV != 2) {
61 print "Usage: $0 filetocheck[.sxw] errorfile[.sxw]\n";
62 exit 1;
63 }
64  
65 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time;
66 my $datestr = sprintf("%04d-%02d-%02d", $year+1900, $mon+1, $mday);
67  
68 # style definition for preamble
69 # double-wavy red underline
70 my $style = '<style:style style:name="gramadoir" style:family="text"><style:properties style:text-underline="double-wave" style:text-underline-color="#800000"/></style:style>';
71 # wavy green underline
72 # my $style = '<style:style style:name="gramadoir" style:family="text"><style:properties style:text-underline="wave" style:text-underline-color="#008000"/></style:style>';
73 # yellow highlighter
74 # my $style = '<style:style style:name="gramadoir" style:family="text"><style:properties style:text-background-color="#ffff00"/></style:style>';
75  
76  
77 # annotation markup
78 my $ann = "<office:annotation office:author=\"An Gramadóir\" office:create-date=\"$datestr\"><text:p>";
79 my $closeann = '</text:p></office:annotation>';
80  
81  
82 # markup to insert before each error
83 my $markup = '<text:span text:style-name="gramadoir">';
84 my $closemarkup = $markup;
85 $closemarkup =~ s/[ >].*/>/;
86 $closemarkup =~ s/</<\//;
87  
88 my $filetocheck = $ARGV[0];
89 my $errorfile = $ARGV[1];
90 $filetocheck =~ s/$/.sxw/ unless ($filetocheck =~ m/\.sxw$/);
91 $errorfile =~ s/$/.sxw/ unless ($errorfile =~ m/\.sxw$/);
92  
93 my $zip = Archive::Zip->new();
94 die "error reading $filetocheck" unless $zip->read($filetocheck) == AZ_OK;
95 my $xml = $zip->contents('content.xml');
96 $xml =~ s/&quot;/\\"/g;
97 $xml =~ s/&lt;/\\</g;
98 $xml =~ s/&gt;/\\>/g;
99 $xml =~ s/&apos;/'/g;
100 $xml =~ s/&amp;/\\&/g;
101  
102 print STDERR "Unpacked contents.xml, converted char entities...\n" if ($debug);
103  
104 my $gr = new Lingua::GA::Gramadoir(
105 # fix_spelling => 1,
106 interface_language => 'ga',
107 input_encoding => 'utf-8',
108 );
109  
110 print STDERR "Gramadoir object created...\n" if ($debug);
111  
112 my $errs = $gr->grammatical_errors($xml);
113 print STDERR "Grammatical errors found...\n" if ($debug);
114 $xml = decode("utf-8", $xml);
115 my @xmllines = split /\n/, $xml;
116 my $xmlans;
117 my $curr_y = 0;
118 my $curr_x = 0;
119 my ($f_y, $f_x, $t_y, $t_x, $errmsg);
120 foreach (@$errs) {
121 ($f_y, $f_x, $t_y, $t_x, $errmsg) = m/^<error fromy="([0-9]+)" fromx="([0-9]+)" toy="([0-9]+)" tox="([0-9]+)" .+msg="([^"]+)"/;
122 while ($curr_y < $f_y) {
123 $xmlans .= substr($xmllines[$curr_y], $curr_x)."\n";
124 $curr_y++;
125 $curr_x = 0;
126 }
127 $xmlans .= substr($xmllines[$f_y], $curr_x, $f_x - $curr_x);
128 $curr_x = $f_x;
129 my $errorspan='';
130 while ($curr_y < $t_y) {
131 $errorspan .= substr($xmllines[$curr_y], $curr_x)."\n";
132 $curr_y++;
133 $curr_x = 0;
134 }
135 $t_x++; # first char after error
136 $errorspan .= substr($xmllines[$t_y], $curr_x, $t_x - $curr_x);
137 $errorspan =~ s/((\s*<[^>]+>\s*)+)/$closemarkup$1$markup/g;
138 $errorspan =~ s/^/$markup/;
139 $errorspan =~ s/$/$closemarkup/;
140 $curr_x = $t_x;
141 $xmlans .= $errorspan;
142 $xmlans .= $ann;
143 $errmsg =~ s/\//\\"/g;
144 $xmlans .= $errmsg;
145 $xmlans .= $closeann;
146 }
147 print STDERR "All error markup inserted...\n" if ($debug);
148 $xmlans .= substr($xmllines[$curr_y], $t_x);
149 $curr_y++;
150 while ($curr_y <= scalar @xmllines) {
151 $xmlans .= $xmllines[$curr_y]."\n";
152 $curr_y++;
153 }
154 print STDERR "New XML completed...\n" if ($debug);
155  
156 $xmlans = encode("utf-8", $xmlans);
157  
158 $xmlans =~ s/\\&/&amp;/g;
159 $xmlans =~ s/\\"/&quot;/g;
160 $xmlans =~ s/\\</&lt;/g;
161 $xmlans =~ s/\\>/&gt;/g;
162 $xmlans =~ s/'/&apos;/g;
163  
164 print STDERR "New XML converted to utf-8 octets...\n" if ($debug);
165  
166  
167 # insert description of gramadoir style in preamble
168 $xmlans =~ s/(?<=<office:automatic-styles>)/$style/;
169 $xmlans =~ s/(?<=<office:automatic-styles)\/>/>$style<\/office:automatic-styles>/;
170  
171 $zip->contents('content.xml', $xmlans);
172 print STDERR "New XML replaces old in contents.xml...\n" if ($debug);
173 die "could not write to $errorfile" unless ($zip->writeToFileNamed( $errorfile ) == AZ_OK);
174 print STDERR "Zip file written...\n" if ($debug);
175  
176 exit 0;