blib/lib/Lingua/YALI/LanguageIdentifier.pm

filter: Statementstmt 98.2% 56 / 57 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 97.5% 79 / 81 SCAR 22.0
CC
9
Cov
97%
remove_language
4.1
add_language
4.0
get_available_languages
3.0
CRAP 9.0
« blib/lib/Lingua/YALI.pm ↑ Summary bin/yali-builder »
1 package Lingua::YALI::LanguageIdentifier;
2 # ABSTRACT: Module for language identification.
3  
4 94 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 94 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 94 ▶use File::ShareDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 94 ▶use File::Glob;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 94 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 94 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 94 our $VERSION = '0.014_01'; # VERSION
12  
13 94 extends 'Lingua::YALI::Identifier';
14  
15 94 has '_languages' => (is => 'rw', isa => 'ArrayRef');
16 94 has '_language_model' => (is => 'rw', isa => 'HashRef');
17  
18  
19  
20  
21  
22 sub add_language
23 {
24 65 ▶ my ($self, @languages) = @_;
Subroutine add_language: called CC=4 SCAR=13.9
Pod documented
25  
26 # lazy loading
27 65 ▶ if ( ! defined($self->_languages) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_languages) 62 3 65
28 62 $self->get_available_languages();
29 }
30  
31 # register languages
32 65 my $added_languages = 0;
33 65 for my $lang (@languages) {
34 131 ▶ if ( ! defined($self->{_language_model}->{$lang}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_language_model"}{$lang}) 2 129 131
35 2 croak("Unknown language $lang");
36 }
37 129 $added_languages += $self->add_class($lang, $self->{_language_model}->{$lang});
38 }
39  
40 63 return $added_languages;
41 }
42  
43  
44 sub remove_language
45 {
46 7 ▶ my ($self, @languages) = @_;
Subroutine remove_language: called CC=4 SCAR=14.0
Pod documented
47  
48 # lazy loading
49 7 ▶ if ( ! defined($self->_languages) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_languages) 0 7 7
50 0 $self->get_available_languages();
51 }
52  
53 # remove languages
54 7 my $removed_languages = 0;
55 7 for my $lang (@languages) {
56 9 ▶ if ( ! defined($self->{_language_model}->{$lang}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_language_model"}{$lang}) 1 8 9
57 1 croak("Unknown language $lang");
58 }
59 8 $removed_languages += $self->remove_class($lang);
60 }
61  
62 6 return $removed_languages;
63 }
64  
65  
66 sub get_languages
67 {
68 5 ▶ my $self = shift;
Subroutine get_languages: called CC=1 SCAR=0.0
Pod documented
69 5 return $self->get_classes();
70 }
71  
72  
73 sub get_available_languages
74 {
75 72 ▶ my $self = shift;
Subroutine get_available_languages: called CC=3 SCAR=11.0
Pod documented
76  
77 # Get a module's shared files directory
78 72 ▶ if ( ! defined($self->_languages) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_languages) 71 1 72
79  
80 71 my $dir = "share/";
81 71 eval { $dir = File::ShareDir::dist_dir('Lingua-YALI'); };
82  
83 71 my @languages = ();
84  
85 71 for my $file (File::Glob::bsd_glob($dir . "/*.yali.gz")) {
86 8662 my $language = $file;
87 8662 $language =~ s/\Q$dir\E.//;
88 8662 $language =~ s/.yali.gz//;
89  
90 8662 push(@languages, $language);
91 8662 $self->{_language_model}->{$language} = $file;
92 }
93 71 $self->_languages(\@languages);
94 # print STDERR join("\t", @languages), "\n";
95 }
96  
97 72 return $self->_languages;
98 }
99  
100  
101  
102  
103 # for lang in `ls lib/auto/Lingua/YALI/ | cut -f1 -d.`; do name=`webAPI.sh GET $lang name | cut -f3-`; echo -e "=item * $lang - $name\n"; done
104  
105  
106  
107  
108 94 1;
109  
110 __END__
111  
112 =pod
113  
114 =encoding UTF-8
115  
116 =head1 NAME
117  
118 Lingua::YALI::LanguageIdentifier - Module for language identification.
119  
120 =head1 VERSION
121  
122 version 0.014_01
123  
124 =head1 SYNOPSIS
125  
126 This modul is for language identification and can identify 122 languages.
127  
128 use Lingua::YALI::LanguageIdentifier;
129  
130 # create identifier and register languages
131 my $identifier = Lingua::YALI::LanguageIdentifier->new();
132 $identifier->add_language("ces", "eng")
133  
134 # identify string
135 my $result = $identifier->identify_string("CPAN, the Comprehensive Perl Archive Network, is an archive of modules written in Perl.");
136 print "The most probable language is " . $result->[0]->[0] . ".\n";
137 # prints out The most probable language is eng.
138  
139 More examples is presented in L<Lingua::YALI::Examples|Lingua::YALI::Examples>.
140  
141 =head1 METHODS
142  
143 =head2 add_language
144  
145 my $added_languages = $identifier->add_languages(@languages)
146  
147 Registers new languages C<@languages> for identification and returns
148 the amount of newly added languages. Languages are identified by their
149 ISO 639-3 code.
150  
151 It croaks when unsupported language is used.
152  
153 print $identifier->add_languages("ces", "deu", "eng") . "\n";
154 # prints out 3
155 print $identifier->add_languages("ces", "slk") . "\n";
156 # prints out 1
157  
158 =head2 remove_language
159  
160 my $removed_languages = $identifier->remove_languages(@languages)
161  
162 Remove languages C<@languages> and returns the amount of removed languages.
163  
164 It croaks when unsupported language is used.
165  
166 print $identifier->add_languages("ces", "deu", "eng")
167 # prints out 3
168 print $identifier->remove_languages("ces", "slk") . "\n";
169 # prints out 1
170 print $identifier->remove_languages("ces", "slk") . "\n";
171 # prints out 0
172  
173 =head2 get_languages
174  
175 my \@languages = $identifier->get_languages();
176  
177 Returns all registered languages.
178  
179 =head2 get_available_languages
180  
181 my \@languages = $identifier->get_available_languages();
182  
183 Returns all available languages. Currently there is 122 languages (L</LANGUAGES>).
184  
185 =head2 identify_file
186  
187 my $result = $identifier->identify_file($file)
188  
189 Identifies language for file C<$file>.
190  
191 For more details look at method L<Lingua::YALI::Identifier/identify_file>.
192  
193 =head2 identify_string
194  
195 my $result = $identifier->identify_string($string)
196  
197 Identifies language for string C<$string>.
198  
199 For more details look at method L<Lingua::YALI::Identifier/identify_string>.
200  
201 =head2 identify_handle
202  
203 my $result = $identifier->identify_handle($fh)
204  
205 Identifies language for handle C<$fh>.
206  
207 For more details look at method L<Lingua::YALI::Identifier/identify_handle>.
208  
209 =head1 LANGUAGES
210  
211 More details about supported languages may be found at L<http://ufal.mff.cuni.cz/~majlis/w2c/download.html>.
212  
213 =over
214  
215 =item * afr - Afrikaans
216  
217 =item * als - Tosk Albanian
218  
219 =item * amh - Amharic
220  
221 =item * ara - Arabic
222  
223 =item * arg - Aragonese
224  
225 =item * arz - Egyptian Arabic
226  
227 =item * ast - Asturian
228  
229 =item * aze - Azerbaijani
230  
231 =item * bcl - Central Bicolano
232  
233 =item * bel - Belarusian
234  
235 =item * ben - Bengali
236  
237 =item * bos - Bosnian
238  
239 =item * bpy - Bishnupriya
240  
241 =item * bre - Breton
242  
243 =item * bug - Buginese
244  
245 =item * bul - Bulgarian
246  
247 =item * cat - Catalan
248  
249 =item * ceb - Cebuano
250  
251 =item * ces - Czech
252  
253 =item * chv - Chuvash
254  
255 =item * cos - Corsican
256  
257 =item * cym - Welsh
258  
259 =item * dan - Danish
260  
261 =item * deu - German
262  
263 =item * diq - Dimli (individual language)
264  
265 =item * ell - Modern Greek (1453-)
266  
267 =item * eng - English
268  
269 =item * epo - Esperanto
270  
271 =item * est - Estonian
272  
273 =item * eus - Basque
274  
275 =item * fao - Faroese
276  
277 =item * fas - Persian
278  
279 =item * fin - Finnish
280  
281 =item * fra - French
282  
283 =item * fry - Western Frisian
284  
285 =item * gan - Gan Chinese
286  
287 =item * gla - Scottish Gaelic
288  
289 =item * gle - Irish
290  
291 =item * glg - Galician
292  
293 =item * glk - Gilaki
294  
295 =item * guj - Gujarati
296  
297 =item * hat - Haitian
298  
299 =item * hbs - Serbo-Croatian
300  
301 =item * heb - Hebrew
302  
303 =item * hif - Fiji Hindi
304  
305 =item * hin - Hindi
306  
307 =item * hrv - Croatian
308  
309 =item * hsb - Upper Sorbian
310  
311 =item * hun - Hungarian
312  
313 =item * hye - Armenian
314  
315 =item * ido - Ido
316  
317 =item * ina - Interlingua (International Auxiliary Language Association)
318  
319 =item * ind - Indonesian
320  
321 =item * isl - Icelandic
322  
323 =item * ita - Italian
324  
325 =item * jav - Javanese
326  
327 =item * jpn - Japanese
328  
329 =item * kan - Kannada
330  
331 =item * kat - Georgian
332  
333 =item * kaz - Kazakh
334  
335 =item * kor - Korean
336  
337 =item * kur - Kurdish
338  
339 =item * lat - Latin
340  
341 =item * lav - Latvian
342  
343 =item * lim - Limburgan
344  
345 =item * lit - Lithuanian
346  
347 =item * lmo - Lombard
348  
349 =item * ltz - Luxembourgish
350  
351 =item * mal - Malayalam
352  
353 =item * mar - Marathi
354  
355 =item * mkd - Macedonian
356  
357 =item * mlg - Malagasy
358  
359 =item * mon - Mongolian
360  
361 =item * mri - Maori
362  
363 =item * msa - Malay (macrolanguage)
364  
365 =item * mya - Burmese
366  
367 =item * nap - Neapolitan
368  
369 =item * nds - Low German
370  
371 =item * nep - Nepali
372  
373 =item * new - Newari
374  
375 =item * nld - Dutch
376  
377 =item * nno - Norwegian Nynorsk
378  
379 =item * nor - Norwegian
380  
381 =item * oci - Occitan (post 1500)
382  
383 =item * oss - Ossetian
384  
385 =item * pam - Pampanga
386  
387 =item * pms - Piemontese
388  
389 =item * pnb - Western Panjabi
390  
391 =item * pol - Polish
392  
393 =item * por - Portuguese
394  
395 =item * que - Quechua
396  
397 =item * ron - Romanian
398  
399 =item * rus - Russian
400  
401 =item * sah - Yakut
402  
403 =item * scn - Sicilian
404  
405 =item * sco - Scots
406  
407 =item * slk - Slovak
408  
409 =item * slv - Slovenian
410  
411 =item * spa - Spanish
412  
413 =item * sqi - Albanian
414  
415 =item * srp - Serbian
416  
417 =item * sun - Sundanese
418  
419 =item * swa - Swahili (macrolanguage)
420  
421 =item * swe - Swedish
422  
423 =item * tam - Tamil
424  
425 =item * tat - Tatar
426  
427 =item * tel - Telugu
428  
429 =item * tgk - Tajik
430  
431 =item * tgl - Tagalog
432  
433 =item * tha - Thai
434  
435 =item * tur - Turkish
436  
437 =item * ukr - Ukrainian
438  
439 =item * urd - Urdu
440  
441 =item * uzb - Uzbek
442  
443 =item * vec - Venetian
444  
445 =item * vie - Vietnamese
446  
447 =item * vol - Volapük
448  
449 =item * war - Waray (Philippines)
450  
451 =item * wln - Walloon
452  
453 =item * yid - Yiddish
454  
455 =item * yor - Yoruba
456  
457 =item * zho - Chinese
458  
459 =back
460  
461 =head1 SEE ALSO
462  
463 =over
464  
465 =item * Identifier for own models is L<Lingua::YALI::Identifier|Lingua::YALI::Identifier>.
466  
467 =item * There is also command line tool L<yali-language-identifier|Lingua::YALI::yali-language-identifier> with similar functionality.
468  
469 =item * Source codes are available at L<https://github.com/martin-majlis/YALI>.
470  
471 =back
472  
473 =head1 AUTHOR
474  
475 Martin Majlis <martin@majlis.cz>
476  
477 =head1 COPYRIGHT AND LICENSE
478  
479 This software is Copyright (c) 2012 by Martin Majlis.
480  
481 This is free software, licensed under:
482  
483 The (three-clause) BSD License
484  
485 =cut
« blib/lib/Lingua/YALI.pm ↑ Summary bin/yali-builder »