blib/lib/Lingua/YALI/Builder.pm

filter: Statementstmt 97.3% 111 / 114 Branchbran 87.5% 28 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.7% 18 / 19 Podpod 100% 7 / 7 totaltotal 95.3% 164 / 172 SCAR 30.0
CC
20
Cov
95%
store
11.0
train_handle
7.0
train_string
3.0
CRAP 20.0
« blib/lib/Lingua/YALI/Identifier.pm ↑ Summary blib/lib/Lingua/YALI.pm »
1 package Lingua::YALI::Builder;
2 # ABSTRACT: Constructs language models for language identification.
3  
4 65 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 65 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 65 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 65 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 65 ▶use Lingua::YALI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 65 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 65 ▶use List::MoreUtils qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 65 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 65 our $VERSION = '0.014_01'; # VERSION
14  
15  
16 subtype 'PositiveInt',
17 as 'Int',
18 70 ▶ where { $_ > 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 0 ▶ message { "The number you provided, $_, was not a positive number" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21  
22 # list of all n-gram sizes that will be used during training
23 65 has 'ngrams' => (
24 is => 'ro',
25 isa => 'ArrayRef[PositiveInt]',
26 required => 1
27 );
28  
29 # the greatest n-gram size
30 # i.e. ngrams = [1, 2, 3]; _max_ngram = 3
31 65 has '_max_ngram' => (
32 is => 'rw',
33 isa => 'Int'
34 );
35  
36 # hash of all n-grams
37 # After procissing string 'ab' and n-grams set to [ 1, 2]:
38 # _dict => { 1 => { 'a' => 1, 'b' => 1}; 2 => { 'ab' => 1 } }
39 65 has '_dict' => (
40 is => 'rw',
41 isa => 'HashRef'
42 );
43  
44  
45 sub BUILD
46 {
47 42 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
48  
49 # keep only unique n-gram sizes
50 42 my @unique = uniq( @{$self->{ngrams}} );
51 42 my @sorted = sort { $a <=> $b } @unique;
52 42 $self->{ngrams} = \@sorted;
53  
54 # select the greatest n-gram
55 42 $self->{_max_ngram} = $sorted[-1];
56  
57 42 return;
58 }
59  
60  
61  
62 sub get_ngrams
63 {
64 2 ▶ my $self = shift;
Subroutine get_ngrams: called CC=1 SCAR=0.0
Pod documented
65 2 return $self->ngrams;
66 }
67  
68  
69 sub get_max_ngram
70 {
71 53 ▶ my $self = shift;
Subroutine get_max_ngram: called CC=1 SCAR=0.0
Pod documented
72 53 return $self->{_max_ngram};
73 }
74  
75  
76 sub train_file
77 {
78 13 ▶ my ( $self, $file ) = @_;
Subroutine train_file: called CC=2 SCAR=6.9
Pod documented
79  
80 # parameter check
81 13 ▶ if ( ! defined($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 12 13
82 1 return;
83 }
84  
85 12 my $fh = Lingua::YALI::_open($file);
86  
87 11 return $self->train_handle($fh);
88 }
89  
90  
91 sub train_string
92 {
93 3 ▶ my ( $self, $string ) = @_;
Subroutine train_string: called CC=3 SCAR=11.0
Pod documented
94  
95 # parameter check
96 3 ▶ if ( ! defined($string) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 1 2 3
97 1 return;
98 }
99  
100 2 ▶ open(my $fh, "<", \$string) or croak $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", \$string or croak($!) 0 2 2
101  
102 2 my $result = $self->train_handle($fh);
103  
104 2 close($fh);
105  
106 2 return $result;
107 }
108  
109  
110 sub train_handle
111 {
112 42 ▶ my ($self, $fh) = @_;
Subroutine train_handle: called CC=7 SCAR=19.5
Pod documented
113  
114 # print STDERR "\nX\n" . (ref $fh) . "\nX\n";
115  
116 # parameter check
117 42 ▶ if ( ! defined($fh) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $fh) { } 1 41 42
elsif (ref $fh ne 'GLOB') { } 1 40 41
118 1 return;
119 } elsif ( ref $fh ne "GLOB" ) {
120 1 croak("Expected file handler but " . (ref $fh) . " was used.");
121 }
122  
123 # my $padding = $self->{_padding};
124 40 my @ngrams = @{$self->ngrams};
125 40 my $padding = "";
126 40 my $subsub = "";
127 40 my $sub = "";
128  
129 40 my $total_length = 0;
130  
131 40 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 132 40 172
132 132 chomp;
133 132 s/ +/ /g;
134 132 s/^ +//g;
135 132 s/ +$//g;
136 132 ▶ if ( ! $_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_) 0 132 132
137 0 next;
138 }
139  
140 132 $_ = $padding . $_ . $padding;
141  
142 {
143 65 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 132 my $act_length = bytes::length($_);
146 132 $total_length += $act_length;
147  
148 132 for my $i (0 .. $act_length - $self->{_max_ngram}) {
149 10104 $sub = substr($_, $i, $self->{_max_ngram});
150 10104 for my $j (@ngrams) {
151 14584 $subsub = bytes::substr($sub, 0, $j);
152 # if ( $subsub =~ /[[:digit:][:punct:]]/ ) {
153 # next;
154 # }
155  
156 14584 $self->{_dict}->{$j}{$subsub}++;
157 14584 $self->{_dict}->{$j}{___total___}++;
158 }
159 }
160 }
161 }
162  
163 40 return $total_length;
164 }
165  
166  
167 sub store
168 {
169 69 ▶ my ($self, $file, $ngram, $count) = @_;
Subroutine store: called CC=11 SCAR=24.0
Pod documented
170  
171 # parameter check
172 69 ▶ if ( ! defined($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 2 67 69
173 2 croak("parametr file has to be specified");
174 }
175  
176 # if ( -f $file && ! -w $file ) {
177 # croak("file $file has to be writeable");
178 # }
179  
180 67 ▶ if ( ! defined($ngram) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ngram) 1 66 67
181 1 croak("parametr ngram has to be specified");
182 }
183  
184 66 ▶ if ( ! defined($self->{_dict}->{$ngram}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_dict"}{$ngram}) 13 53 66
185 13 croak("$ngram-grams were not counted.");
186 }
187  
188 53 ▶ if ( ! defined($count) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 17 36 53
189 17 $count = POSIX::INT_MAX;
190 }
191  
192 53 ▶ if ( $count < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 1) 2 51 53
193 2 croak("At least one n-gram has to be saved. Count was set to: $count");
194 }
195  
196 51 ▶ if ( ! defined($self->{_dict}->{$self->get_max_ngram()}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"_dict"}{$self->get_max_ngram}) 0 51 51
197 0 croak("No training data was used.");
198 }
199  
200 # open file
201 51 ▶ open(my $fhModel, ">:gzip:bytes", $file) or croak($!);
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $fhModel, ">:gzip:bytes", $file or croak($!) 0 51 51
202  
203 # prints out n-gram size
204 51 print $fhModel $ngram . "\n";
205  
206 # store n-grams
207 51 my $i = 0;
208 51 for my $k (sort {
209 286 ▶ $self->{_dict}->{$ngram}{$b} <=> $self->{_dict}->{$ngram}{$a}
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_dict"}{$ngram}{$b} <=> $self->{"_dict"}{$ngram}{$a} or $a cmp $b 106 180 286
210 ||
211 $a cmp $b
212 51 } keys %{$self->{_dict}->{$ngram}}) {
213 200 print $fhModel "$k\t$self->{_dict}->{$ngram}{$k}\n";
214 200 ▶ if ( ++$i > $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i > $count) 9 191 200
215 9 last;
216 }
217 }
218  
219  
220 51 close($fhModel);
221  
222 51 return ($i - 1);
223 }
224  
225  
226 65 1;
227  
228 __END__
229  
230 =pod
231  
232 =encoding UTF-8
233  
234 =head1 NAME
235  
236 Lingua::YALI::Builder - Constructs language models for language identification.
237  
238 =head1 VERSION
239  
240 version 0.014_01
241  
242 =head1 SYNOPSIS
243  
244 This modul creates models for L<Lingua::YALI::Identifier|Lingua::YALI::Identifier>.
245  
246 If your texts are from specific domain you can achive better
247 results when your models will be trained on texts from the same domain.
248  
249 Creating bigram and trigram models from a string.
250  
251 use Lingua::YALI::Builder;
252 my $builder = Lingua::YALI::Builder->new(ngrams=>[2, 3]);
253 $builder->train_string("aaaaa aaaa aaa aaa aaa aaaaa aa");
254 $builder->train_string("aa aaaaaa aa aaaaa aaaaaa aaaaa");
255 $builder->store("model_a.2_4.gz", 2, 4);
256 $builder->store("model_a.2_all.gz", 2);
257 $builder->store("model_a.3_all.gz", 3);
258 $builder->store("model_a.4_all.gz", 4);
259 # croaks because 4-grams were not trained
260  
261 More examples is presented in L<Lingua::YALI::Examples|Lingua::YALI::Examples>.
262  
263 =head1 METHODS
264  
265 =head2 BUILD
266  
267 BUILD()
268  
269 Constructs C<Builder>.
270  
271 my $builder = Lingua::YALI::Builder->new(ngrams=>[2, 3, 4]);
272  
273 =head2 get_ngrams
274  
275 my \@ngrams = $builder->get_ngrams()
276  
277 Returns all n-grams that will be used during training.
278  
279 my $builder = Lingua::YALI::Builder->new(ngrams=>[2, 3, 4, 2, 3]);
280 my $ngrams = $builder->get_ngrams();
281 print join(", ", @$ngrams) . "\n";
282 # prints out 2, 3, 4
283  
284 =head2 get_max_ngram
285  
286 my $max_ngram = $builder->get_max_ngram()
287  
288 Returns the highest n-gram size that will be used during training.
289  
290 my $builder = Lingua::YALI::Builder->new(ngrams=>[2, 3, 4]);
291 print $builder->get_max_ngram() . "\n";
292 # prints out 4
293  
294 =head2 train_file
295  
296 my $used_bytes = $builder->train_file($file)
297  
298 Uses file C<$file> for training and returns the amount of bytes used.
299  
300 =over
301  
302 =item * It returns undef if C<$file> is undef.
303  
304 =item * It croaks if the file C<$file> does not exist or is not readable.
305  
306 =item * It returns the amount of bytes used for trainig otherwise.
307  
308 =back
309  
310 For more details look at method L</train_handle>.
311  
312 =head2 train_string
313  
314 my $used_bytes = $builder->train_string($string)
315  
316 Uses string C<$string> for training and returns the amount of bytes used.
317  
318 =over
319  
320 =item * It returns undef if C<$string> is undef.
321  
322 =item * It returns the amount of bytes used for trainig otherwise.
323  
324 =back
325  
326 For more details look at method L</train_handle>.
327  
328 =head2 train_handle
329  
330 my $used_bytes = $builder->train_handle($fh)
331  
332 Uses file handle C<$fh> for training and returns the amount of bytes used.
333  
334 =over
335  
336 =item * It returns undef if C<$fh> is undef.
337  
338 =item * It croaks if the C<$fh> is not file handle.
339  
340 =item * It returns the amount of bytes used for trainig otherwise.
341  
342 =back
343  
344 =head2 store
345  
346 my $stored_count = $builder->store($file, $ngram, $count)
347  
348 Stores trained model with at most C<$count> C<$ngram>-grams to file C<$file>.
349 If count is not specified all C<$ngram>-grams are stored.
350  
351 =over
352  
353 =item * It croaks if incorrect parameters are passed or it was not trained.
354  
355 =item * It returns the amount of stored n-grams.
356  
357 =back
358  
359 =head1 SEE ALSO
360  
361 =over
362  
363 =item * Trained models are suitable for L<Lingua::YALI::Identifier|Lingua::YALI::Identifier>.
364  
365 =item * There is also command line tool L<yali-builder|Lingua::YALI::yali-builder> with similar functionality.
366  
367 =item * Source codes are available at L<https://github.com/martin-majlis/YALI>.
368  
369 =back
370  
371 =head1 AUTHOR
372  
373 Martin Majlis <martin@majlis.cz>
374  
375 =head1 COPYRIGHT AND LICENSE
376  
377 This software is Copyright (c) 2012 by Martin Majlis.
378  
379 This is free software, licensed under:
380  
381 The (three-clause) BSD License
382  
383 =cut
« blib/lib/Lingua/YALI/Identifier.pm ↑ Summary blib/lib/Lingua/YALI.pm »