Debian Codegraph @

README

raw
1 Lingua::Identify version
2 ========================
3
4 =head1 NAME
5
6 Lingua::Identify - Language identification
7
8 =head1 SYNOPSIS
9
10 use Lingua::Identify qw(:language_identification);
11 $a = langof($textstring); # gives the most probable language
12
13 or the complete way:
14
15 @a = langof($textstring); # gives pairs of languages / probabilities
16 # sorted from most to least probable
17
18 %a = langof($textstring); # gives a hash of language / probability
19
20 or the expert way (see section OPTIONS, under HOW TO PERFORM IDENTIFICATION)
21
22 $a = langof( { method => [qw/smallwords prefix2 suffix2/] }, $text);
23
24 $a = langof( { 'max-size' => 3_000_000 }, $text);
25
26 $a = langof( { 'extract_from' => ( 'head' => 1, 'tail' => 2)}, $text);
27
28 =head1 DESCRIPTION
29
30 C<Lingua::Identify> identifies the language a given string or file is
31 written in.
32
33 See section WHY LINGUA::IDENTIFY for a list of C<Lingua::Identify>'s strong
34 points.
35
36 See section KNOWN LANGUAGES for a list of available languages and HOW TO
37 PERFORM IDENTIFICATION to know how to really use this module.
38
39 If you're in a hurry, jump to section EXAMPLES, way down below.
40
41 Also, don't forget to read the following section, IMPORTANT WARNING.
42
43 =head1 A WARNING ON THE ACCURACY OF LANGUAGE IDENTIFICATION METHODS
44
45 Take a word that exists in two different languages, take a good look at it and
46 answer this question: "What language does this word belong to?".
47
48 You can't give an answer like "Language X", right? You can only say it looks
49 like any of a set of languages.
50
51 Similarly, it isn't always easy to identify the language of a text if the only
52 two active languages are very similar.
53
54 Now that we've taken out of the way the warning that language identification
55 is not 100% accurate, please keep reading the documentation.
56
57 =head1 WHY LINGUA::IDENTIFY
58
59 You might be wondering why you should use Lingua::Identify instead of any other
60 tool for language identification.
61
62 Here's a list of Lingua::Identify's strong points:
63
64 =over 6
65
66 =item * it's free and it's open-source;
67
68 =item * it's portable (it's Perl, which means it will work in lots of different
69 platforms);
70
71 =item * 33 languages and growing;
72
73 =item * 4 different methods of language identification and growing (see
74 METHODS OF LANGUAGE IDENTIFICATION for more details on this one);
75
76 =item * it's a module, which means you can easily write your own application
77 (be it CGI, TK, whatever) around it;
78
79 =item * it comes with I<langident>, which means you don't actually need to
80 write your own application around it;
81
82 =item * it's flexible (at the moment, you can actually choose the
83 methods to use and their relevance, the max size of input to analyze
84 each time and which part(s) of the input to analyze)
85
86 =item * it supports big inputs (through the 'max-size' and
87 'extract_from' options)
88
89 =item * it's easy to deal with languages (you can activate and
90 deactivate the ones you choose whenever you want to, which can improve
91 your times and accuracy);
92
93 =item * it's maintained.
94
95 =back
96
97 =head1 HOW TO PERFORM IDENTIFICATION
98
99 =head2 langof
100
101 To identify the language a given text is written in, use the I<langof> function.
102 To get a single value, do:
103
104 $language = langof($text);
105
106 To get the most probable language and also the percentage of its probability,
107 do:
108
109 ($language, $probability) = langof($text);
110
111 If you want a hash where each active language is mapped into its percentage,
112 use this:
113
114 %languages = langof($text);
115
116 =head3 OPTIONS
117
118 I<langof> can also be given some configuration parameters, in this way:
119
120 $language = langof(\%config, $text);
121
122 These parameters are detailed here:
123
124 =over 6
125
126 =item * B<extract-from>
127
128 When the size of the input exceeds the C'max-size', C<langof> analyzes
129 only the beginning of the file. You can specify which part of the file
130 is analyzed with the 'extract-from' option:
131
132 langof( { 'extract_from' => 'tail' } , $text );
133
134 Possible values are 'head' and 'tail' (for now).
135
136 You can also specify more than one part of the file, so that text is
137 extracted from those parts:
138
139 langof( { 'extract_from' => [ 'head', 'tail' ] } , $text );
140
141 (this will be useful when more than two possibilities exist)
142
143 You can also specify different values for each part of the file (not
144 necessarily for all of them:
145
146 langof( { 'extract_from' => { head => 40, tail => 60 } } , $text);
147
148 The line above, for instance, retrives 40% of the text from the
149 beginning and 60% from the end. Note, however, that those values are
150 not percentages. You'd get the same behavior with:
151
152 langof( { 'extract_from' => { head => 80, tail => 120 } } , $text);
153
154 The percentages would be the same.
155
156 =item * B<max-size>
157
158 By default, C<langof> analyzes only 1,000,000 bytes. You can specify
159 how many bytes (at the most) can be analyzed (if not enough exist, the
160 whole input is still analyzed).
161
162 langof( { 'max-size' => 2000 }, $text);
163
164 If you want all the text to be analyzed, set max-size to 0:
165
166 langof( { 'max-size' => 0 }, $text);
167
168 See also C<set_max_size>.
169
170 =item * B<method>
171
172 You can choose which method or methods to use, and also the relevance of each of
173 them.
174
175 To choose a single method to use:
176
177 langof( {method => 'smallwords' }, $text);
178
179 To choose several methods:
180
181 langof( {method => [qw/prefixes2 suffixes2/]}, $text);
182
183 To choose several methods and give them different weight:
184
185 langof( {method => {smallwords => 0.5, ngrams3 => 1.5} }, $text);
186
187 To see the list of available methods, see section METHODS OF LANGUAGE
188 IDENTIFICATION.
189
190 If no method is specified, the configuration for this parameter is the
191 following (this might change in the future):
192
193 method => {
194 smallwords => 0.5,
195 prefixes2 => 1,
196 suffixes3 => 1,
197 ngrams3 => 1.3
198 };
199
200 =item * B<mode>
201
202 By default, C<Lingua::Identify> assumes C<normal> mode, but others are
203 available.
204
205 In C<dummy> mode, instead of actually calculating anything,
206 C<Lingua::Identify> only does the preparation it has to and then
207 returns a bunch of information, including the list of the active
208 languages, the selected methods, etc. It also returns the text meant
209 to be analised.
210
211 Do be warned that, with I<langof_file>, the dummy mode still reads the
212 files, it simply doesn't calculate language.
213
214 langof( { 'mode' => 'dummy' }, $text);
215
216 This returns something like this:
217
218 { 'methods' => { 'smallwords' => '0.5',
219 'prefixes2' => '1',
220 },
221 'config' => { 'mode' => 'dummy' },
222 'max-size' => 1000000,
223 'active-languages' => [ 'es', 'pt' ],
224 'text' => $text,
225 'mode' => 'dummy',
226 }
227
228 =back
229
230 =head2 langof_file
231
232 I<langof_file> works just like I<langof>, with the exception that it
233 reveives filenames instead of text. It reads these texts (if existing
234 and readable, of course) and parses its content.
235
236 Currently, I<langof_file> assumes the files are regular text. This may
237 change in the future and the files might be scanned to check their
238 filetype and then parsed to extract only their textual content (which
239 should be pretty useful so that you can perform language
240 identification, say, in HTML files, or PDFs).
241
242 To identify the language a file is written in:
243
244 $language = langof_file($path);
245
246 To get the most probable language and also the percentage of its probability,
247 do:
248
249 ($language, $probability) = langof_file($path);
250
251 If you want a hash where each active language is mapped into its percentage,
252 use this:
253
254 %languages = langof_file($path);
255
256 If you pass more than one file to I<langof_file>, they will all be
257 read and their content merged and then parsed for language
258 identification.
259
260 =head3 OPTIONS
261
262 I<langof_file> accepts all the options I<langof> does, so refer to
263 those first (up in this document).
264
265 $language = langof_file(\%config, $path);
266
267 I<langof_file> currently only reads the first 10,000 bytes of each
268 file.
269
270 =head2 confidence
271
272 After getting the results into an array, its first element is the most probable
273 language. That doesn't mean it is very probable or not.
274
275 You can find more about the likeliness of the results to be accurate by
276 computing its confidence level.
277
278 use Lingua::Identify qw/:language_identification/;
279 my @results = langof($text);
280 my $confidence_level = confidence(@results);
281 # $confidence_level now holds a value between 0.5 and 1; the higher that
282 # value, the more accurate the results seem to be
283
284 The formula used is pretty simple: p1 / (p1 + p2) , where p1 is the
285 probability of the most likely language and p2 is the probability of
286 the language which came in second. A couple of examples to illustrate
287 this:
288
289 English 50% Portuguese 10% ...
290
291 confidence level: 50 / (50 + 10) = 0.83
292
293 Another example:
294
295 Spanish 30% Portuguese 10% ...
296
297 confidence level: 30 / (25 + 30) = 0.55
298
299 French 10% German 5% ...
300
301 confidence level: 10 / (10 + 5) = 0.67
302
303 As you can see, the first example is probably the most accurate one.
304 Are there any doubts? The English language has five times the
305 probability of the second language.
306
307 The second example is a bit more tricky. 55% confidence. The
308 confidence level is always above 50%, for obvious reasons. 55% doesn't
309 make anyone confident in the results, and one shouldn't be, with
310 results such as these.
311
312 Notice the third example. The confidence level goes up to 67%, but the
313 probability of French is of mere 10%. So what? It's twice as much as
314 the second language. The low probability may well be caused by a great
315 number of languages in play.
316
317 =head2 get_all_methods
318
319 Returns a list comprised of all the available methods for language
320 identification.
321
322 =head1 LANGUAGE IDENTIFICATION IN GENERAL
323
324 Language identification is based in patterns.
325
326 In order to identify the language a given text is written in, we repeat a given
327 process for each active language (see section LANGUAGES MANIPULATION); in that
328 process, we look for common patterns of that language. Those patterns can be
329 prefixes, suffixes, common words, ngrams or even sequences of words.
330
331 After repeating the process for each language, the total score for each of them
332 is then used to compute the probability (in percentage) for each language to be
333 the one of that text.
334
335 =head1 METHODS OF LANGUAGE IDENTIFICATION
336
337 C<Lingua::Identify> currently comprises four different ways for language
338 identification, in a total of thirteen variations of those.
339
340 The available methods are the following: B<smallwords>, B<prefixes1>,
341 B<prefixes2>, B<prefixes3>, B<prefixes4>, B<suffixes1>, B<suffixes2>,
342 B<suffixes3>, B<suffixes4>, B<ngrams1>, B<ngrams2>, B<ngrams3> and B<ngrams4>.
343
344 Here's a more detailed explanation of each of those ways and those methods
345
346 =head2 Small Word Technique - B<smallwords>
347
348 The "Small Word Technique" searches the text for the most common words of each
349 active language. These words are usually articles, pronouns, etc, which happen
350 to be (usually) the shortest words of the language; hence, the method name.
351
352 This is usually a good method for big texts, especially if you happen to have
353 few languages active.
354
355 =head2 Prefix Analysis - B<prefixes1>, B<prefixes2>, B<prefixes3>, B<prefixes4>
356
357 This method analyses text for the common prefixes of each active language.
358
359 The methods are, respectively, for prefixes of size 1, 2, 3 and 4.
360
361 =head2 Suffix Analysis - B<suffixes1>, B<suffixes2>, B<suffixes3>, B<suffixes4>
362
363 Similar to the Prefix Analysis (see above), but instead analysing common
364 suffixes.
365
366 The methods are, respectively, for suffixes of size 1, 2, 3 and 4.
367
368 =head2 Ngram Categorization - B<ngrams1>, B<ngrams2>, B<ngrams3>, B<ngrams4>
369
370 Ngrams are sequences of tokens. You can think of them as syllables, but they
371 are also more than that, as they are not only comprised by characters, but also
372 by spaces (delimiting or separating words).
373
374 Ngrams are a very good way for identifying languages, given that the most
375 common ones of each language are not generally very common in others.
376
377 This is usually the best method for small amounts of text or too many active
378 languages.
379
380 The methods are, respectively, for ngrams of size 1, 2, 3 and 4.
381
382 =head1 LANGUAGE MANIPULATION
383
384 When trying to perform language identification, C<Lingua::Identify> works not with
385 all available languages, but instead with the ones that are active.
386
387 By default, all available languages are active, but that can be changed by the
388 user.
389
390 For your convenience, several methods regarding language manipulation were
391 created. In order to use them, load the module with the tag
392 :language_manipulation.
393
394 These methods work with the two letters code for languages.
395
396 =over 6
397
398 =item B<activate_language>
399
400 Activate a language
401
402 activate_language('en');
403
404 # or
405
406 activate_language($_) for get_all_languages();
407
408 =item B<activate_all_languages>
409
410 Activates all languages
411
412 activate_all_languages();
413
414 =item B<deactivate_language>
415
416 Deactivates a language
417
418 deactivate_language('en');
419
420 =item B<deactivate_all_languages>
421
422 Deactivates all languages
423
424 deactivate_all_languages();
425
426 =item B<get_all_languages>
427
428 Returns the names of all available languages
429
430 my @all_languages = get_all_languages();
431
432 =item B<get_active_languages>
433
434 Returns the names of all active languages
435
436 my @active_languages = get_active_languages();
437
438 =item B<get_inactive_languages>
439
440 Returns the names of all inactive languages
441
442 my @active_languages = get_inactive_languages();
443
444 =item B<is_active>
445
446 Returns the name of the language if it is active, an empty list otherwise
447
448 if (is_active('en')) {
449 # YOUR CODE HERE
450 }
451
452 =item B<is_valid_language>
453
454 Returns the name of the language if it exists, an empty list otherwise
455
456 if (is_valid_language('en')) {
457 # YOUR CODE HERE
458 }
459
460 =item B<set_active_languages>
461
462 Sets the active languages
463
464 set_active_languages('en', 'pt');
465
466 # or
467
468 set_active_languages(get_all_languages());
469
470 =item B<name_of>
471
472 Given the two letter tag of a language, returns its name
473
474 my $language_name = name_of('pt');
475
476 =back
477
478 =head1 KNOWN LANGUAGES
479
480 Currently, C<Lingua::Identify> knows the following languages (33 total):
481
482 =over 6
483
484 =item AF - Afrikaans
485
486 =item BG - Bulgarian
487
488 =item BR - Breton
489
490 =item BS - Bosnian
491
492 =item CY - Welsh
493
494 =item DA - Danish
495
496 =item DE - German
497
498 =item EN - English
499
500 =item EO - Esperanto
501
502 =item ES - Spanish
503
504 =item FI - Finnish
505
506 =item FR - French
507
508 =item FY - Frisian
509
510 =item GA - Irish
511
512 =item HR - Croatian
513
514 =item HU - Hungarian
515
516 =item ID - Indonesian
517
518 =item IS - Icelandic
519
520 =item IT - Italian
521
522 =item LA - Latin
523
524 =item MS - Malay
525
526 =item NL - Dutch
527
528 =item NO - Norwegian
529
530 =item PL - Polish
531
532 =item PT - Portuguese
533
534 =item RO - Romanian
535
536 =item RU - Russian
537
538 =item SL - Slovene
539
540 =item SO - Somali
541
542 =item SQ - Albanian
543
544 =item SV - Swedish
545
546 =item SW - Swahili
547
548 =item TR - Turkish
549
550 =back
551
552 =head1 CONTRIBUTING WITH NEW LANGUAGES
553
554 Please do not contribute with modules you made yourself. It's easier
555 to contribute with unprocessed text, because that allows for new
556 versions of Lingua::Identify not having to drop languages down in case
557 I can't contact you by that time.
558
559 Use I<make-lingua-identify-language> to create a new module for your
560 own personal use, if you must, but try to contribute with unprocessed
561 text rather than those modules.
562
563 =head1 EXAMPLES
564
565 =head2 THE BASIC EXAMPLE
566
567 Check the language a given text file is written in:
568
569 use Lingua::Identify qw/langof/;
570
571 my $text = join "\n", <>;
572
573 # identify the language by letting the module decide on the best way
574 # to do so
575 my $language = langof($text);
576
577 =head2 IDENTIFYING BETWEEN TWO LANGUAGES
578
579 Check the language a given text file is written in, supposing you
580 happen to know it's either Portuguese or English:
581
582 use Lingua::Identify qw/langof set_active_languages/;
583 set_active_languages(qw/pt en/);
584
585 my $text = join "\n", <>;
586
587 # identify the language by letting the module decide on the best way
588 # to do so
589 my $language = langof($text);
590
591 =head1 TO DO
592
593 =over 6
594
595 =item * WordNgrams based methods;
596
597 =item * More languages (always);
598
599 =item * File recognition and treatment;
600
601 =item * Deal with different encodings;
602
603 =item * Create sets of languages and allow their activation/deactivation;
604
605 =item * There should be a way of knowing the default configuration
606 (other than using the dummy mode, of course, or than accessing the variables
607 directly);
608
609 =item * Add a section about other similar tools.
610
611 =back
612
613 =head1 SEE ALSO
614
615 langident(1), Text::ExtractWords(3), Text::Ngram(3), Text::Affixes(3).
616
617 ISO 639 Language Codes, at http://www.w3.org/WAI/ER/IG/ert/iso639.htm
618
619 =head1 AUTHOR
620
621 Alberto Simoes, C<< <[email protected]> >>
622
623 Jose Castro, C<< <[email protected]> >>
624
625 =head1 COPYRIGHT & LICENSE
626
627 Copyright 2008 Alberto Simoes, All Rights Reserved.
628 Copyright 2004 Jose Castro, All Rights Reserved.
629
630 This program is free software; you can redistribute it and/or modify it
631 under the same terms as Perl itself.
632