File Coverage

blib/lib/RePrec/Tools.pm
Criterion Covered Total %
statement 12 64 18.7
branch 0 28 0.0
condition 0 15 0.0
subroutine 4 9 44.4
pod 5 5 100.0
total 21 121 17.3


line stmt bran cond sub pod time code
1             ###*###################### -*- Mode: Perl -*- #########################
2             ##
3             ## File : $RCSfile: Tools.pm,v $
4             ##
5             ## Author : Norbert Gövert
6             ## Created On : Fri Nov 10 13:21:58 2000
7             ## Last Modified : Time-stamp: <2002-04-25 17:18:31 goevert>
8             ##
9             ## Description :
10             ##
11             ## $Id: Tools.pm,v 1.6 2003/06/13 12:29:30 goevert Exp $
12             ##
13             ######################################################################
14              
15              
16 1     1   10 use strict;
  1         3  
  1         93  
17              
18              
19             =pod #---------------------------------------------------------------#
20              
21             =head1 NAME
22              
23             RePrec::Tools - Collection of tools for RePrec(3) libraries
24              
25             =head1 SYNOPSIS
26              
27             use RePrec::Tools qw(gnuplot system choose fac);
28              
29             =head1 DESCRIPTION
30              
31             Functions shared between the various RePrec(3) libraries.
32              
33             =head1 FUNCTIONS
34              
35             =over
36              
37             =cut #---------------------------------------------------------------#
38              
39             package RePrec::Tools;
40              
41              
42 1     1   5 use base qw(Exporter);
  1         2  
  1         105  
43              
44              
45 1     1   6 use Carp;
  1         2  
  1         68  
46 1     1   5 use IO::File;
  1         2  
  1         1348  
