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; |
10
|
|
|
|
|
|
|
{ |
11
|
|
|
|
|
|
|
$Lingua::AtD::VERSION = '1.121570'; |
12
|
|
|
|
|
|
|
} |
13
|
2
|
|
|
2
|
|
2950
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
89
|
|
14
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
77
|
|
15
|
2
|
|
|
2
|
|
13
|
use Carp; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
168
|
|
16
|
2
|
|
|
2
|
|
2292
|
use Class::Std; |
|
2
|
|
|
|
|
23811
|
|
|
2
|
|
|
|
|
14
|
|
17
|
2
|
|
|
2
|
|
2381
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
112857
|
|
|
2
|
|
|
|
|
72
|
|
18
|
2
|
|
|
2
|
|
1601
|
use Lingua::AtD::Results; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
use Lingua::AtD::Scores; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#use Lingua::AtD::Exceptions; |
22
|
|
|
|
|
|
|
use URI; |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# ABSTRACT: Provides an OO wrapper for After the Deadline grammar and spelling service. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
{ |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# Attributes |
29
|
|
|
|
|
|
|
my %api_key_of : |
30
|
|
|
|
|
|
|
ATTR( :init_arg :get :default<'Lingua-AtD'> ); |
31
|
|
|
|
|
|
|
my %throttle_of : |
32
|
|
|
|
|
|
|
ATTR( :init_arg :get :set :default<2> ); |
33
|
|
|
|
|
|
|
my %last_call_of : ATTR( :get :default<0> ); |
34
|
|
|
|
|
|
|
my %service_host_of : |
35
|
|
|
|
|
|
|
ATTR( :init_arg :get :default<'service.afterthedeadline.com'> ); |
36
|
|
|
|
|
|
|
my %service_port_of : |
37
|
|
|
|
|
|
|
ATTR( :init_arg :get :default<80> ); |
38
|
|
|
|
|
|
|
my %service_url_of : ATTR( :get ); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub START { |
41
|
|
|
|
|
|
|
my ( $self, $ident, $arg_ref ) = @_; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Construct the URL |
44
|
|
|
|
|
|
|
$service_url_of{$ident} = |
45
|
|
|
|
|
|
|
'http://' |
46
|
|
|
|
|
|
|
. $service_host_of{$ident} . ':' |
47
|
|
|
|
|
|
|
. $service_port_of{$ident} . '/'; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# Generate API Key |
50
|
|
|
|
|
|
|
my $rand_hex = join "", map { unpack "H*", chr( rand(256) ) } 1 .. 16; |
51
|
|
|
|
|
|
|
$api_key_of{$ident} = "Lingua-AtD-$rand_hex"; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
return; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub _atd : PRIVATE { |
57
|
|
|
|
|
|
|
my ( $self, $verb, $arg_ref ) = @_; |
58
|
|
|
|
|
|
|
my $ident = ident($self); |
59
|
|
|
|
|
|
|
my $url = $service_url_of{$ident} . $verb; |
60
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
61
|
|
|
|
|
|
|
$ua->agent( 'Lingua::AtD/' . $Lingua::AtD::VERSION ); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Throttle Calls. AtD throws a 503 if called too quickly. |
64
|
|
|
|
|
|
|
my $remaining = $throttle_of{$ident} - ( time - $last_call_of{$ident} ); |
65
|
|
|
|
|
|
|
sleep($remaining) if ( $remaining > 0 ); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $response = $ua->post( $url, Content => [ %{$arg_ref} ] ); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$last_call_of{$ident} = time; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
if ( $response->is_error() ) { |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# TODO: Implement Exceptions |
74
|
|
|
|
|
|
|
my $msg = "'$url' responded with '" . $response->status_line . "'."; |
75
|
|
|
|
|
|
|
croak $msg; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# Lingua::AtD::HTTPException->throw( |
78
|
|
|
|
|
|
|
# http_status => $response->status_line, |
79
|
|
|
|
|
|
|
# service_url => $url, |
80
|
|
|
|
|
|
|
# ); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
return $response->content; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub check_document { |
87
|
|
|
|
|
|
|
my ( $self, $text ) = @_; |
88
|
|
|
|
|
|
|
my $ident = ident($self); |
89
|
|
|
|
|
|
|
my $raw_response = |
90
|
|
|
|
|
|
|
$self->_atd( 'checkDocument', |
91
|
|
|
|
|
|
|
{ key => $api_key_of{$ident}, data => $text } ); |
92
|
|
|
|
|
|
|
return Lingua::AtD::Results->new( { xml => $raw_response } ); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub check_grammar { |
96
|
|
|
|
|
|
|
my ( $self, $text ) = @_; |
97
|
|
|
|
|
|
|
my $ident = ident($self); |
98
|
|
|
|
|
|
|
my $raw_response = |
99
|
|
|
|
|
|
|
$self->_atd( 'checkGrammar', |
100
|
|
|
|
|
|
|
{ key => $api_key_of{$ident}, data => $text } ); |
101
|
|
|
|
|
|
|
return Lingua::AtD::Results->new( { xml => $raw_response } ); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub stats { |
105
|
|
|
|
|
|
|
my ( $self, $text ) = @_; |
106
|
|
|
|
|
|
|
my $ident = ident($self); |
107
|
|
|
|
|
|
|
my $raw_response = |
108
|
|
|
|
|
|
|
$self->_atd( 'stats', { key => $api_key_of{$ident}, data => $text } ); |
109
|
|
|
|
|
|
|
return Lingua::AtD::Scores->new( { xml => $raw_response } ); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=pod |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 NAME |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Lingua::AtD - Provides an OO wrapper for After the Deadline grammar and spelling service. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 VERSION |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
version 1.121570 |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 SYNOPSIS |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
use Lingua::AtD; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# Create a new service proxy |
131
|
|
|
|
|
|
|
my $atd = Lingua::AtD->new( { |
132
|
|
|
|
|
|
|
host => 'service.afterthedeadline.com', |
133
|
|
|
|
|
|
|
port => 80, |
134
|
|
|
|
|
|
|
throttle => 2, |
135
|
|
|
|
|
|
|
}); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Run spelling and grammar checks. Returns a Lingua::AtD::Response object. |
138
|
|
|
|
|
|
|
my $doc_check = $atd->check_document('Text to check.'); |
139
|
|
|
|
|
|
|
# Loop through reported document errors. |
140
|
|
|
|
|
|
|
foreach my $atd_error ($doc_check->get_errors()) { |
141
|
|
|
|
|
|
|
# Do something with... |
142
|
|
|
|
|
|
|
print "Error string: ", $atd_error->get_string(), "\n"; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# Run only grammar checks. Essentially the same as |
146
|
|
|
|
|
|
|
# check_document(), sans spell-check. |
147
|
|
|
|
|
|
|
my $grmr_check = $atd->check_grammar('Text to check.'); |
148
|
|
|
|
|
|
|
# Loop through reported document errors. |
149
|
|
|
|
|
|
|
foreach my $atd_error ($grmr_check->get_errors()) { |
150
|
|
|
|
|
|
|
# Do something with... |
151
|
|
|
|
|
|
|
print "Error string: ", $atd_error->get_string(), "\n"; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# Get statistics on a document. Returns a Lingua::AtD::Scores object. |
155
|
|
|
|
|
|
|
my $atd_scores = $atd->stats('Text to check.'); |
156
|
|
|
|
|
|
|
# Loop through reported document errors. |
157
|
|
|
|
|
|
|
foreach my $atd_metric ($atd_scores->get_metrics()) { |
158
|
|
|
|
|
|
|
# Do something with... |
159
|
|
|
|
|
|
|
print $atd_metric->get_type(), "/", $atd_metric->get_key(), |
160
|
|
|
|
|
|
|
" = ", $atd_metric->get_value(), "\n"; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 DESCRIPTION |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
Lingua::AtD provides an OO-style interface for After the Deadline's grammar and spell checking services. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 METHODS |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 new |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
This constructor takes four arguments, all optional. The sample below shows the defaults. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
$atd = Lingua::AtD->new({ |
174
|
|
|
|
|
|
|
api_key => 'Lingua-AtD', |
175
|
|
|
|
|
|
|
host => 'service.afterthedeadline.com', |
176
|
|
|
|
|
|
|
port => 80, |
177
|
|
|
|
|
|
|
throttle => 2, |
178
|
|
|
|
|
|
|
}); |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=over 4 |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item api_key |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
API key used to access the service. Defaults to this package's name plus 32 hex digits (i.e. I-7b8391f59fd9fa4246b2c69cd8793b88). See the L for requirements. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item host |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Host for the AtD service. Defaults to the public host: I. AtD's software is open source, and it's entirely possible to download and set up your own private AtD service. See the L for details. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item port |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
Port for the AtD service. Defaults to the standard http port: I<80>. AtD's software is open source, and it's entirely possible to download and set up your own private AtD service. See the L for details. |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=item throttle |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
There's no API documentation stating such, but testing has shown that AtD service throws a 503 error if called too quickly. This specifies the number of seconds to wait between calls. The default is 2 and seems to work fine. If you see 503 errors, consider bumping this up more. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=back |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=head2 get_api_key |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
$atd->get_api_key(); |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Returns the API Key used to access the AtD service. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 get_host |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
$atd->get_host(); |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Returns the host of the AtD service. |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=head2 get_port |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
$atd->get_port(); |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Returns the port of the AtD service. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head2 get_service_url |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
$atd->get_service(); |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Returns a formatted URL for the AtD service. |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 get_throttle |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
$atd->get_throttle(); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
Returns the number of seconds that must pass between calls to the AtD service. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head2 set_throttle |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
$atd->set_throttle(3); |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
Sets the number of seconds that must pass between calls to the AtD service. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head2 check_document |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
$atd_results = $atd->check_document('Some text stringg in badd nneed of prufreding.'); |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Invokes the document check service for some string of text and return a L object. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
From the L: I |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head2 check_grammar |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
$atd_results = $atd->check_grammar('Some text stringg in badd nneed of prufreding.'); |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Invokes the grammar check service for some string of text and return a L object. This differs from I in that it only checks grammar and style, not spelling. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
From the L: I |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
=head2 stats |
253
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
$atd_scores = $atd->stats('Some text stringg in badd nneed of prufreding.'); |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
Invokes the stats service for some string of text and return a L object. This differs from I in that it only checks grammar and style, not spelling. |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
From the L: I |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head1 BUGS |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
No known bugs. |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=head1 IRONY |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
Wouldn't it be kind of funny if I had a ton of spelling/grammar/style errors in my documentation? Yeah, it would. And I bet there are. Shame on me for not running my documentation through my own module. |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=head1 SEE ALSO |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=for :list * L |
271
|
|
|
|
|
|
|
* L |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
See the L at After the Deadline's website. |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
B In the L, there is a fourth service called B. I do not plan to implement this. Each Lingua::AtD::Error supplies an informative URL when one is available. I see no reason to call this independently, but feel free to submit a patch if you find a reason. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head1 AUTHOR |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
David L. Day |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
This software is copyright (c) 2011 by David L. Day. |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
286
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=cut |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
__END__ |