File Coverage

blib/lib/Lingua/AtD/Scores.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             #
2             # This file is part of Lingua-AtD
3             #
4             # This software is copyright (c) 2011 by David L. Day.
5             #
6             # This is free software; you can redistribute it and/or modify it under
7             # the same terms as the Perl 5 programming language system itself.
8             #
9             package Lingua::AtD::Scores;
10             {
11             $Lingua::AtD::Scores::VERSION = '1.121570';
12             }
13 1     1   2042 use strict;
  1         2  
  1         53  
14 1     1   7 use warnings;
  1         4  
  1         83  
15 1     1   6 use Carp;
  1         3  
  1         96  
16 1     1   746 use XML::LibXML;
  0            
  0            
17             use Lingua::AtD::Metric;
18             use Lingua::AtD::Exceptions;
19             use Class::Std;
20              
21             # ABSTRACT: Encapsulate conversion of XML from /stats call to Metric objects.
22              
23             {
24              
25             # Attributes
26             my %xml_of : ATTR( :init_arg :get );
27             my %server_exception_of : ATTR( :get );
28             my %metric_count_of : ATTR( :get :default<0> );
29             my %metrics_of : ATTR();
30              
31             sub START {
32             my ( $self, $ident, $arg_ref ) = @_;
33             my @atd_metrics = ();
34              
35             my $parser = XML::LibXML->new();
36             my $dom = $parser->load_xml( string => $xml_of{$ident} );
37              
38             # Check for server message. Not sure if stats will do this.
39             if ( $dom->exists('/results/message') ) {
40             $server_exception_of{$ident} = $dom->findvalue('/results/message');
41              
42             # TODO: Implement Exceptions
43             croak $server_exception_of{$ident};
44              
45             # Lingua::AtD::ServiceException->throw(
46             # service_message => $server_exception_of{$ident} );
47             }
48              
49             foreach my $metric_node ( $dom->findnodes('/scores/metric') ) {
50             my $atd_metric = Lingua::AtD::Metric->new(
51             {
52             key => $metric_node->findvalue('key'),
53             type => $metric_node->findvalue('type'),
54             value => $metric_node->findvalue('value'),
55             }
56             );
57             $metric_count_of{$ident} += 1;
58             push( @atd_metrics, $atd_metric );
59             }
60             $metrics_of{$ident} = [@atd_metrics];
61              
62             return;
63             }
64              
65             sub has_server_exception {
66             my $self = shift;
67             return defined( $server_exception_of{ ident($self) } ) ? 1 : 0;
68             }
69              
70             sub has_metrics {
71             my $self = shift;
72             return defined( $metrics_of{ ident($self) } ) ? 1 : 0;
73             }
74              
75             sub get_metrics {
76             my $self = shift;
77             return $self->has_metrics()
78             ? @{ $metrics_of{ ident($self) } }
79             : undef;
80             }
81             }
82              
83             1; # Magic true value required at end of module
84              
85              
86             =pod
87              
88             =head1 NAME
89              
90             Lingua::AtD::Scores - Encapsulate conversion of XML from /stats call to Metric objects.
91              
92             =head1 VERSION
93              
94             version 1.121570
95              
96             =head1 SYNOPSIS
97              
98             use Lingua::AtD;
99              
100             # Create a new service proxy
101             my $atd = Lingua::AtD->new( {
102             host => 'service.afterthedeadline.com',
103             port => 80
104             throttle => 2,
105             });
106              
107             # Run spelling and grammar checks. Returns a Lingua::AtD::Response object.
108             my $doc_check = $atd->check_document('Text to check.');
109             # Loop through reported document errors.
110             foreach my $atd_error ($doc_check->get_errors()) {
111             # Do something with...
112             print "Error string: ", $atd_error->get_string(), "\n";
113             }
114              
115             # Run only grammar checks. Essentially the same as
116             # check_document(), sans spell-check.
117             my $grmr_check = $atd->check_grammar('Text to check.');
118             # Loop through reported document errors.
119             foreach my $atd_error ($grmr_check->get_errors()) {
120             # Do something with...
121             print "Error string: ", $atd_error->get_string(), "\n";
122             }
123              
124             # Get statistics on a document. Returns a Lingua::AtD::Scores object.
125             my $atd_scores = $atd->stats('Text to check.');
126             # Loop through reported document errors.
127             foreach my $atd_metric ($atd_scores->get_metrics()) {
128             # Do something with...
129             print $atd_metric->get_type(), "/", $atd_metric->get_key(),
130             " = ", $atd_metric->get_value(), "\n";
131             }
132              
133             =head1 DESCRIPTION
134              
135             Encapsulates conversion of the XML response from the AtD server into a list of spelling/grammar/style metric objects (L).
136              
137             =head1 METHODS
138              
139             =head2 new
140              
141             # Possible, but not likely
142             my $atd_scores = Lingua::AtD::Scores->new($xml_response);
143             foreach my $atd_metric ($atd_scores->get_metrics()) {
144             # Do something really fun...
145             }
146              
147             Lingua::AtD::Scores objects should only ever be created from a method calls to L. However, if you have saved XML responses from prior calls to AtD, you can use this object to convert those responses into PERL objects. I won't stop you.
148              
149             See the L for typical usage.
150              
151             =head2 has_server_exception
152              
153             Convenience method to see if the AtD server returned an error message.
154              
155             =head2 get_server_exception
156              
157             Exception message from the server.
158              
159             =head2 has_metrics
160              
161             Convenience method to see if the XML response from AtD actually contained any metrics.
162              
163             =head2 get_metric_count
164              
165             Returns the number of linguistic metrics generated by AtD.
166              
167             =head2 get_metrics
168              
169             Returns a list of linguistic metrics as L objects. For details on what metrics are supplied, see the L.
170              
171             =head2 get_xml
172              
173             Returns a string containing the raw XML response from the AtD service call.
174              
175             =head1 SEE ALSO
176              
177             See the L at After the Deadline's website.
178              
179             =head1 AUTHOR
180              
181             David L. Day
182              
183             =head1 COPYRIGHT AND LICENSE
184              
185             This software is copyright (c) 2011 by David L. Day.
186              
187             This is free software; you can redistribute it and/or modify it under
188             the same terms as the Perl 5 programming language system itself.
189              
190             =cut
191              
192              
193             __END__