47              
48              
49             our $VERSION;
50             '$Name: release_0_32 $ 0_0' =~ /(\d+)[-_](\d+)/; $VERSION = sprintf '%d.%03d', $1, $2;
51              
52             our @EXPORT_OK = qw( gnuplot
53             system
54             choose
55             fac
56             write_rpdata
57             );
58              
59              
60             ## public ############################################################
61              
62             =cut #---------------------------------------------------------------#
63              
64             =item gnuplot($rpdata, $average, $gnuplot)
65              
66             plot curve with gnuplot(1). $rpdata and $average are the data for the
67             curves to be displayed. $gnuplot is a hash reference where
68             configuration options for gnuplot can be set. The default settings
69             are:
70              
71             style => 'lines'
72             title => 'Recall-Precision'
73             ylabel => 'Precision'
74             xlabel => 'Recall'
75             output => '/tmp/RP'
76             binary => 'gnuplot'
77              
78             The I parameter gives a prefix name used for files created
79             during the plotting. By default the following files are created:
80             F (holds the data for the curves), F
81             (holds the average precision), and F (holds the gnuplot
82             config file).
83              
84             The I parameter gives the name of the gnuplot binary. The
85             I parameter selects the gnuplot terminal to use (for
86             example: C).
87              
88             =cut #---------------------------------------------------------------#
89              
90             sub gnuplot {
91              
92 0     0 1   my($rpdata, $average, $gnuplot) = @_;
93              
94 0   0       my $file = $gnuplot->{output} || "/tmp/RP";
95 0 0         $file = "/tmp/RP" unless $file =~ s/^\s*([A-z\/.\d_-]+).*$/$1/;
96              
97 0   0       my $style = $gnuplot->{style} || 'lines';
98 0   0       my $title = $gnuplot->{title} || 'Recall-Precision';
99 0   0       my $ylabel = $gnuplot->{ylabel} || 'Precision';
100 0   0       my $xlabel = $gnuplot->{xlabel} || 'Recall';
101              
102 0           my $head = qq{
103             set title "$title"
104             set ylabel "$ylabel"
105             set xlabel "$xlabel"
106             set xrange [0:1]
107             set yrange [0:1]
108             set xtics 0,.5,1
109             set ytics 0,.2,1
110             #set xtics 0,.1,1
111             #set ytics 0,.1,1
112             set data style $style
113             set size square 0.757, 1.0
114             set grid
115             };
116              
117 0           my $plot = "plot '$file.average.dat' title 'Average', '$file.dat' title 'Recall-Precision'\n";
118              
119 0 0 0       if (defined $gnuplot->{terminal} and $gnuplot->{terminal} =~ /postscript/i) {
120 0           my $ext = 'ps';
121 0 0         $ext = 'eps' if $gnuplot->{terminal} =~ /eps/i;
122 0           $head .= qq{
123             set terminal $gnuplot->{terminal}
124             set output "$file.$ext"
125             $plot
126             };
127             } else {
128 0           $head .= $plot . "pause -1 'Hit return to continue... '\n";
129             }
130              
131             # write gnuplot config file
132 0 0         my $GP = IO::File->new("$file.gp", 'w')
133             or croak "Couldn't write open file `$file.gp': $!\n";
134 0           $GP->print($head);
135 0           $GP->close;
136              
137 0           write_rpdata($file, $rpdata, $average);
138              
139             # call gnuplot?!
140 0   0       my $GPbin = $gnuplot->{binary} || 'gnuplot';
141 0           &system($GPbin, "$file.gp");
142             }
143              
144              
145             =pod #---------------------------------------------------------------#
146              
147             =item write_rpdata($file, $rpdata, [$average]);
148              
149             Write the recall precision data to file(s).
150              
151             =cut #---------------------------------------------------------------#
152              
153             sub write_rpdata {
154              
155 0     0 1   my($file, $rpdata, $average) = @_;
156              
157             # write gnuplot data file for curve
158 0 0         my $fh = IO::File->new("$file.dat", 'w')
159             or croak "Couldn't write open file `$file.dat': $!\n";
160 0           foreach (@{$rpdata}) {
  0            
161 0           $fh->print("$_->[0] $_->[1]\n");
162             }
163 0           $fh->close;
164              
165 0 0         return unless defined $average;
166              
167             # write gnuplot data file for average
168 0 0         $fh = IO::File->new("$file.average.dat", 'w')
169             or croak"Couldn't write open file `$file.average.dat': $!\n";
170 0           $fh->print("0 $average\n1 $average\n");
171 0           $fh->close;
172             }
173              
174              
175             =pod #---------------------------------------------------------------#
176              
177             =item $rp->system(@args)
178              
179             forks of a process and executes therein the command given by @args
180             (list of executable's name and arguments). Displays some proper return
181             status interpretations.
182              
183             =cut #---------------------------------------------------------------#
184              
185             sub system {
186              
187 0     0 1   my @args = @_;
188              
189 0           my $rc = 0xffff & system @args;
190              
191 0           printf STDERR "system(%s) returned %#04x: ", "@args", $rc;
192              
193 0 0         if ($rc == 0) {
    0          
    0          
194 0           print STDERR "ran with normal exit\n";
195             } elsif ($rc == 0xff00) {
196 0           print STDERR "command failed: $!\n";
197             } elsif ($rc > 0x80) {
198 0           $rc >>= 8;
199 0           print STDERR "ran with non-zero exit status $rc\n";
200             } else {
201 0           print STDERR "ran with ";
202 0 0         if ($rc & 0x80) {
203 0           $rc &= ~0x80;
204 0           print STDERR "core dump from ";
205             }
206 0           print STDERR "signal $rc\n"
207             }
208 0           my $ok = ($rc != 0);
209 0           print STDERR "ok: $ok\n";
210             }
211              
212              
213             =pod #---------------------------------------------------------------#
214              
215             =item $bc = choose($n, $k)
216              
217             computes the binomial coefficient for $n over $k.
218              
219             =cut #---------------------------------------------------------------#
220              
221             sub choose {
222              
223 0     0 1   my($n, $k) = @_;
224              
225 0 0         die "choose($n, $k) not defined" if $n < $k;
226              
227 0           fac($n) / ( fac($k) * fac($n - $k));
228             }
229              
230              
231             =pod #---------------------------------------------------------------#
232              
233             =item $fac = fac($n)
234              
235             computes faculty of $n.
236              
237             =cut #---------------------------------------------------------------#
238              
239             our @_fac = (1, 1);
240             sub fac {
241              
242 0     0 1   my $n = shift;
243              
244 0 0         die "fac($n) not defined" if $n < 0;
245              
246 0 0         return $_fac[$n] if exists $_fac[$n];
247 0           $_fac[$n] = $n * fac($n - 1);
248             }
249              
250              
251             =pod #---------------------------------------------------------------#
252              
253             =back
254              
255             =head1 BUGS
256              
257             Yes. Please let me know!
258              
259             =head1 SEE ALSO
260              
261             RePrec::Average(3),
262             RePrec(3),
263             perl(1).
264              
265             =head1 AUTHOR
266              
267             Norbert Gövert EFE
268              
269             =cut #---------------------------------------------------------------#
270              
271              
272             1;
273             __END__