line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Lingua::Identify::CLD; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
77075
|
use 5.008; |
|
3
|
|
|
|
|
15
|
|
4
|
3
|
|
|
3
|
|
21
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
96
|
|
5
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
195
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=encoding UTF-8 |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Lingua::Identify::CLD - Interface to Chrome language detection library. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Version 0.10 |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
20
|
|
|
|
|
|
|
|
21
|
3
|
|
|
3
|
|
19
|
use XSLoader; |
|
3
|
|
|
|
|
17
|
|
|
3
|
|
|
|
|
187
|
|
22
|
|
|
|
|
|
|
BEGIN { |
23
|
3
|
|
|
3
|
|
693338
|
XSLoader::load('Lingua::Identify::CLD', $VERSION); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 SYNOPSIS |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
use Lingua::Identify::CLD; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Vanilla identification |
31
|
|
|
|
|
|
|
my $cld = Lingua::Identify::CLD->new(); |
32
|
|
|
|
|
|
|
my $lang = $cld->identify("Text"); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# using TLD hint |
35
|
|
|
|
|
|
|
my $lang = $cld->identify("Text", tld => 'by'); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# obtaining further information |
38
|
|
|
|
|
|
|
my @lang = $cld->identify("Text"); |
39
|
|
|
|
|
|
|
# $lang[0] -> language name |
40
|
|
|
|
|
|
|
# $lang[1] -> language id |
41
|
|
|
|
|
|
|
# $lang[2] -> confidence |
42
|
|
|
|
|
|
|
# $lang[3] -> is_reliable (bool) |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# CLD object can also be created with this option |
45
|
|
|
|
|
|
|
my $bycld = Lingua::Identify::CLD->new(tld => 'by'); |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 METHODS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 new |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Constructs a CLD object. You can pass some global configuration |
52
|
|
|
|
|
|
|
options. Currently supported options are listed bellow: |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=over 4 |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item tld |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
A top level domain (tld) to help on the language identification. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item isPlainText |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
By default is set to true. If you have some HTML/XML markup, set it to |
63
|
|
|
|
|
|
|
false. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=item allowExtendedLanguages |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
Set to true by default, let you control weather extended languages |
68
|
|
|
|
|
|
|
should be checked. Like Klingon or Pig Latin. |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=back |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=cut |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub new { |
75
|
2
|
|
|
2
|
1
|
256402
|
my ($class, %options) = @_; |
76
|
2
|
|
|
|
|
8
|
my $self = {%options}; |
77
|
2
|
|
|
|
|
9
|
return bless $self => $class # amen |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 identify |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Receives a string, returns a language name. Following the text a set |
83
|
|
|
|
|
|
|
of key/value options may be supplied. The supported options are the |
84
|
|
|
|
|
|
|
same as of C. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=cut |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub identify { |
89
|
26
|
|
|
26
|
1
|
16535
|
my ($self, $text, %options) = @_; |
90
|
|
|
|
|
|
|
|
91
|
26
|
|
|
|
|
145
|
my %cfg = ( %$self, %options ); |
92
|
|
|
|
|
|
|
|
93
|
26
|
50
|
|
|
|
76
|
my $tld = exists($cfg{tld}) ? $cfg{tld} : ""; |
94
|
26
|
100
|
|
|
|
56
|
my $plaintext = exists($cfg{isPlainText}) ? $cfg{isPlainText} : 1; |
95
|
26
|
50
|
|
|
|
47
|
my $extended = exists($cfg{allowExtendedLanguages}) ? $cfg{allowExtendedLanguages} : 1; |
96
|
|
|
|
|
|
|
|
97
|
26
|
|
|
|
|
33
|
my $confidence = 0; |
98
|
26
|
|
|
|
|
29
|
my $is_reliable = 0; |
99
|
26
|
|
|
|
|
36
|
my $id = ''; |
100
|
|
|
|
|
|
|
|
101
|
26
|
|
|
|
|
8223
|
my $lang = _identify($text, $tld, $plaintext, $extended, $id, $confidence, $is_reliable); |
102
|
|
|
|
|
|
|
|
103
|
26
|
50
|
|
|
|
289
|
return wantarray ? (uc $lang, $id, $confidence, $is_reliable) : uc $lang; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 AUTHOR |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Alberto Simoes, C<< >> |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 BUGS |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
113
|
|
|
|
|
|
|
L. I will be notified, |
114
|
|
|
|
|
|
|
and then you'll automatically be notified of progress on your bug as I |
115
|
|
|
|
|
|
|
make changes. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 SUPPORT |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
perldoc Lingua::Identify::CLD |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
You can also look for information at: |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over 4 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item * Git repository and ticket tracker |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
L |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
L |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=item * CPAN Ratings |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
L |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item * Search CPAN |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
L |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=back |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Chrome team for making the code available. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Jean VĂ©ronis for pushing me to do this. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
SocialFlow L for simplifiying the |
154
|
|
|
|
|
|
|
build/install process. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
Copyright 2011-2013 Alberto Simoes. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
This program is distributed under the (Revised) BSD License: |
161
|
|
|
|
|
|
|
L |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without |
164
|
|
|
|
|
|
|
modification, are permitted provided that the following conditions |
165
|
|
|
|
|
|
|
are met: |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright |
168
|
|
|
|
|
|
|
notice, this list of conditions and the following disclaimer. |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright |
171
|
|
|
|
|
|
|
notice, this list of conditions and the following disclaimer in the |
172
|
|
|
|
|
|
|
documentation and/or other materials provided with the distribution. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
* Neither the name of Alberto Simoes's Organization |
175
|
|
|
|
|
|
|
nor the names of its contributors may be used to endorse or promote |
176
|
|
|
|
|
|
|
products derived from this software without specific prior written |
177
|
|
|
|
|
|
|
permission. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
180
|
|
|
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
181
|
|
|
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
182
|
|
|
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
183
|
|
|
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
184
|
|
|
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
185
|
|
|
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
186
|
|
|
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
187
|
|
|
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
188
|
|
|
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
189
|
|
|
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=cut |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
1; # End of Lingua::Identify::CLD |