Identify.pm

filter: Statementstmt 97.6% 167 / 171 Branchbran 79.0% 49 / 62 Conditioncond 65.0% 13 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 32 / 32 Podpod 100% 15 / 15 totaltotal 90.0% 281 / 312 CC 52 SCAR 23.3
CC
52
Cov
91%
langof
25.8
langof_file
7.1
BEGIN
6.8
CRAP 10.3
↑ Summary Identify/BG.pm »
1 package Lingua::Identify;
2  
3 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our %EXPORT_TAGS =
11 (
12 all => [ qw(
13 langof
14 langof_file
15 confidence
16 get_all_methods
17 activate_all_languages
18 deactivate_all_languages
19 get_all_languages
20 get_active_languages
21 get_inactive_languages
22 is_active
23 is_valid_language
24 activate_language
25 deactivate_language
26 set_active_languages
27 name_of
28 )
29 ],
30 language_identification => [ qw(
31 langof
32 langof_file
33 confidence
34 get_all_methods
35 )
36 ],
37  
38 language_manipulation => [ qw(
39 activate_all_languages
40 deactivate_all_languages
41 get_all_languages
42 get_active_languages
43 get_inactive_languages
44 is_active
45 is_valid_language
46 activate_language
47 deactivate_language
48 set_active_languages
49 name_of
50 )
51 ],
52 );
53  
54 8 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
55 8 our @EXPORT = qw();
56  
57 8 our $VERSION = '0.56';
58  
59  
60 # DEFAULT VALUES #
61 8 our %default_methods = qw/smallwords 1.3 prefixes2 1.5 suffixes3 1.5 ngrams3 1.2/;
62 8 my $default_maxsize = 1_000_000;
63 8 my %default_extractfrom = qw/head 1/;
64  
65 =head1 NAME
66  
67 Lingua::Identify - Language identification
68  
69 =head1 SYNOPSIS
70  
71 use Lingua::Identify qw(:language_identification);
72 $a = langof($textstring); # gives the most probable language
73  
74 or the complete way:
75  
76 @a = langof($textstring); # gives pairs of languages / probabilities
77 # sorted from most to least probable
78  
79 %a = langof($textstring); # gives a hash of language / probability
80  
81 or the expert way (see section OPTIONS, under HOW TO PERFORM IDENTIFICATION)
82  
83 $a = langof( { method => [qw/smallwords prefix2 suffix2/] }, $text);
84  
85 $a = langof( { 'max-size' => 3_000_000 }, $text);
86  
87 $a = langof( { 'extract_from' => ( 'head' => 1, 'tail' => 2)}, $text);
88  
89 =head1 DESCRIPTION
90  
91 B<STARTING WITH VERSION 0.25, Lingua::Identify IS UNICODE BY DEFAULT!>
92  
93 C<Lingua::Identify> identifies the language a given string or file is
94 written in.
95  
96 See section WHY LINGUA::IDENTIFY for a list of C<Lingua::Identify>'s strong
97 points.
98  
99 See section KNOWN LANGUAGES for a list of available languages and HOW TO
100 PERFORM IDENTIFICATION to know how to really use this module.
101  
102 If you're in a hurry, jump to section EXAMPLES, way down below.
103  
104 Also, don't forget to read the following section, IMPORTANT WARNING.
105  
106 =head1 A WARNING ON THE ACCURACY OF LANGUAGE IDENTIFICATION METHODS
107  
108 Take a word that exists in two different languages, take a good look at it and
109 answer this question: "What language does this word belong to?".
110  
111 You can't give an answer like "Language X", right? You can only say it looks
112 like any of a set of languages.
113  
114 Similarly, it isn't always easy to identify the language of a text if the only
115 two active languages are very similar.
116  
117 Now that we've taken out of the way the warning that language identification
118 is not 100% accurate, please keep reading the documentation.
119  
120 =head1 WHY LINGUA::IDENTIFY
121  
122 You might be wondering why you should use Lingua::Identify instead of any other
123 tool for language identification.
124  
125 Here's a list of Lingua::Identify's strong points:
126  
127 =over 6
128  
129 =item * it's free and it's open-source;
130  
131 =item * it's portable (it's Perl, which means it will work in lots of different
132 platforms);
133  
134 =item * unicode support;
135  
136 =item * 4 different methods of language identification and growing (see
137 METHODS OF LANGUAGE IDENTIFICATION for more details on this one);
138  
139 =item * it's a module, which means you can easily write your own application
140 (be it CGI, TK, whatever) around it;
141  
142 =item * it comes with I<langident>, which means you don't actually need to
143 write your own application around it;
144  
145 =item * it's flexible (at the moment, you can actually choose the
146 methods to use and their relevance, the max size of input to analyze
147 each time and which part(s) of the input to analyze)
148  
149 =item * it supports big inputs (through the 'max-size' and
150 'extract_from' options)
151  
152 =item * it's easy to deal with languages (you can activate and
153 deactivate the ones you choose whenever you want to, which can improve
154 your times and accuracy);
155  
156 =item * it's maintained.
157  
158 =back
159  
160 =cut
161  
162 # initialization
163  
164 8 our (@all_languages,@active_languages,%languages,%regexen,@methods);
165 8 ▶BEGIN {
Subroutine BEGIN: called CC=6 SCAR=19.1
166  
167 8 ▶ use Class::Factory::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
168 8 for ( Lingua::Identify->subclasses() ) {
169 216 ▶ /^[A-Z][A-Z]$/ || next;
Branch: 1 branch
BranchTrueFalseTotal
/^[A-Z][A-Z]$/ or next 208 8 216
170 208 eval "require Lingua::Identify::$_ ;";
171 208 ▶ if ($languages{_versions}{lc $_} < 0.02) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lingua::Identify::languages{"_versions"}{lc $_} < "0.02") 0 208 208
172 0 for my $k (keys %languages) {
173 0 ▶ delete($languages{$k}{lc $_}) if exists $languages{$k}{lc $_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Lingua::Identify::languages{$k}{lc $_} 0 0 0
174 }
175 }
176 }
177  
178 8 @all_languages = @active_languages = keys %{$languages{_names}};
179  
180 8 @methods = qw/smallwords/;
181  
182 }
183  
184 =head1 HOW TO PERFORM IDENTIFICATION
185  
186 =head2 langof
187  
188 To identify the language a given text is written in, use the I<langof> function.
189 To get a single value, do:
190  
191 $language = langof($text);
192  
193 To get the most probable language and also the percentage of its probability,
194 do:
195  
196 ($language, $probability) = langof($text);
197  
198 If you want a hash where each active language is mapped into its percentage,
199 use this:
200  
201 %languages = langof($text);
202  
203 =cut
204  
205 70 ▶sub langof {
Subroutine langof: called CC=25 SCAR=32.5
Pod documented
206 70 my %config = ();
207 70 ▶ %config = (%config, %{+shift}) if ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 41 29 70
208  
209 =head3 OPTIONS
210  
211 I<langof> can also be given some configuration parameters, in this way:
212  
213 $language = langof(\%config, $text);
214  
215 These parameters are detailed here:
216  
217 =over 6
218  
219 =item * B<extract-from>
220  
221 When the size of the input exceeds the C'max-size', C<langof> analyzes
222 only the beginning of the file. You can specify which part of the file
223 is analyzed with the 'extract-from' option:
224  
225 langof( { 'extract_from' => 'tail' } , $text );
226  
227 Possible values are 'head' and 'tail' (for now).
228  
229 You can also specify more than one part of the file, so that text is
230 extracted from those parts:
231  
232 langof( { 'extract_from' => [ 'head', 'tail' ] } , $text );
233  
234 (this will be useful when more than two possibilities exist)
235  
236 You can also specify different values for each part of the file (not
237 necessarily for all of them:
238  
239 langof( { 'extract_from' => { head => 40, tail => 60 } } , $text);
240  
241 The line above, for instance, retrives 40% of the text from the
242 beginning and 60% from the end. Note, however, that those values are
243 not percentages. You'd get the same behavior with:
244  
245 langof( { 'extract_from' => { head => 80, tail => 120 } } , $text);
246  
247 The percentages would be the same.
248  
249 =item * B<max-size>
250  
251 By default, C<langof> analyzes only 1,000,000 bytes. You can specify
252 how many bytes (at the most) can be analyzed (if not enough exist, the
253 whole input is still analyzed).
254  
255 langof( { 'max-size' => 2000 }, $text);
256  
257 If you want all the text to be analyzed, set max-size to 0:
258  
259 langof( { 'max-size' => 0 }, $text);
260  
261 See also C<set_max_size>.
262  
263 =item * B<method>
264  
265 You can choose which method or methods to use, and also the relevance of each of
266 them.
267  
268 To choose a single method to use:
269  
270 langof( {method => 'smallwords' }, $text);
271  
272 To choose several methods:
273  
274 langof( {method => [qw/prefixes2 suffixes2/]}, $text);
275  
276 To choose several methods and give them different weight:
277  
278 langof( {method => {smallwords => 0.5, ngrams3 => 1.5} }, $text);
279  
280 To see the list of available methods, see section METHODS OF LANGUAGE
281 IDENTIFICATION.
282  
283 If no method is specified, the configuration for this parameter is the
284 following (this might change in the future):
285  
286 method => {
287 smallwords => 0.5,
288 prefixes2 => 1,
289 suffixes3 => 1,
290 ngrams3 => 1.3
291 };
292  
293 =item * B<mode>
294  
295 By default, C<Lingua::Identify> assumes C<normal> mode, but others are
296 available.
297  
298 In C<dummy> mode, instead of actually calculating anything,
299 C<Lingua::Identify> only does the preparation it has to and then
300 returns a bunch of information, including the list of the active
301 languages, the selected methods, etc. It also returns the text meant
302 to be analised.
303  
304 Do be warned that, with I<langof_file>, the dummy mode still reads the
305 files, it simply doesn't calculate language.
306  
307 langof( { 'mode' => 'dummy' }, $text);
308  
309 This returns something like this:
310  
311 { 'methods' => { 'smallwords' => '0.5',
312 'prefixes2' => '1',
313 },
314 'config' => { 'mode' => 'dummy' },
315 'max-size' => 1000000,
316 'active-languages' => [ 'es', 'pt' ],
317 'text' => $text,
318 'mode' => 'dummy',
319 }
320  
321 =back
322  
323 =cut
324  
325 # select the methods
326 70 ▶ my %methods = defined $config{'method'} ? _make_hash($config{'method'})
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'method'} ? : 11 59 70
327 : %default_methods;
328  
329 # select max-size
330 70 ▶ my $maxsize = defined $config{'max-size'} ? $config{'max-size'}
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'max-size'} ? : 2 68 70
331 : $default_maxsize;
332  
333 # get the text
334 70 my $text = join "\n", @_;
335 70 ▶ return wantarray ? () : undef unless $text;
Branch: 2 branches
BranchTrueFalseTotal
unless $text 2 68 70
wantarray ? : 1 1 2
336  
337 # this is the support for big files; if the input is bigger than the $maxsize, we act
338 68 ▶ if ($maxsize < length $text && $maxsize != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maxsize < length $text and $maxsize != 0) 1 67 68
Condition100%3/3
$maxsize < length $text and $maxsize != 0
!ll&&!rl&&r
6611
MC/DC100%2/2
$maxsize < length $text and $maxsize != 0
$maxsize < length $text $maxsize != 0
Truth table100%3/3
A and B
A= $maxsize < length $textB= $maxsize != 0
ABresult
0X0
100
111
339 # select extract_from
340 1 ▶ my %extractfrom = defined $config{'extract_from'} ? _make_hash($config{'extract_from'})
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'extract_from'} ? : 0 1 1
341 : %default_extractfrom;
342 1 my $total_weight = 0;
343 1 for (keys %extractfrom) {
344 1 ▶ if ($_ eq 'head' or $_ eq 'tail') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'head' or $_ eq 'tail') { } 1 0 1
Condition33%1/3
$_ eq "head" or $_ eq "tail"
l!l&&r!l&&!r
100
MC/DC0%0/2
$_ eq "head" or $_ eq "tail"
$_ eq "head" $_ eq "tail"
Truth table33%1/3
A or B
A= $_ eq "head"B= $_ eq "tail"
ABresult
1X1
011
000
345 1 $total_weight += $extractfrom{$_};
346 1 next;
347 }
348 else {
349 0 delete $extractfrom{$_};
350 }
351 }
352 1 for (keys %extractfrom) {
353 1 $extractfrom{$_} = $extractfrom{$_} / $total_weight;
354 }
355  
356 1 ▶ $extractfrom{'head'} ||= 0;
Condition50%1/2
$extractfrom{"head"} ||= 0
l!l
10
MC/DC0%0/1
$extractfrom{"head"} ||= 0
$extractfrom{"head"}
Truth table50%1/2
A
A= $extractfrom{"head"}
Aresult
11
00
357 1 ▶ $extractfrom{'tail'} ||= 0;
Condition50%1/2
$extractfrom{"tail"} ||= 0
l!l
01
MC/DC0%0/1
$extractfrom{"tail"} ||= 0
$extractfrom{"tail"}
Truth table50%1/2
A
A= $extractfrom{"tail"}
Aresult
11
00
358  
359 1 my $head = int $maxsize * $extractfrom{'head'};
360 1 my $tail = length($text) - $head - int $maxsize * $extractfrom{'tail'};
361 1 substr( $text, $head, $tail, '');
362 }
363  
364 # dummy mode exits here
365 68 ▶ $config{'mode'} ||= 'normal';
Condition100%2/2
$config{"mode"} ||= "normal"
l!l
761
MC/DC100%1/1
$config{"mode"} ||= "normal"
$config{"mode"}
Truth table100%2/2
A
A= $config{"mode"}
Aresult
11
00
366 68 ▶ if ($config{'mode'} eq 'dummy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($config{"mode"} eq "dummy") 7 61 68
367 return {
368 'method' => \%methods,
369 'max-size' => $maxsize,
370 'config' => \%config,
371 'active-languages' => [ sort (get_active_languages()) ],
372 'text' => $text,
373 7 'mode' => $config{'mode'},
374 };
375 }
376  
377 # use the methods
378 61 my (%result, $total);
379 61 for (keys %methods) {
380 229 my %temp_result;
381  
382 229 ▶ if (/^smallwords$/) {
Branch: 4 branches
BranchTrueFalseTotal
if (/^smallwords$/) { } 58 171 229
elsif (/^(prefixes[1-4])$/) { } 56 115 171
elsif (/^(suffixes[1-4])$/) { } 59 56 115
elsif (/^(ngrams[1-4])$/) { } 56 0 56
383 58 %temp_result = _langof_by_word_method('smallwords', $text);
384 }
385 elsif (/^(prefixes[1-4])$/) {
386 56 %temp_result = _langof_by_prefix_method($1, $text);
387 }
388 elsif (/^(suffixes[1-4])$/) {
389 59 %temp_result = _langof_by_suffix_method($1, $text);
390 }
391 elsif (/^(ngrams[1-4])$/) {
392 56 %temp_result = _langof_by_ngram_method($1, $text);
393 }
394  
395 229 for my $l (keys %temp_result) {
396 3154 my $temp = $temp_result{$l} * $methods{$_};
397 3154 $result{$l} += $temp;
398 3154 $total += $temp;
399 }
400 }
401  
402 # report the results
403 my @result = (
404 1040 ▶ map { ( $_, ($total ? $result{$_} / $total : 0)) }
Branch: 1 branch
BranchTrueFalseTotal
$total ? : 1040 0 1040
405 61 sort { $result{$b} <=> $result{$a} } keys %result
406 );
407  
408 61 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 60 1 61
409 }
410  
411 11 ▶sub _make_hash {
Subroutine _make_hash: called CC=5 SCAR=16.1
412 11 my %hash;
413 11 my $temp = shift;
414 11 for (ref($temp)) {
415 11 ▶ if (/^HASH$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^HASH$/) { } 1 10 11
elsif (/^ARRAY$/) { } 1 9 10
416 1 %hash = %{$temp};
417 }
418 elsif (/^ARRAY$/) {
419 1 for (@{$temp}) {
420 2 $hash{$_}++;
421 }
422 }
423 else {
424 9 $hash{$temp} = 1;
425 }
426 }
427 11 %hash;
428 }
429  
430 =head2 langof_file
431  
432 I<langof_file> works just like I<langof>, with the exception that it
433 reveives filenames instead of text. It reads these texts (if existing
434 and readable, of course) and parses its content.
435  
436 Currently, I<langof_file> assumes the files are regular text. This may
437 change in the future and the files might be scanned to check their
438 filetype and then parsed to extract only their textual content (which
439 should be pretty useful so that you can perform language
440 identification, say, in HTML files, or PDFs).
441  
442 To identify the language a file is written in:
443  
444 $language = langof_file($path);
445  
446 To get the most probable language and also the percentage of its probability,
447 do:
448  
449 ($language, $probability) = langof_file($path);
450  
451 If you want a hash where each active language is mapped into its percentage,
452 use this:
453  
454 %languages = langof_file($path);
455  
456 If you pass more than one file to I<langof_file>, they will all be
457 read and their content merged and then parsed for language
458 identification.
459  
460 =cut
461  
462 30 ▶sub langof_file {
Subroutine langof_file: called CC=7 SCAR=19.6
Pod documented
463 30 my %config = ();
464 30 ▶ if (ref($_[0]) eq 'HASH') {%config = (%config, %{+shift})}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH") 2 28 30
465  
466 =head3 OPTIONS
467  
468 I<langof_file> accepts all the options I<langof> does, so refer to
469 those first (up in this document).
470  
471 $language = langof_file(\%config, $path);
472  
473 I<langof_file> currently only reads the first 10,000 bytes of each
474 file.
475  
476 You can force an input encoding with C<< { encoding => 'ISO-8859-1' } >>
477 in the configuration hash.
478  
479 =cut
480  
481 # select max-size
482 30 ▶ my $maxsize = defined $config{'max-size'} ? $config{'max-size'}
Branch: 1 branch
BranchTrueFalseTotal
defined $config{'max-size'} ? : 0 30 30
483 : $default_maxsize;
484  
485 30 my @files = @_;
486 30 my $text = '';
487  
488 30 for my $file (@files) {
489 #-r and -e or next;
490 31 ▶ if (exists($config{encoding})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $config{'encoding'}) { } 1 30 31
491 1 ▶ open(FILE, "<:encoding($config{encoding})", $file) or next;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, qq[<:encoding($config{"encoding"})], $file or next 1 0 1
492 } else {
493 30 ▶ open(FILE, "<:utf8", $file) or next;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<:utf8", $file or next 30 0 30
494 }
495 31 local $/ = \$maxsize;
496 31 $text .= <FILE>;
497 31 close(FILE);
498 }
499  
500 30 return langof(\%config,$text);
501 }
502  
503 =head2 confidence
504  
505 After getting the results into an array, its first element is the most probable
506 language. That doesn't mean it is very probable or not.
507  
508 You can find more about the likeliness of the results to be accurate by
509 computing its confidence level.
510  
511 use Lingua::Identify qw/:language_identification/;
512 my @results = langof($text);
513 my $confidence_level = confidence(@results);
514 # $confidence_level now holds a value between 0.5 and 1; the higher that
515 # value, the more accurate the results seem to be
516  
517 The formula used is pretty simple: p1 / (p1 + p2) , where p1 is the
518 probability of the most likely language and p2 is the probability of
519 the language which came in second. A couple of examples to illustrate
520 this:
521  
522 English 50% Portuguese 10% ...
523  
524 confidence level: 50 / (50 + 10) = 0.83
525  
526 Another example:
527  
528 Spanish 30% Portuguese 10% ...
529  
530 confidence level: 30 / (25 + 30) = 0.55
531  
532 French 10% German 5% ...
533  
534 confidence level: 10 / (10 + 5) = 0.67
535  
536 As you can see, the first example is probably the most accurate one.
537 Are there any doubts? The English language has five times the
538 probability of the second language.
539  
540 The second example is a bit more tricky. 55% confidence. The
541 confidence level is always above 50%, for obvious reasons. 55% doesn't
542 make anyone confident in the results, and one shouldn't be, with
543 results such as these.
544  
545 Notice the third example. The confidence level goes up to 67%, but the
546 probability of French is of mere 10%. So what? It's twice as much as
547 the second language. The low probability may well be caused by a great
548 number of languages in play.
549  
550 =cut
551  
552 60 ▶sub confidence {
Subroutine confidence: called CC=5 SCAR=16.3
Pod documented
553 60 ▶ defined $_[1] and $_[1] or return 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] and $_[1] or return 0 57 3 60
Condition66%2/3
defined $_[1] and $_[1]
!ll&&!rl&&r
3057
MC/DC50%1/2
defined $_[1] and $_[1]
defined $_[1] $_[1]
Truth table66%2/3
A and B
A= defined $_[1]B= $_[1]
ABresult
0X0
100
111
554 57 ▶ defined $_[3] and $_[3] or return 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] and $_[3] or return 1 56 1 57
Condition66%2/3
defined $_[3] and $_[3]
!ll&&!rl&&r
1056
MC/DC50%1/2
defined $_[3] and $_[3]
defined $_[3] $_[3]
Truth table66%2/3
A and B
A= defined $_[3]B= $_[3]
ABresult
0X0
100
111
555 56 $_[1] / ($_[1] + $_[3]);
556 }
557  
558 =head2 get_all_methods
559  
560 Returns a list comprised of all the available methods for language
561 identification.
562  
563 =cut
564  
565 1 ▶sub get_all_methods {
Subroutine get_all_methods: called CC=1 SCAR=0.0
Pod documented
566 1 qw/smallwords
567 prefixes1 prefixes2 prefixes3 prefixes4
568 suffixes1 suffixes2 suffixes3 suffixes4
569 ngrams1 ngrams2 ngrams3 ngrams4/
570 }
571  
572 =head1 LANGUAGE IDENTIFICATION IN GENERAL
573  
574 Language identification is based in patterns.
575  
576 In order to identify the language a given text is written in, we repeat a given
577 process for each active language (see section LANGUAGES MANIPULATION); in that
578 process, we look for common patterns of that language. Those patterns can be
579 prefixes, suffixes, common words, ngrams or even sequences of words.
580  
581 After repeating the process for each language, the total score for each of them
582 is then used to compute the probability (in percentage) for each language to be
583 the one of that text.
584  
585 =cut
586  
587 229 ▶sub _langof_by_method {
Subroutine _langof_by_method: called CC=6 SCAR=18.0
588 229 my ($method, $elements, $text) = @_;
589 229 my (%result, $total);
590  
591 229 for my $language (get_active_languages()) {
592 5903 for (keys %{$languages{$method}{$language}}) {
593 116140 ▶ if (exists $$elements{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $elements->{$_}) 15746 100394 116140
594 $result{$language} +=
595 15746 $$elements{$_} * ${languages{$method}{$language}{$_}};
596 $total +=
597 15746 $$elements{$_} * ${languages{$method}{$language}{$_}};
598 }
599 }
600 }
601  
602 my @result = (
603 3154 ▶ map { ( $_, ($total ? $result{$_} / $total : 0)) }
Branch: 1 branch
BranchTrueFalseTotal
$total ? : 3154 0 3154
604 229 sort { $result{$b} <=> $result{$a} } keys %result
605 );
606  
607 229 ▶ return wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 229 0 229
608 }
609  
610 =head1 METHODS OF LANGUAGE IDENTIFICATION
611  
612 C<Lingua::Identify> currently comprises four different ways for language
613 identification, in a total of thirteen variations of those.
614  
615 The available methods are the following: B<smallwords>, B<prefixes1>,
616 B<prefixes2>, B<prefixes3>, B<prefixes4>, B<suffixes1>, B<suffixes2>,
617 B<suffixes3>, B<suffixes4>, B<ngrams1>, B<ngrams2>, B<ngrams3> and B<ngrams4>.
618  
619 Here's a more detailed explanation of each of those ways and those methods
620  
621 =head2 Small Word Technique - B<smallwords>
622  
623 The "Small Word Technique" searches the text for the most common words of each
624 active language. These words are usually articles, pronouns, etc, which happen
625 to be (usually) the shortest words of the language; hence, the method name.
626  
627 This is usually a good method for big texts, especially if you happen to have
628 few languages active.
629  
630 =cut
631  
632 58 ▶sub _langof_by_word_method {
Subroutine _langof_by_word_method: called CC=1 SCAR=0.0
633 58 my ($method, $text) = @_;
634  
635 58 ▶ sub _words_count {
Subroutine _words_count: called CC=2 SCAR=6.9
636 58 my ($words, $text) = @_;
637 58 for my $word (split /[\s\n]+/, $text) {
638 17437 $words->{$word}++
639 }
640 }
641  
642 58 my %words;
643 58 _words_count(\%words, $text);
644 58 return _langof_by_method($method, \%words, $text);
645 }
646  
647 =head2 Prefix Analysis - B<prefixes1>, B<prefixes2>, B<prefixes3>, B<prefixes4>
648  
649 This method analyses text for the common prefixes of each active language.
650  
651 The methods are, respectively, for prefixes of size 1, 2, 3 and 4.
652  
653 =cut
654  
655 56 ▶sub _langof_by_prefix_method {
Subroutine _langof_by_prefix_method: called CC=1 SCAR=0.0
656 8 ▶ use Text::Affixes;
Subroutine BEGIN: called CC=1 SCAR=0.0
657  
658 56 (my $method = shift) =~ /^prefixes(\d)$/;
659 56 my $text = shift;
660  
661 56 my $prefixes = get_prefixes( {min => $1, max => $1}, $text);
662  
663 56 return _langof_by_method($method, $$prefixes{$1}, $text);
664 }
665  
666 =head2 Suffix Analysis - B<suffixes1>, B<suffixes2>, B<suffixes3>, B<suffixes4>
667  
668 Similar to the Prefix Analysis (see above), but instead analysing common
669 suffixes.
670  
671 The methods are, respectively, for suffixes of size 1, 2, 3 and 4.
672  
673 =cut
674  
675 59 ▶sub _langof_by_suffix_method {
Subroutine _langof_by_suffix_method: called CC=1 SCAR=0.0
676 8 ▶ use Text::Affixes;
Subroutine BEGIN: called CC=1 SCAR=0.0
677  
678 59 (my $method = shift) =~ /^suffixes(\d)$/;
679 59 my $text = shift;
680  
681 59 my $suffixes = get_suffixes({min => $1, max => $1}, $text);
682  
683 59 return _langof_by_method($method, $$suffixes{$1}, $text);
684 }
685  
686 ###
687  
688 # Have you seen my brother? He's a two line long comment. I think he
689 # might be lost... :-\ Me and my father have been looking for him for
690 # some time now :-/
691  
692 ###
693  
694 =head2 Ngram Categorization - B<ngrams1>, B<ngrams2>, B<ngrams3>, B<ngrams4>
695  
696 Ngrams are sequences of tokens. You can think of them as syllables, but they
697 are also more than that, as they are not only comprised by characters, but also
698 by spaces (delimiting or separating words).
699  
700 Ngrams are a very good way for identifying languages, given that the most
701 common ones of each language are not generally very common in others.
702  
703 This is usually the best method for small amounts of text or too many active
704 languages.
705  
706 The methods are, respectively, for ngrams of size 1, 2, 3 and 4.
707  
708 =cut
709  
710 56 ▶sub _langof_by_ngram_method {
Subroutine _langof_by_ngram_method: called CC=1 SCAR=0.0
711 8 ▶ use Text::Ngram qw(ngram_counts);
Subroutine BEGIN: called CC=1 SCAR=0.0
712  
713 56 (my $method = shift) =~ /^ngrams([1-4])$/;
714 56 my $text = shift;
715  
716 56 my $ngrams = ngram_counts( {spaces => 0}, $text, $1);
717  
718 56 return _langof_by_method($method, $ngrams, $text);
719 }
720  
721 =head1 LANGUAGE MANIPULATION
722  
723 When trying to perform language identification, C<Lingua::Identify> works not with
724 all available languages, but instead with the ones that are active.
725  
726 By default, all available languages are active, but that can be changed by the
727 user.
728  
729 For your convenience, several methods regarding language manipulation were
730 created. In order to use them, load the module with the tag
731 :language_manipulation.
732  
733 These methods work with the two letters code for languages.
734  
735 =over 6
736  
737 =item B<activate_language>
738  
739 Activate a language
740  
741 activate_language('en');
742  
743 # or
744  
745 activate_language($_) for get_all_languages();
746  
747 =cut
748  
749 1 ▶sub activate_language {
Subroutine activate_language: called CC=2 SCAR=7.6
Pod documented
750 0 ▶ unless (grep { $_ eq $_[0] } @active_languages) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $_[0];} @Lingua::Identify::active_languages) 1 0 1
751 1 push @active_languages, $_[0];
752 }
753 1 return @active_languages;
754 }
755  
756 =item B<activate_all_languages>
757  
758 Activates all languages
759  
760 activate_all_languages();
761  
762 =cut
763  
764 2 ▶sub activate_all_languages {
Subroutine activate_all_languages: called CC=1 SCAR=0.0
Pod documented
765 2 @active_languages = get_all_languages();
766 2 return @active_languages;
767 }
768  
769 =item B<deactivate_language>
770  
771 Deactivates a language
772  
773 deactivate_language('en');
774  
775 =cut
776  
777 1 ▶sub deactivate_language {
Subroutine deactivate_language: called CC=1 SCAR=0.0
Pod documented
778 1 @active_languages = grep { ! ($_ eq $_[0]) } @active_languages;
779 1 return @active_languages;
780 }
781  
782 =item B<deactivate_all_languages>
783  
784 Deactivates all languages
785  
786 deactivate_all_languages();
787  
788 =cut
789  
790 3 ▶sub deactivate_all_languages {
Subroutine deactivate_all_languages: called CC=1 SCAR=0.0
Pod documented
791 3 @active_languages = ();
792 3 return @active_languages;
793 }
794  
795 =item B<get_all_languages>
796  
797 Returns the names of all available languages
798  
799 my @all_languages = get_all_languages();
800  
801 =cut
802  
803 54 ▶sub get_all_languages {
Subroutine get_all_languages: called CC=1 SCAR=0.0
Pod documented
804 54 return @all_languages;
805 }
806  
807 =item B<get_active_languages>
808  
809 Returns the names of all active languages
810  
811 my @active_languages = get_active_languages();
812  
813 =cut
814  
815 299 ▶sub get_active_languages {
Subroutine get_active_languages: called CC=1 SCAR=0.0
Pod documented
816 299 return @active_languages;
817 }
818  
819 =item B<get_inactive_languages>
820  
821 Returns the names of all inactive languages
822  
823 my @active_languages = get_inactive_languages();
824  
825 =cut
826  
827 2 ▶sub get_inactive_languages {
Subroutine get_inactive_languages: called CC=1 SCAR=0.0
Pod documented
828 2 return grep { ! is_active($_) } get_all_languages();
829 }
830  
831 =item B<is_active>
832  
833 Returns the name of the language if it is active, an empty list otherwise
834  
835 if (is_active('en')) {
836 # YOUR CODE HERE
837 }
838  
839 =cut
840  
841 54 ▶sub is_active {
Subroutine is_active: called CC=1 SCAR=0.0
Pod documented
842 54 return grep { $_ eq $_[0] } get_active_languages();
843 }
844  
845 =item B<is_valid_language>
846  
847 Returns the name of the language if it exists, an empty list otherwise
848  
849 if (is_valid_language('en')) {
850 # YOUR CODE HERE
851 }
852  
853 =cut
854  
855 36 ▶sub is_valid_language {
Subroutine is_valid_language: called CC=1 SCAR=0.0
Pod documented
856 36 return grep { $_ eq $_[0] } get_all_languages();
857 }
858  
859 =item B<set_active_languages>
860  
861 Sets the active languages
862  
863 set_active_languages('en', 'pt');
864  
865 # or
866  
867 set_active_languages(get_all_languages());
868  
869 =cut
870  
871 3 ▶sub set_active_languages {
Subroutine set_active_languages: called CC=1 SCAR=0.0
Pod documented
872 3 @active_languages = grep { is_valid_language($_) } @_;
873 3 return @active_languages;
874 }
875  
876 =item B<name_of>
877  
878 Given the two letter tag of a language, returns its name
879  
880 my $language_name = name_of('pt');
881  
882 =cut
883  
884 1 ▶sub name_of {
Subroutine name_of: called CC=2 SCAR=7.2
Pod documented
885 1 ▶ my $tag = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
10
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
886 1 return $languages{_names}{$tag};
887 }
888  
889 =back
890  
891 =cut
892  
893 8 1;
894 __END__
↑ Summary Identify/BG.pm »