| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WebService::DetectLanguage; |
|
2
|
|
|
|
|
|
|
$WebService::DetectLanguage::VERSION = '0.04'; |
|
3
|
2
|
|
|
2
|
|
72859
|
use 5.010; |
|
|
2
|
|
|
|
|
10
|
|
|
4
|
2
|
|
|
2
|
|
509
|
use Moo; |
|
|
2
|
|
|
|
|
10957
|
|
|
|
2
|
|
|
|
|
9
|
|
|
5
|
2
|
|
|
2
|
|
2089
|
use JSON::MaybeXS; |
|
|
2
|
|
|
|
|
6350
|
|
|
|
2
|
|
|
|
|
1273
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has key => ( |
|
8
|
|
|
|
|
|
|
is => 'ro', |
|
9
|
|
|
|
|
|
|
required => 1, |
|
10
|
|
|
|
|
|
|
); |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has ua => ( |
|
13
|
|
|
|
|
|
|
is => 'ro', |
|
14
|
|
|
|
|
|
|
default => sub { |
|
15
|
|
|
|
|
|
|
require HTTP::Tiny; |
|
16
|
|
|
|
|
|
|
require IO::Socket::SSL; |
|
17
|
|
|
|
|
|
|
return HTTP::Tiny->new; |
|
18
|
|
|
|
|
|
|
}, |
|
19
|
|
|
|
|
|
|
); |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has base_url => ( |
|
22
|
|
|
|
|
|
|
is => 'ro', |
|
23
|
|
|
|
|
|
|
default => sub { 'https://ws.detectlanguage.com/0.2' }, |
|
24
|
|
|
|
|
|
|
); |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub _get |
|
27
|
|
|
|
|
|
|
{ |
|
28
|
2
|
|
|
2
|
|
8
|
my ($self, $relurl) = @_; |
|
29
|
2
|
|
|
|
|
11
|
my $url = $self->base_url.'/'.$relurl; |
|
30
|
2
|
|
|
|
|
12
|
my $headers = { "Authorization" => "Bearer ".$self->key }; |
|
31
|
2
|
|
|
|
|
14
|
my $response = $self->ua->get($url, { headers => $headers }); |
|
32
|
|
|
|
|
|
|
|
|
33
|
2
|
50
|
|
|
|
29
|
if (not $response->{success}) { |
|
34
|
0
|
|
|
|
|
0
|
die "failed $response->{status} $response->{reason}\n"; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
2
|
|
|
|
|
158
|
return decode_json($response->{content}); |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub detect |
|
41
|
|
|
|
|
|
|
{ |
|
42
|
1
|
|
|
1
|
1
|
2085
|
my ($self, $string) = @_; |
|
43
|
|
|
|
|
|
|
|
|
44
|
1
|
|
|
|
|
5
|
my ($result) = $self->multi_detect($string); |
|
45
|
1
|
|
|
|
|
2
|
return @{ $result }; |
|
|
1
|
|
|
|
|
4
|
|
|
46
|
|
|
|
|
|
|
} |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub multi_detect |
|
49
|
|
|
|
|
|
|
{ |
|
50
|
2
|
|
|
2
|
1
|
543
|
my ($self, @strings) = @_; |
|
51
|
2
|
|
|
|
|
8
|
my $url = $self->base_url."/detect"; |
|
52
|
2
|
|
|
|
|
8
|
my $headers = { "Authorization" => "Bearer ".$self->key }; |
|
53
|
2
|
|
|
|
|
6
|
my $form_data = [ 'q[]' => \@strings ]; |
|
54
|
2
|
|
|
|
|
9
|
my $response = $self->ua->post_form($url, $form_data, { headers => $headers }); |
|
55
|
|
|
|
|
|
|
|
|
56
|
2
|
50
|
|
|
|
58
|
if (not $response->{success}) { |
|
57
|
0
|
|
|
|
|
0
|
die "failed $response->{status} $response->{reason}\n"; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
2
|
|
|
|
|
502
|
require WebService::DetectLanguage::Result; |
|
61
|
2
|
|
|
|
|
7
|
require WebService::DetectLanguage::Language; |
|
62
|
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
22
|
my $data = decode_json($response->{content}); |
|
64
|
2
|
|
|
|
|
4
|
my @results; |
|
65
|
|
|
|
|
|
|
|
|
66
|
2
|
|
|
|
|
2
|
foreach my $result_set (@{ $data->{data}{detections} }) { |
|
|
2
|
|
|
|
|
6
|
|
|
67
|
3
|
|
|
|
|
5
|
my $set = []; |
|
68
|
3
|
|
|
|
|
4
|
foreach my $result (@$result_set) { |
|
69
|
|
|
|
|
|
|
my $object = WebService::DetectLanguage::Result->new( |
|
70
|
|
|
|
|
|
|
language => WebService::DetectLanguage::Language->new(code => $result->{language}), |
|
71
|
|
|
|
|
|
|
is_reliable => $result->{isReliable}, |
|
72
|
|
|
|
|
|
|
confidence => $result->{confidence}, |
|
73
|
3
|
|
|
|
|
56
|
); |
|
74
|
3
|
|
|
|
|
1159
|
push(@$set, $object); |
|
75
|
|
|
|
|
|
|
} |
|
76
|
3
|
|
|
|
|
5
|
push(@results, $set); |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
|
|
79
|
2
|
|
|
|
|
11
|
return @results; |
|
80
|
|
|
|
|
|
|
} |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub languages |
|
83
|
|
|
|
|
|
|
{ |
|
84
|
1
|
|
|
1
|
1
|
10
|
my $self = shift; |
|
85
|
1
|
|
|
|
|
4
|
my $result = $self->_get("languages"); |
|
86
|
|
|
|
|
|
|
|
|
87
|
1
|
|
|
|
|
464
|
require WebService::DetectLanguage::Language; |
|
88
|
|
|
|
|
|
|
|
|
89
|
1
|
|
|
|
|
5
|
return map { WebService::DetectLanguage::Language->new($_) } @$result; |
|
|
164
|
|
|
|
|
5271
|
|
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub account_status |
|
93
|
|
|
|
|
|
|
{ |
|
94
|
1
|
|
|
1
|
1
|
3632
|
my $self = shift; |
|
95
|
1
|
|
|
|
|
25
|
my $result = $self->_get("user/status"); |
|
96
|
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
528
|
require WebService::DetectLanguage::AccountStatus; |
|
98
|
|
|
|
|
|
|
|
|
99
|
1
|
|
|
|
|
5
|
return WebService::DetectLanguage::AccountStatus->new( $result ); |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 NAME |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
WebService::DetectLanguage - interface to the language detection API at DetectLanguage.com |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
use WebService::DetectLanguage; |
|
111
|
|
|
|
|
|
|
my $api = WebService::DetectLanguage->new(key => '...'); |
|
112
|
|
|
|
|
|
|
my @possibilities = $api->detect("there can be only one"); |
|
113
|
|
|
|
|
|
|
foreach my $poss (@possibilities) { |
|
114
|
|
|
|
|
|
|
printf "language = %s confidence=%f\n", |
|
115
|
|
|
|
|
|
|
$poss->language->name, |
|
116
|
|
|
|
|
|
|
$poss->confidence; |
|
117
|
|
|
|
|
|
|
} |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
This module is an interface to the DetectLanguage service, |
|
122
|
|
|
|
|
|
|
which provides an API for guessing what natural language is used |
|
123
|
|
|
|
|
|
|
in a sample of text. |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
This is very much a first cut at an interface, |
|
126
|
|
|
|
|
|
|
so (a) the interface may well change, and |
|
127
|
|
|
|
|
|
|
(b) contributions are welcome. |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
To use the API you must sign up to get an API key, |
|
130
|
|
|
|
|
|
|
at L. |
|
131
|
|
|
|
|
|
|
There is a free level which lets you make 1,000 requests per day, |
|
132
|
|
|
|
|
|
|
and you don't have to provide a card to sign up for the free level. |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 Example Usage |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Let's say you've got a sample of text in a file. |
|
137
|
|
|
|
|
|
|
You might read it into C<$text> using C |
|
138
|
|
|
|
|
|
|
from L. |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
To identify the language, you call the C method: |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
@results = $api->detect($text); |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Each result is an instance of L. |
|
145
|
|
|
|
|
|
|
If there's only one result, |
|
146
|
|
|
|
|
|
|
you should look at the C flag to see whether they're |
|
147
|
|
|
|
|
|
|
confident of the identification |
|
148
|
|
|
|
|
|
|
The more text they're given, the more confident they are, in general. |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
if (@results == 1) { |
|
151
|
|
|
|
|
|
|
$result = $results[0]; |
|
152
|
|
|
|
|
|
|
if ($result->is_reliable) { |
|
153
|
|
|
|
|
|
|
printf "Language is %s!\n", $result->language->name; |
|
154
|
|
|
|
|
|
|
} |
|
155
|
|
|
|
|
|
|
else { |
|
156
|
|
|
|
|
|
|
# Hmm, maybe check with the user? |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
} |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
You might get more than one result though. |
|
161
|
|
|
|
|
|
|
This might happen if your sample contains |
|
162
|
|
|
|
|
|
|
words from more than one language, |
|
163
|
|
|
|
|
|
|
for example. |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
In that case, the C flag can be used to check |
|
166
|
|
|
|
|
|
|
if the first result is reliable enough to go with. |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
if (@results > 1 && $results[0]->is_reliable) { |
|
169
|
|
|
|
|
|
|
# we'll go with that! |
|
170
|
|
|
|
|
|
|
} |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
There will only ever be at most one result with C |
|
173
|
|
|
|
|
|
|
set to a true value. |
|
174
|
|
|
|
|
|
|
If you get multiple results, they're always in decreasing order |
|
175
|
|
|
|
|
|
|
of reliability. |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Each result also includes a confidence value, |
|
178
|
|
|
|
|
|
|
which looks a bit like a percentage, |
|
179
|
|
|
|
|
|
|
but L |
|
180
|
|
|
|
|
|
|
says that it can go higher than 100. |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
foreach my $result (@results) { |
|
183
|
|
|
|
|
|
|
my $language = $result->language; |
|
184
|
|
|
|
|
|
|
printf "language = %s (%s) with confidence %f\n", |
|
185
|
|
|
|
|
|
|
$language->name, |
|
186
|
|
|
|
|
|
|
$language->code, |
|
187
|
|
|
|
|
|
|
$result->confidence; |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head1 METHODS |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 new |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
You must provide the B that you got from C. |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
my $api = WebService::WordsAPI->new( |
|
197
|
|
|
|
|
|
|
key => '...', |
|
198
|
|
|
|
|
|
|
); |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 detect |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
This method takes a UTF-8 text string, |
|
204
|
|
|
|
|
|
|
and returns a list of one or more guesses |
|
205
|
|
|
|
|
|
|
at the language. |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Each guess is a data object which has attributes |
|
208
|
|
|
|
|
|
|
C, C, and C. |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
my $text = "It was a bright cold day in April, ..."; |
|
211
|
|
|
|
|
|
|
my @results = $api->detect($text); |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
foreach my $result (@results) { |
|
214
|
|
|
|
|
|
|
printf "language = %s (%s) confidence = %f reliable = %s\n", |
|
215
|
|
|
|
|
|
|
$result->language->name, |
|
216
|
|
|
|
|
|
|
$result->language->code, |
|
217
|
|
|
|
|
|
|
$result->confidence, |
|
218
|
|
|
|
|
|
|
$result->is_reliable ? 'Yes' : 'No'; |
|
219
|
|
|
|
|
|
|
} |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Look at the L |
|
222
|
|
|
|
|
|
|
to see how to interpret each result. |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head2 multi_detect |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
This takes multiple strings and returns a list of arrayrefs; |
|
228
|
|
|
|
|
|
|
there is one arrayref for each string, returned in the same order as the strings. |
|
229
|
|
|
|
|
|
|
Each arrayref contains one or more language guess, |
|
230
|
|
|
|
|
|
|
as for C above. |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
my @strings = ( |
|
233
|
|
|
|
|
|
|
"All happy families are alike; each unhappy family ... ", |
|
234
|
|
|
|
|
|
|
"This is my favourite book in all the world, though ... ", |
|
235
|
|
|
|
|
|
|
"It is a truth universally acknowledged, that Perl ... ", |
|
236
|
|
|
|
|
|
|
); |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
my @results = $api->multi_detect(@strings); |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
for (my $i = 0; $i < @strings; $i++) { |
|
241
|
|
|
|
|
|
|
print "Text: $strings[$i]\n"; |
|
242
|
|
|
|
|
|
|
my @results = @{ $results[$i] }; |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
# ... as for detect() above |
|
245
|
|
|
|
|
|
|
} |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
=head2 languages |
|
249
|
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
This returns a list of the supported languages: |
|
251
|
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
my @languages = $api->languages; |
|
253
|
|
|
|
|
|
|
|
|
254
|
|
|
|
|
|
|
foreach my $language (@languages) { |
|
255
|
|
|
|
|
|
|
printf "%s: %s\n", |
|
256
|
|
|
|
|
|
|
$language->code, |
|
257
|
|
|
|
|
|
|
$language->name; |
|
258
|
|
|
|
|
|
|
} |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
=head2 account_status |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
This returns a bunch of information about your account: |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
my $status = $api->account_status; |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
printf "plan=%s status=%s requests=%d\n", |
|
267
|
|
|
|
|
|
|
$status->plan, |
|
268
|
|
|
|
|
|
|
$status->status, |
|
269
|
|
|
|
|
|
|
$status->requests; |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
For the full list of attributes, |
|
272
|
|
|
|
|
|
|
either look at the API documentation, |
|
273
|
|
|
|
|
|
|
or L. |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
L is the home page for the service; |
|
278
|
|
|
|
|
|
|
documentation for the API can be found at L. |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=head1 REPOSITORY |
|
281
|
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
L |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
=head1 AUTHOR |
|
285
|
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
Neil Bowers Eneilb@cpan.orgE |
|
287
|
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
|
289
|
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
This software is copyright (c) 2019 by Neil Bowers . |
|
291
|
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
293
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
294
|
|
|
|
|
|
|
|