script/gram-ga.pl untested

filter: Statementstmt 0.0% 0 / 143 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 222 CC 7 SCAR 33.0
CC
7
Cov
0%
localized_die
56.0
gettext
2.0
BEGIN
2.0
CRAP 27.2
1 #!/usr/local/bin/perl
2  
3 =head1 NAME
4  
5 gram-ga.pl - Command-line interface to An GramadE<oacute>ir
6  
7 =head1 SYNOPSIS
8  
9 B<gram-ga.pl> [I<options>] [I<FILE>...]
10  
11 =head1 DESCRIPTION
12  
13 This script checks the grammar of Irish language input
14 I<FILE>s (or standard input if no files are named). The default behavior
15 is to write a summary of possible errors to standard output.
16  
17 =head1 OPTIONS
18  
19 =over 4
20  
21 =item B<-a>, B<--all>,
22 B<--iomlan>
23  
24 report all errors (i.e. do not use ~/.neamhshuim)
25  
26 =item B<-f>, B<--incode>,
27 B<--ionchod>=I<ENC>
28  
29 specify the character encoding of the text to be checked
30  
31 =item B<-t>, B<--outcode>,
32 B<--aschod>=I<ENC>
33  
34 specify the character encoding for output
35  
36 =item B<--interface>,
37 B<--comheadan>=xx
38  
39 choose the language for error messages
40  
41 =item B<--color>, B<--colour>,
42 B<--dath>=I<COLOR>
43  
44 specify the color to use for highlighting errors
45  
46 =item B<-l>, B<--list>,
47 B<--litriu>
48  
49 write misspelled words to standard output
50  
51 =item B<--moltai>,
52 B<--aspell>
53  
54 suggest corrections for misspellings
55  
56 =item B<-o>, B<--output>,
57 B<--aschur>=I<FILE>
58  
59 write output to I<FILE>
60  
61 =item B<-h>, B<--cabhair>,
62 B<--help>
63  
64 display this help and exit
65  
66 =item B<-v>, B<--leagan>,
67 B<--version>
68  
69 output version information and exit
70  
71  
72 =item B<--api>
73  
74 output a simple XML format for use with other applications
75  
76 =item B<--html>
77  
78 produce HTML output for viewing in a web browser
79  
80 =item B<--no-unigram>
81  
82 do not resolve ambiguous parts of speech by frequency
83  
84 =item B<--xml>
85  
86 write tagged XML stream to standard output, for debugging
87  
88  
89 =back
90  
91 =head1 FILES
92  
93 If there are words you wish to be ignored by the grammar checker
94 (proper names, etc.) you can place them in a file called I<.neamhshuim>
95 in your home directory, one word per line.
96  
97 =head1 REQUIRES
98  
99 Perl 5.8, Lingua::GA::Gramadoir
100  
101 =head1 SEE ALSO
102  
103 =over 4
104  
105 =item *
106 L<http://borel.slu.edu/gramadoir/>
107  
108 =item *
109 L<Lingua::GA::Gramadoir>
110  
111 =item *
112 L<perl(1)>
113  
114 =back
115  
116 =head1 AUTHOR
117  
118 Kevin P. Scannell, E<lt>kscanne@gmail.comE<gt>.
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 Copyright (C) 2004, 2005, 2007 Kevin P. Scannell
123  
124 This is free software; you can redistribute it and/or modify
125 it under the same terms as Perl itself, either Perl version 5.8.2 or,
126 at your option, any later version of Perl 5 you may have available.
127  
128 =cut
129  
130 use strict;
131 use warnings;
132  
133 use Getopt::Long qw(:config gnu_getopt);
134 use Term::ANSIColor;
135 use Lingua::GA::Gramadoir;
136 use Lingua::GA::Gramadoir::Languages;
137 # use open OUT => ':utf8'; # bad bad bad
138 # gram-ga.pl -v
139 # != gram-ga.pl -v > file; cat file
140  
141 my $lh;
142 my $clar;
143 my $VERSION = '0.70';
144  
145 sub gettext
146 {
147 my ( $string, @rest ) = @_;
148  
149 $string =~ s/\\n/\n/g;
150 $string =~ s/\[/~[/g;
151 $string =~ s/\]/~]/g;
152 $string =~ s/\%s/[_1]/;
153 $string =~ s/\%s/[_2]/;
154 return $lh->maketext($string, @rest);
155 }
156  
157 sub localized_die
158 {
159 my ( $signal ) = @_;
160 my $msg;
161  
162 if ( $signal =~ m/^Unknown option: (.*)/ ) {
163 my $arg = $1;
164 chomp $arg;
165 $msg = gettext('unrecognized option %s', '`'.$arg.'\'');
166 }
167 elsif ( $signal =~ m/^Option (.*) requires an argument/ ) {
168 $msg = gettext('option %s requires an argument', '`'.$1.'\'');
169 }
170 elsif ( $signal =~ m/^Option (.*) does not take/ ) {
171 $msg = gettext('option %s does not allow an argument', '`'.$1.'\'');
172 }
173 elsif ( $signal =~ m/^getopt/ ) {
174 $msg = gettext('error parsing command-line options');
175 }
176 elsif ( $signal =~ m/^Invalid attribute name ([^ ]*)/ ) {
177 $msg = gettext('Unable to set output color to %s', $1);
178 }
179 elsif ( $signal =~ m/^gram: maketext (.*)/ ) {
180 $msg = gettext('Language %s is not supported.', $1);
181 }
182 else {
183 die $signal;
184 }
185 my $dieclar = gettext('An Gramadoir'); # can't use global $clar yet
186 my $tryhelp = gettext('Try %s for more information.', '`gram-ga.pl --help\'');
187 print STDERR "$dieclar: $msg\n$tryhelp\n";
188 exit 1;
189 }
190  
191 $lh = Lingua::GA::Gramadoir::Languages->get_handle();
192 $SIG{__DIE__} = 'localized_die';
193 # scalars for global options
194 use vars qw($api $aschur $aspell $comheadan $dath $help $html $iomlan $ionchod $aschod $litriu $version $xml $nounigram);
195 $dath = 'bold red';
196 $ionchod = 'ISO-8859-1';
197 $aschod = 'utf8';
198 eval {
199 local $SIG{__WARN__} = 'localized_die';
200 GetOptions (
201 'all|iomlan|a' => \$iomlan,
202 'api' => \$api,
203 'aspell|moltai' => \$aspell,
204 'list|litriu|l' => \$litriu,
205 'color|colour|dath=s' => \$dath,
206 'incode|ionchod|f=s' => \$ionchod,
207 'outcode|aschod|t=s' => \$aschod,
208 'no-unigram' => \$nounigram,
209 'help|cabhair|h' => \$help,
210 'html' => \$html,
211 'interface|comheadan=s' => \$comheadan,
212 'output|aschur|o=s' => \$aschur,
213 'version|leagan|v' => \$version,
214 'xml' => \$xml,
215 )
216 };
217 die "getopt error" if $@;
218  
219 $xml = 1 if $nounigram;
220 unless ($dath eq "none") {
221 eval { color $dath };
222 }
223  
224 if ($aschur) {
225 unless ($^O eq 'MSWin32') {
226 $aschur =~ s#^~(\w*)#$1 ? (getpwnam($1))[7] : ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>))[7] )#e;
227 }
228 open(OUTSTREAM, ">:encoding($aschod)", $aschur) or
229 warn "Can't open $aschur: $!\n";
230 }
231 else {
232 open(OUTSTREAM, ">&=STDOUT") or
233 warn "Couldn't alias STDOUT: $!\n";
234 binmode OUTSTREAM, ":encoding($aschod)"; # must be after alias
235 }
236  
237 binmode STDERR, ":encoding($aschod)";
238  
239 if ($comheadan) {
240 $lh = Lingua::GA::Gramadoir::Languages->get_handle($comheadan);
241 die "gram: maketext $comheadan" unless $lh;
242 }
243 # $lh->fail_with('failure_handler_auto');
244  
245 # TRANSLATORS: Please leave untranslated, but add an acute accent to the
246 # "o" in "Gramadoir" if available in your character set and encoding.
247 # If you must translate, this is the Irish for (literally) "The Grammarian"
248 $clar = gettext('An Gramadoir');
249  
250 if ($version) {
251 my $vstring = gettext('version %s', $VERSION);
252 my $copyright = 'Copyright (C) 2003-2007 Kevin P. Scannell';
253 my $gpl = gettext('This is free software; see the source for copying conditions. There is NO\\nwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\\nto the extent permitted by law.');
254 if ($html) {
255 $copyright =~ s#(Kevin P. Scannell)#<a href="http://borel.slu.edu/">$1</a><br><br>#;
256 print OUTSTREAM "<p>\n<a href=\"http://borel.slu.edu/gramadoir/\">$clar</a>, $vstring<br>\n$copyright\n<i>$gpl</i></p><hr>\n";
257 }
258 else {
259 print OUTSTREAM "$clar, $vstring\n$copyright\n$gpl\n";
260 }
261 close OUTSTREAM;
262 exit 0;
263 }
264 if ($help) {
265 my @helpmessages = (
266 gettext(
267 'Usage: %s [OPTIONS] [FILES]', "gram-ga.pl"),
268 "",
269 gettext(
270 "Options for end-users:"),
271 # TRANSLATORS: ~/.neamhshuim is an "ignore file" like those with spellcheckers
272 " -a, --all,",
273 gettext(
274 " --iomlan report all errors (i.e. do not use ~/.neamhshuim)"),
275 " -f, --incode,",
276 gettext(
277 " --ionchod=ENC specify the character encoding of the text to be checked"),
278 " -t, --outcode,",
279 gettext(
280 " --aschod=ENC specify the character encoding for output"),
281 " --interface,",
282 gettext(
283 " --comheadan=xx choose the language for error messages"),
284 " --color, --colour,",
285 gettext(
286 " --dath=COLOR specify the color to use for highlighting errors"),
287 " -l, --list,",
288 gettext(
289 " --litriu write misspelled words to standard output"),
290 " --moltai,",
291 gettext(
292 " --aspell suggest corrections for misspellings"),
293 " -o, --output,",
294 gettext(
295 " --aschur=FILE write output to FILE"),
296 " -h, --cabhair,",
297 gettext(
298 " --help display this help and exit"),
299 " -v, --leagan,",
300 gettext(
301 " --version output version information and exit"),
302 "",
303 gettext(
304 "Options for developers:"),
305 gettext(
306 " --api output a simple XML format for use with other applications"),
307 gettext(
308 " --html produce HTML output for viewing in a web browser"),
309 # TRANSLATORS: By default, if there is no rule in the disambiguation module
310 # for selecting the correct part of speech of an ambiguous word, the program
311 # chooses the part of speech with the highest frequency. This is sometimes
312 # called "unigram" tagging. The --no-unigram option turns this behavior off.
313 gettext(
314 " --no-unigram do not resolve ambiguous parts of speech by frequency"),
315 # TRANSLATORS: The grammar checker works by passing the input text
316 # through a sequence of filters which add XML markup indicating
317 # important grammatical information. The --xml option displays the
318 # marked up file as an aid in debugging.
319 gettext(
320 " --xml write tagged XML stream to standard output, for debugging"),
321 "",
322 gettext(
323 "If no file is given, read from standard input."),
324 "",
325 gettext(
326 'Send bug reports to <%s>.','kscanne@gmail.com'),
327 ""
328 );
329  
330 print OUTSTREAM join("\n", @helpmessages);
331 close OUTSTREAM;
332 exit 0;
333 }
334  
335 my $gr = new Lingua::GA::Gramadoir(
336 fix_spelling => $aspell,
337 use_ignore_file => ! $iomlan,
338 unigram_tagging => ! $nounigram,
339 interface_language => $comheadan,
340 input_encoding => $ionchod,
341 );
342  
343 binmode STDIN, ":bytes";
344 unshift(@ARGV, '-') unless @ARGV;
345 while ($ARGV = shift @ARGV) {
346 unless ($ARGV eq "-") {
347 unless ( -e $ARGV ) {
348 warn "$clar: $ARGV: ".gettext('There is no such file.')."\n";
349 next;
350 }
351 if ( -d $ARGV ) {
352 warn "$clar: $ARGV: ".gettext('Is a directory')."\n";
353 next;
354 }
355 unless ( -f $ARGV ) {
356 warn "$ARGV is Not a plain file\n";
357 next;
358 }
359 unless ( -r $ARGV ) {
360 warn "$clar: $ARGV: ".gettext('Permission denied')."\n";
361 next;
362 }
363 unless (open(ARGV, "<:bytes", $ARGV)) {
364 warn "Can't open $ARGV: $!\n";
365 next;
366 }
367 }
368 local $/; # slurp a file at a time
369 $_ = <ARGV>;
370 warn gettext('%s: warning: problem closing %s\n', $clar, $ARGV) unless close ARGV;
371 if ($litriu) {
372 my $missp = $gr->spell_check($_);
373 foreach (@$missp) {
374 print OUTSTREAM "$_\n";
375 }
376 }
377 elsif ($xml) {
378 print OUTSTREAM $gr->xml_stream($_);
379 }
380 elsif ($api) {
381 print OUTSTREAM '<?xml version="1.0" encoding="utf-8" standalone="no"?>'."\n".'<!DOCTYPE matches SYSTEM "http://borel.slu.edu/dtds/api-output.dtd">'."\n<matches>\n";
382 my $apierr = $gr->grammatical_errors($_);
383 foreach (@$apierr) {
384 print OUTSTREAM "$_\n";
385 }
386 print OUTSTREAM "</matches>\n";
387 }
388 else { # vanilla or html
389 my $status = gettext('Currently checking %s', $ARGV);
390 print OUTSTREAM "$status\n" unless ($ARGV eq "-");
391 my $errs = $gr->grammatical_errors($_);
392 foreach my $error (@$errs) {
393 (my $ln, my $msg, my $snt, my $offset, my $len) = $error =~ m/^<error fromy="([0-9]+)".* msg="([^"]+)".* context="([^"]+)" contextoffset="([0-9]+)" errorlength="([0-9]+)"\/>$/;
394 my $errortext = substr($snt,$offset,$len);
395 $ln++; # people count lines from 1
396 my $s = "<br><br>$ln: ".substr($snt,0,$offset);
397 if ($html) {
398 $s .= "<b class=gramadoir>$errortext</b>";
399 }
400 else {
401 if ($dath eq "none") {
402 $s .= $errortext;
403 }
404 else {
405 $s .= colored($errortext,$dath);
406 }
407 }
408 $s .= substr($snt,$offset+$len)."<br>\n$msg\n\n"; # don't add punctuation
409 if (!$html) {
410 $s =~ s/<br>//g;
411 $s =~ s/&quot;/"/g;
412 $s =~ s/&lt;/</g;
413 $s =~ s/&gt;/>/g;
414 $s =~ s/&amp;/&/g;
415 }
416 print OUTSTREAM $s;
417 } # loop over errors
418 } # vanilla/html block
419 } # loop over files
420  
421 close OUTSTREAM;
422 exit 0;