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::Results; |
10
|
|
|
|
|
|
|
{ |
11
|
|
|
|
|
|
|
$Lingua::AtD::Results::VERSION = '1.121570'; |
12
|
|
|
|
|
|
|
} |
13
|
3
|
|
|
3
|
|
1857
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
131
|
|
14
|
3
|
|
|
3
|
|
19
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
119
|
|
15
|
3
|
|
|
3
|
|
63
|
use Carp; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
257
|
|
16
|
3
|
|
|
3
|
|
2104
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
use Lingua::AtD::Error; |
18
|
|
|
|
|
|
|
use Lingua::AtD::Exceptions; |
19
|
|
|
|
|
|
|
use Class::Std; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# ABSTRACT: Encapsulate conversion of XML from /checkDocument or /checkGrammar call to Error objects. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
{ |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Attributes |
26
|
|
|
|
|
|
|
my %xml_of : ATTR( :init_arg :get ); |
27
|
|
|
|
|
|
|
my %server_exception_of : ATTR( :get ); |
28
|
|
|
|
|
|
|
my %error_count_of : ATTR( :get :default<0> ); |
29
|
|
|
|
|
|
|
my %errors_of : ATTR(); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub START { |
32
|
|
|
|
|
|
|
my ( $self, $ident, $arg_ref ) = @_; |
33
|
|
|
|
|
|
|
my @atd_errors = (); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $parser = XML::LibXML->new(); |
36
|
|
|
|
|
|
|
my $dom = $parser->load_xml( string => $xml_of{$ident} ); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Check for server message. |
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 $error_node ( $dom->findnodes('/results/error') ) { |
50
|
|
|
|
|
|
|
my @options = (); |
51
|
|
|
|
|
|
|
foreach |
52
|
|
|
|
|
|
|
my $option_node ( $error_node->findnodes('./suggestions/option') ) |
53
|
|
|
|
|
|
|
{ |
54
|
|
|
|
|
|
|
push( @options, $option_node->string_value ); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
my $url = |
57
|
|
|
|
|
|
|
( $error_node->exists('url') ) |
58
|
|
|
|
|
|
|
? $error_node->findvalue('url') |
59
|
|
|
|
|
|
|
: undef; |
60
|
|
|
|
|
|
|
my $atd_error = Lingua::AtD::Error->new( |
61
|
|
|
|
|
|
|
{ |
62
|
|
|
|
|
|
|
string => $error_node->findvalue('string'), |
63
|
|
|
|
|
|
|
description => $error_node->findvalue('description'), |
64
|
|
|
|
|
|
|
precontext => $error_node->findvalue('precontext'), |
65
|
|
|
|
|
|
|
suggestions => [@options], |
66
|
|
|
|
|
|
|
type => $error_node->findvalue('type'), |
67
|
|
|
|
|
|
|
url => $url, |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
push( @atd_errors, $atd_error ); |
71
|
|
|
|
|
|
|
$error_count_of{$ident} += 1; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
$errors_of{$ident} = [@atd_errors]; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub has_server_exception { |
79
|
|
|
|
|
|
|
my $self = shift; |
80
|
|
|
|
|
|
|
return defined( $server_exception_of{ ident($self) } ) ? 1 : 0; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub has_errors { |
84
|
|
|
|
|
|
|
my $self = shift; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# return defined( $errors_of{ ident($self) } ) ? 1 : 0; |
87
|
|
|
|
|
|
|
return ( $error_count_of{ ident($self) } > 0 ) ? 1 : 0; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub get_errors { |
91
|
|
|
|
|
|
|
my $self = shift; |
92
|
|
|
|
|
|
|
return $self->has_errors() |
93
|
|
|
|
|
|
|
? @{ $errors_of{ ident($self) } } |
94
|
|
|
|
|
|
|
: undef; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=pod |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 NAME |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
Lingua::AtD::Results - Encapsulate conversion of XML from /checkDocument or /checkGrammar call to Error objects. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 VERSION |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
version 1.121570 |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 SYNOPSIS |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
use Lingua::AtD; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# Create a new service proxy |
117
|
|
|
|
|
|
|
my $atd = Lingua::AtD->new( { |
118
|
|
|
|
|
|
|
host => 'service.afterthedeadline.com', |
119
|
|
|
|
|
|
|
port => 80 |
120
|
|
|
|
|
|
|
throttle => 2, |
121
|
|
|
|
|
|
|
}); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Run spelling and grammar checks. Returns a Lingua::AtD::Response object. |
124
|
|
|
|
|
|
|
my $doc_check = $atd->check_document('Text to check.'); |
125
|
|
|
|
|
|
|
# Loop through reported document errors. |
126
|
|
|
|
|
|
|
foreach my $atd_error ($doc_check->get_errors()) { |
127
|
|
|
|
|
|
|
# Do something with... |
128
|
|
|
|
|
|
|
print "Error string: ", $atd_error->get_string(), "\n"; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Run only grammar checks. Essentially the same as |
132
|
|
|
|
|
|
|
# check_document(), sans spell-check. |
133
|
|
|
|
|
|
|
my $grmr_check = $atd->check_grammar('Text to check.'); |
134
|
|
|
|
|
|
|
# Loop through reported document errors. |
135
|
|
|
|
|
|
|
foreach my $atd_error ($grmr_check->get_errors()) { |
136
|
|
|
|
|
|
|
# Do something with... |
137
|
|
|
|
|
|
|
print "Error string: ", $atd_error->get_string(), "\n"; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Get statistics on a document. Returns a Lingua::AtD::Scores object. |
141
|
|
|
|
|
|
|
my $atd_scores = $atd->stats('Text to check.'); |
142
|
|
|
|
|
|
|
# Loop through reported document errors. |
143
|
|
|
|
|
|
|
foreach my $atd_metric ($atd_scores->get_metrics()) { |
144
|
|
|
|
|
|
|
# Do something with... |
145
|
|
|
|
|
|
|
print $atd_metric->get_type(), "/", $atd_metric->get_key(), |
146
|
|
|
|
|
|
|
" = ", $atd_metric->get_value(), "\n"; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 DESCRIPTION |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Encapsulates conversion of the XML response from the AtD server into a list of spelling/grammar/style error objects (L). |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 METHODS |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 new |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
# Possible, but not likely |
158
|
|
|
|
|
|
|
my $atd_results = Lingua::AtD::Results->new($xml_response); |
159
|
|
|
|
|
|
|
foreach my $atd_error ($atd_results->get_errors()) { |
160
|
|
|
|
|
|
|
# Do something really fun... |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Lingua::AtD::Results objects should only ever be created from 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. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
See the L for typical usage. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 has_server_exception |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Convenience method to see if the AtD server returned an error message. |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 get_server_exception |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
Exception message from the server. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 has_errors |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Convenience method to see if the XML response from AtD actually contained any spelling/grammar/style errors. These are not exceptions (see L). These are expected, and in fact are what you've asked for. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head2 get_error_count |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Returns the number of spelling/grammar/style errors detected. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=head2 get_errors |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
Returns a list of spelling/grammar/style errors as L objects. |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head2 get_xml |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Returns a string containing the raw XML response from the AtD service call. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 SEE ALSO |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
See the L at After the Deadline's website. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 AUTHOR |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
David L. Day |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This software is copyright (c) 2011 by David L. Day. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
204
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=cut |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
__END__ |