blib/lib/Lingua/YALI/Identifier.pm

filter: Statementstmt 95.0% 134 / 141 Branchbran 80.4% 37 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 92.4% 195 / 211 SCAR 33.9
CC
29
Cov
91%
identify_handle
13.2
_load_model
8.0
add_class
4.0
CRAP 29.5
↑ Summary blib/lib/Lingua/YALI/Builder.pm »
1 package Lingua::YALI::Identifier;
2 # ABSTRACT: Module for language identification with custom models.
3  
4 173 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 173 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 173 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 173 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 173 ▶use PerlIO::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 173 ▶use Lingua::YALI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 173 our $VERSION = '0.015'; # VERSION
12  
13 # hash with paths to models
14 # format: { 'class1' => 'file1', 'class2' => 'file2' }
15 173 has '_model_file' => (
16 is => 'rw',
17 isa => 'HashRef'
18 );
19  
20 # hash with n-gram frequencies retrieved from models
21 # format: { 'ngram1' => { 'class1' => 0.5, 'class2' => 0.3}, 'ngram2' => ...}
22 173 has '_frequency' => (
23 is => 'rw',
24 isa => 'HashRef'
25 );
26  
27 # n-gram size
28 173 has '_ngram' => (
29 is => 'rw',
30 isa => 'Int'
31 );
32  
33 # list of identified classes
34 173 has '_classes' => (
35 is => 'rw',
36 isa => 'ArrayRef'
37 );
38  
39  
40  
41 sub BUILD
42 {
43 139 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
44 139 my %frequency = ();
45 139 my @classes = ();
46 139 $self->{_frequency} = \%frequency;
47 139 $self->{_classes} = \@classes;
48  
49 139 return;
50 }
51  
52  
53 sub add_class
54 {
55 259 ▶ my ( $self, $class, $file ) = @_;
Subroutine add_class: called CC=4 SCAR=13.9
Pod documented
56  
57 259 ▶ if ( defined( $self->{_model_file}->{$class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_model_file"}{$class}) 5 254 259
58 5 return 0;
59 }
60  
61 # parameter check
62 254 ▶ if ( ! defined($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 253 254
63 1 croak("Model has to be specified.");
64 }
65  
66 253 ▶ if ( ! -r $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $file) 1 252 253
67 1 croak("Model $file is not readable.");
68 }
69  
70 252 $self->_load_model($class, $file);
71  
72 251 return 1;
73 }
74  
75  
76 sub remove_class
77 {
78 17 ▶ my ( $self, $class ) = @_;
Subroutine remove_class: called CC=2 SCAR=6.9
Pod documented
79  
80 17 ▶ if ( defined( $self->{_model_file}->{$class} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_model_file"}{$class}) 11 6 17
81 11 $self->_unload_model($class);
82  
83 11 return 1;
84 }
85  
86 6 return 0;
87 }
88  
89  
90 sub get_classes
91 {
92 246 ▶ my $self = shift;
Subroutine get_classes: called CC=1 SCAR=0.0
Pod documented
93 246 return $self->{_classes};
94 }
95  
96  
97 sub identify_file
98 {
99 10 ▶ my ( $self, $file ) = @_;
Subroutine identify_file: called CC=2 SCAR=7.4
Pod documented
100  
101 10 ▶ if ( ! defined($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 10 10
102 0 return;
103 }
104  
105 10 my $fh = Lingua::YALI::_open($file);
106  
107 10 return $self->identify_handle($fh);
108 }
109  
110  
111 sub identify_string
112 {
113 131 ▶ my ( $self, $string ) = @_;
Subroutine identify_string: called CC=3 SCAR=11.6
Pod documented
114 131 ▶ open(my $fh, "<", \$string) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", \$string or croak($!) 0 131 131
115  
116 131 ▶ if ( ! defined($string) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 0 131 131
117 0 return;
118 }
119  
120 131 my $result = $self->identify_handle($fh);
121  
122 131 close($fh);
123  
124 131 return $result;
125 }
126  
127  
128 sub identify_handle
129 {
130 223 ▶ my ($self, $fh) = @_;
Subroutine identify_handle: called CC=13 SCAR=25.8
Pod documented
131 223 my %actRes = ();
132  
133 # parameter check
134 223 my $ngram = $self->{_ngram};
135 223 ▶ if ( ! defined($ngram) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ngram) 1 222 223
136 1 croak("At least one class must be specified.");
137 }
138  
139 222 ▶ if ( ! defined($fh) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $fh) { } 0 222 222
elsif (ref $fh ne 'GLOB') { } 0 222 222
140 0 return;
141 } elsif ( ref $fh ne "GLOB" ) {
142 0 croak("Expected file handler but " . (ref $fh) . " was used.");
143 }
144  
145 # read input file
146 222 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 387 222 609
147 387 chomp;
148 387 s/ +/ /g;
149 387 s/^ +//g;
150 387 s/ +$//g;
151 387 ▶ if ( ! $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_) 0 387 387
152 0 next;
153 }
154  
155 # $_ = $padding . $_ . $padding;
156  
157 {
158 173 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 387 for my $i (0 .. bytes::length($_) - $ngram) {
160 154958 my $w = substr($_, $i, $ngram);
161  
162 154958 ▶ if ( defined($self->{_frequency}->{$w}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_frequency"}{$w}) 73118 81840 154958
163 73118 for my $lang (keys %{$self->{_frequency}->{$w}}) {
164 # print STDERR "$w - $lang - $frequency{$w}{$lang}\n";
165 77421 $actRes{$lang} += $self->{_frequency}->{$w}{$lang};
166 # print STDERR "Lang: $lang - $actRes{$lang}\n";
167 }
168 }
169 }
170 }
171 }
172  
173 # sum scores of all classifiers
174 222 my @allLanguages = @ { $self->get_classes() };
175  
176 222 my $sum = 0;
177 222 for my $l (@allLanguages) {
178 441 my $score = 0;
179 441 ▶ if ( defined($actRes{$l}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $actRes{$l}) 302 139 441
180 302 $score = $actRes{$l};
181 }
182 441 $sum += $score;
183 }
184  
185 # normalize results
186 222 my @res = ();
187  
188 222 for my $l (@allLanguages) {
189 441 my $score = 0;
190 441 ▶ if ( defined($actRes{$l}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $actRes{$l}) 302 139 441
191 302 $score = $actRes{$l} / $sum;
192 }
193 441 my @pair = ($l, $score);
194 441 push(@res, \@pair);
195 }
196  
197 # sort according to score
198 222 my @sortedRes = sort { $b->[1] <=> $a->[1] } @res;
199  
200 222 return \@sortedRes;
201 }
202  
203 # recompute classes after manipulation with classes
204 sub _compute_classes
205 {
206 262 ▶ my $self = shift;
Subroutine _compute_classes: called CC=1 SCAR=0.0
207 262 my @classes = keys %{ $self->{_model_file} };
208  
209 262 $self->{_classes} = \@classes;
210  
211 262 return;
212 }
213  
214 # load model
215 sub _load_model
216 {
217 252 ▶ my ($self, $class, $file) = @_;
Subroutine _load_model: called CC=8 SCAR=20.8
218  
219 252 ▶ if ( $self->{_model_file}->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_model_file"}{$class}) 0 252 252
220 0 return;
221 }
222  
223 252 ▶ open(my $fh, "<:gzip:bytes", $file) or croak($!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:gzip:bytes", $file or croak($!) 0 252 252
224 252 my $ngram = <$fh>;
225 252 my $total_line = <$fh>;
226  
227 252 ▶ if ( ! defined($self->{_ngram}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'_ngram'}) { } 125 127 252
228 125 $self->{_ngram} = $ngram;
229 } else {
230 127 ▶ if ( $ngram != $self->{_ngram} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ngram != $self->{"_ngram"}) 1 126 127
231 1 croak("Incompatible model for '$class'. Expected $self->{_ngram}-grams, but was $ngram-gram.");
232 }
233 }
234  
235 251 my $sum = 0;
236 251 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 201731 251 201982
237 201731 chomp;
238 201731 my @p = split(/\t/, $_);
239 201731 my $word = $p[0];
240 201731 $self->{_frequency}->{$word}{$class} = $p[1];
241 201731 $sum += $p[1];
242 }
243  
244 251 for my $word (keys %{$self->{_frequency}}) {
245 299066 ▶ if ( defined($self->{_frequency}->{$word}{$class}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_frequency"}{$word}{$class}) 201734 97332 299066
246 201734 $self->{_frequency}->{$word}{$class} /= $sum;
247 }
248 }
249  
250 251 close($fh);
251  
252 251 $self->{_model_file}->{$class} = $file;
253 251 $self->_compute_classes();
254  
255 251 return;
256 }
257  
258 # unload model
259 sub _unload_model
260 {
261 11 ▶ my ($self, $class) = @_;
Subroutine _unload_model: called CC=3 SCAR=11.1
262  
263 11 ▶ if ( ! $self->{_model_file}->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_model_file"}{$class}) 0 11 11
264 0 return;
265 }
266  
267 11 delete( $self->{_model_file}->{$class} );
268 11 $self->_compute_classes();
269  
270 11 my $classes = $self->get_classes();
271 # print STDERR "\nX=removing $class\n" . (join("\t", @$classes)) . "\n" . (scalar @$classes) . "\nX\n";
272 11 ▶ if ( scalar @$classes == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$classes == 0) 4 7 11
273 4 delete($self->{_ngram});
274 4 $self->{_ngram} = undef;
275 }
276  
277  
278  
279  
280 11 return;
281 }
282  
283  
284 173 1;
285  
286 __END__
287  
288 =pod
289  
290 =encoding UTF-8
291  
292 =head1 NAME
293  
294 Lingua::YALI::Identifier - Module for language identification with custom models.
295  
296 =head1 VERSION
297  
298 version 0.015
299  
300 =head1 SYNOPSIS
301  
302 This modul identify languages with moduls provided by the user. If you want to use pretrained models use L<Lingua::YALI::LanguageIdentifier|Lingua::YALI::LanguageIdentifier>.
303  
304 Models trained on texts from specific domain outperforms the general ones.
305  
306 use Lingua::YALI::Builder;
307 use Lingua::YALI::Identifier;
308  
309 # create models
310 my $builder_a = Lingua::YALI::Builder->new(ngrams=>[2]);
311 $builder_a->train_string("aaaaa aaaa aaa aaa aaa aaaaa aa");
312 $builder_a->store("model_a.2_all.gz", 2);
313  
314 my $builder_b = Lingua::YALI::Builder->new(ngrams=>[2]);
315 $builder_b->train_string("bbbbbb bbbb bbbb bbb bbbb bbbb bbb");
316 $builder_b->store("model_b.2_all.gz", 2);
317  
318 # create identifier and load models
319 my $identifier = Lingua::YALI::Identifier->new();
320 $identifier->add_class("a", "model_a.2_all.gz");
321 $identifier->add_class("b", "model_b.2_all.gz");
322  
323 # identify strings
324 my $result1 = $identifier->identify_string("aaaaaaaaaaaaaaaaaaa");
325 print $result1->[0]->[0] . "\t" . $result1->[0]->[1];
326 # prints out a 1
327  
328 my $result2 = $identifier->identify_string("bbbbbbbbbbbbbbbbbbb");
329 print $result2->[0]->[0] . "\t" . $result2->[0]->[1];
330 # prints out b 1
331  
332 More examples is presented in L<Lingua::YALI::Examples|Lingua::YALI::Examples>.
333  
334 =head1 METHODS
335  
336 =head2 BUILD
337  
338 Initializes internal variables.
339  
340 # create identifier
341 my $identifier = Lingua::YALI::Identifier->new();
342  
343 =head2 add_class
344  
345 $added = $identifier->add_class($class, $model)
346  
347 Adds model stored in file C<$model> with class C<$class> and
348 returns whether it was added or not.
349  
350 print $identifier->add_class("a", "model.a1.gz") . "\n";
351 # prints out 1
352 print $identifier->add_class("a", "model.a2.gz") . "\n";
353 # prints out 0 - class a was already added
354  
355 =head2 remove_class
356  
357 my $removed = $identifier->remove_class($class);
358  
359 Removes model for class C<$class>.
360  
361 $identifier->add_class("a", "model.a1.gz");
362 print $identifier->remove_class("a") . "\n";
363 # prints out 1
364 print $identifier->remove_class("a") . "\n";
365 # prints out 0 - class a was already removed
366  
367 =head2 get_classes
368  
369 my \@classes = $identifier->get_classes();
370  
371 Returns all registered classes.
372  
373 =head2 identify_file
374  
375 my $result = $identifier->identify_file($file)
376  
377 Identifies class for file C<$file>.
378  
379 =over
380  
381 =item * It returns undef if C<$file> is undef.
382  
383 =item * It croaks if the file C<$file> does not exist or is not readable.
384  
385 =item * Otherwise look for more details at method L</identify_handle>.
386  
387 =back
388  
389 =head2 identify_string
390  
391 my $result = $identifier->identify_string($string)
392  
393 Identifies class for string C<$string>.
394  
395 =over
396  
397 =item * It returns undef if C<$string> is undef.
398  
399 =item * Otherwise look for more details at method L</identify_handle>.
400  
401 =back
402  
403 =head2 identify_handle
404  
405 my $result = $identifier->identify_handle($fh)
406  
407 Identifies class for file handle C<$fh> and returns:
408  
409 =over
410  
411 =item * It returns undef if C<$fh> is undef.
412  
413 =item * It croaks if the C<$fh> is not file handle.
414  
415 =item * It returns array reference in format [ ['class1', score1], ['class2', score2], ...] sorted
416 according to score descendently, so the most probable class is the first.
417  
418 =back
419  
420 =head1 SEE ALSO
421  
422 =over
423  
424 =item * Identifier with pretrained models for language identification is L<Lingua::YALI::LanguageIdentifier|Lingua::YALI::LanguageIdentifier>.
425  
426 =item * Builder for these models is L<Lingua::YALI::Builder|Lingua::YALI::Builder>.
427  
428 =item * There is also command line tool L<yali-identifier|Lingua::YALI::yali-identifier> with similar functionality.
429  
430 =item * Source codes are available at L<https://github.com/martin-majlis/YALI>.
431  
432 =back
433  
434 =head1 AUTHOR
435  
436 Martin Majlis <martin@majlis.cz>
437  
438 =head1 COPYRIGHT AND LICENSE
439  
440 This software is Copyright (c) 2012 by Martin Majlis.
441  
442 This is free software, licensed under:
443  
444 The (three-clause) BSD License
445  
446 =head1 AUTHOR
447  
448 Martin Majlis <martin@majlis.cz>
449  
450 =head1 COPYRIGHT AND LICENSE
451  
452 This software is Copyright (c) 2012 by Martin Majlis.
453  
454 This is free software, licensed under:
455  
456 The (three-clause) BSD License
457  
458 =cut
↑ Summary blib/lib/Lingua/YALI/Builder.pm »