| 1 | package Lingua::Identify::CLD; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 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 | 3 | our $VERSION = '0.10'; | |||||||||
| 20 | |||||||||||
| 21 | 3 | ▶ | use XSLoader; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 3 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 3 | 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 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 75 | 2 | my ($class, %options) = @_; | |||||||||
| 76 | 2 | my $self = {%options}; | |||||||||
| 77 | 2 | 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<new>. | ||||||||||
| 85 | |||||||||||
| 86 | =cut | ||||||||||
| 87 | |||||||||||
| 88 | 26 | ▶ | sub identify { | ||||||||
|
Subroutine
identify: called CC=5 SCAR=16.3
Pod
documented
| |||||||||||
| 89 | 26 | my ($self, $text, %options) = @_; | |||||||||
| 90 | |||||||||||
| 91 | 26 | my %cfg = ( %$self, %options ); | |||||||||
| 92 | |||||||||||
| 93 | 26 | ▶ | my $tld = exists($cfg{tld}) ? $cfg{tld} : ""; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 26 | ▶ | my $plaintext = exists($cfg{isPlainText}) ? $cfg{isPlainText} : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 26 | ▶ | my $extended = exists($cfg{allowExtendedLanguages}) ? $cfg{allowExtendedLanguages} : 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 26 | my $confidence = 0; | |||||||||
| 98 | 26 | my $is_reliable = 0; | |||||||||
| 99 | 26 | my $id = ''; | |||||||||
| 100 | |||||||||||
| 101 | 26 | my $lang = _identify($text, $tld, $plaintext, $extended, $id, $confidence, $is_reliable); | |||||||||
| 102 | |||||||||||
| 103 | 26 | ▶ | return wantarray ? (uc $lang, $id, $confidence, $is_reliable) : uc $lang; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | =head1 AUTHOR | ||||||||||
| 107 | |||||||||||
| 108 | Alberto Simoes, C<< <ambs at cpan.org> >> | ||||||||||
| 109 | |||||||||||
| 110 | =head1 BUGS | ||||||||||
| 111 | |||||||||||
| 112 | Please report any bugs or feature requests to | ||||||||||
| 113 | L<http://github.com/ambs/Lingua-Identify-CLD>. 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<http://github.com/ambs/Lingua-Identify-CLD> | ||||||||||
| 131 | |||||||||||
| 132 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 133 | |||||||||||
| 134 | L<http://annocpan.org/dist/Lingua-Identify-CLD> | ||||||||||
| 135 | |||||||||||
| 136 | =item * CPAN Ratings | ||||||||||
| 137 | |||||||||||
| 138 | L<http://cpanratings.perl.org/d/Lingua-Identify-CLD> | ||||||||||
| 139 | |||||||||||
| 140 | =item * Search CPAN | ||||||||||
| 141 | |||||||||||
| 142 | L<http://search.cpan.org/dist/Lingua-Identify-CLD/> | ||||||||||
| 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<http://www.socialflow.com> 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<http://www.opensource.org/licenses/bsd-license.php> | ||||||||||
| 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 | 3 | 1; # End of Lingua::Identify::CLD | |||||||||