lib/AI/MicroStructure/Categorizer.pm untested

filter: Statementstmt 0.0% 0 / 164 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 232 CC 13 SCAR 21.8
CC
13
Cov
0%
getTestDocs
30.0
getBookList
12.0
analyseBookNames
12.0
CRAP 8.8
1 #!/usr/bin/perl -W
2  
3 package AI::MicroStructure::Categorizer;
4 use strict;
5 use warnings;
6 use Digest::MD5 qw(md5 md5_hex md5_base64);
7 use File::Spec;
8 use Data::Dumper;
9  
10 use Cache::Memcached::Fast;
11 require AI::Categorizer;
12 require AI::Categorizer::Learner::NaiveBayes;
13 require AI::Categorizer::Document;
14 require AI::Categorizer::KnowledgeSet;
15 require Lingua::StopWords;
16  
17 sub new {
18 my $this = shift;
19 my $class = ref($this) || $this;
20 my $self = {};
21 bless $self, $class;
22 $self->initialize(@_);
23 return $self;
24 }
25  
26 sub initialize {
27 my $self = shift;
28 %$self=@_;
29  
30 $self->{cache} = new Cache::Memcached::Fast({
31 servers => [ { address => $config->{memcached},
32 weight => 2.5 }],
33 namespace => 'my:',
34 connect_timeout => 0.2,
35 io_timeout => 0.5,
36 close_on_error => 1,
37 compress_threshold => 100_000,
38 compress_ratio => 0.9,
39 compress_methods =>
40 [ \&IO::Compress::Gzip::gzip,
41 \&IO::Uncompress::Gunzip::gunzip ],
42 max_failures => 3,
43 failure_timeout => 2,
44 ketama_points => 150,
45 nowait => 1,
46 hash_namespace => 1,
47 serialize_methods => [ \&Storable::freeze,
48 \&Storable::thaw ],
49 utf8 => ($^V ge v5.8.1 ? 1 : 0),
50 max_size => 4*512 * 1024,
51 });
52  
53  
54  
55 }
56  
57  
58 sub trim
59 {
60 my $self = shift;
61 my $string = shift;
62 $string = "" unless $string;
63 $string =~ s/^\s+//;
64 $string =~ s/\s+$//;
65 $string =~ s/\t//;
66 $string =~ s/^\s//;
67 return $string;
68 }
69  
70  
71 sub catfile{
72 my $self = shift;
73 my $path = sprintf("%s/%s",$self->{bookpath},
74 shift);
75 # print $path;
76 my $cat = {};
77 my @cat = map{
78 my @x = split(":",$_);
79 $_ = $self->trim($x[1]);
80 }split("\n",lc
81 `microdict $path | data-freq --limit 500`);
82  
83 $cat->{subject} = [@cat[0..10]];
84 $cat->{body} = [@cat];
85  
86 return $cat;
87  
88 }
89  
90 sub getBookList{
91  
92 my $self = shift;
93 my $dir = shift;
94  
95 $dir = $self->{bookpath} unless defined($dir);
96 die "$dir is not a directory" unless -d $dir;
97 opendir(DIR, $dir) or die $!;
98  
99 my @mp3s = grep { $_ = sprintf("%s",$_); }
100 sort grep /^[\x20-\x7E]+$/,
101 readdir(DIR);
102  
103 closedir DIR;
104  
105 return @mp3s;
106 }
107  
108 sub analyseBookNames{
109  
110 my $self = shift;
111 my $returns = {};
112 my @books = $self->getBookList();
113 my @data = ();
114 my $content = {};
115 my $name = "";
116 my $namehex = "";
117  
118 foreach(@books) {
119 $content = {};
120 $namehex = md5_hex($_);
121  
122 next if($_ eq "." || $_ eq "..");
123  
124 $content = $self->{cache}->get($namehex);
125 $name = sprintf("%s/%s",$self->{bookpath},$_);
126  
127 if(defined($content->{body})){
128  
129 $returns->{$namehex} =
130 {subject => $content->{subject},
131 body => $content->{body},
132 name => $name,
133 md5hex => $namehex};
134  
135 }else{
136  
137 $content = $self->catfile($_);
138  
139 $returns->{$namehex} =
140 {subject=>$content->{subject},
141 body=>$content->{body},
142 name=>$name,
143 md5hex=>$namehex};
144  
145 $self->{cache}->set($namehex,$content);
146  
147 }
148  
149 }
150  
151  
152 return $returns;
153  
154 }
155  
156  
157  
158 sub getFeatures {
159  
160 my $self = shift;
161  
162 my %features =
163 (content_weights => {subject =>2,
164 body => 1},
165 stopwords => Lingua::StopWords::getStopWords('en'),
166 stemming => 'porter');
167  
168  
169 return %features;
170 }
171  
172  
173 sub getTestDocs {
174  
175 my $self = shift;
176 my $amount = shift;
177 $amount = 5 unless($amount);
178  
179 my $test_set = { };
180  
181 my @theme = split("\n",`perl -MAI::MicroStructure -le 'print for AI::MicroStructure->structures;'`);
182 my $add = lc `micro`;
183 foreach(@theme){
184  
185 next if($_ =~ /any|new/);
186  
187 my $name = lc $_;
188 my $sub = "";
189 my $body = "";
190  
191 foreach my $i (0..($amount/2)) {
192 $add = lc `micro $name` ;
193 $sub .= " $add" unless($sub =~ m/$add/);
194 }
195  
196  
197  
198 foreach my $i (0..$amount) {
199 $add = lc `micro $name` ;
200 $body .= " $add" unless($body =~ m/$add/);
201 }
202  
203 $body .= " $name" ;
204 $sub .= " $name" ;
205 $body=~ s/\n/ /g;
206 $sub=~ s/\n/ /g;
207 # $name=~ s/_/ /g;
208 # $sub=~ s/_/ /g;
209 # $body=~ s/_/ /g;
210 $test_set->{$name} = {content =>{
211 subject => $self->trim($sub),
212 body => $self->trim($body)}};
213  
214  
215 }
216  
217 print Dumper $test_set;
218 return $test_set;
219 }
220  
221  
222 sub training_books {
223  
224 my $self = shift;
225 my $i = 0;
226 my $booklist = $self->analyseBookNames();
227 my $chaps = {};
228 my @keys = keys %$booklist;
229 my @subject = ();
230 my @body = ();
231  
232 foreach(@keys) {
233  
234 @subject = @{$booklist->{$keys[$i]}->{subject}};
235 @body = @{$booklist->{$keys[$i]}->{body}};
236  
237 if(@subject && @body){
238 $self->{booknames}->{$i}=$booklist->{$keys[$i]}->{name};
239  
240 $chaps->{$i} = {subject=>join(" ",@subject),
241 body=>join(" ",@body)};
242 $i++;
243 }
244 }
245  
246 return $chaps;
247  
248 }
249  
250 sub perform_standard_tests {
251  
252 my $self = shift;
253  
254 my %features = $self->getFeatures();
255 my $chaps = $self->training_books();
256 my $test_set = $self->getTestDocs();
257  
258  
259 my $docs;
260 foreach my $cat(keys %$chaps) {
261 $docs->{$cat} = {categories => [$cat],
262 content => {subject => $chaps->{$cat}->{subject},
263 body => $chaps->{$cat}->{body},
264 },
265 };
266 }
267 my $c = AI::Categorizer->new(knowledge_set =>
268 AI::Categorizer::KnowledgeSet->new( name => 'CSL'),
269 verbose => 0,
270 );
271  
272 while (my ($name, $data) = each %$docs) {
273 $c->knowledge_set->make_document(name => $name, %$data, %features);
274 }
275  
276 my $learner = $c->learner;
277 $learner->train;
278  
279 $learner->save_state('state');
280 $learner = $learner->restore_state('state');
281  
282 my $threshold = 0.9;
283 while (my ($name, $data) = each %$test_set) {
284  
285 my $doc = AI::Categorizer::Document->new
286 (name => $name,
287 content => $data->{content},
288 %features);
289  
290 my $r = $learner->categorize($doc);
291 $r->threshold($threshold);
292 my $b = $r->best_category;
293 next unless $r->in_category($b);
294 printf("\n\n[%s %s %s]\nis in category %d, with score %.3f\n%s\n%s\n",
295 $name,
296 $data->{content}->{subject},
297 $data->{content}->{body},
298 $b,
299 $r->scores($b),
300 $self->{booknames}->{$b},
301 sprintf lc `microdict $self->{booknames}->{$b} | data-freq --limit 15`);
302  
303 }
304  
305  
306 }
307  
308 END{
309 my $self = shift;
310 # untie %{$self->{microtree}};
311  
312 }
313  
314 1;
315  
316 =head1 NAME
317  
318 AI::MicroStructure::Categorizer;
319  
320 =head1 DESCRIPTION
321  
322 old obsolete Categorizer
323  
324 =head1 SYNOPSIS
325  
326 ~$ micro new world
327  
328 ~$ micro structures
329  
330 ~$ micro any 2
331  
332 ~$ micro drop world
333  
334 ~$ micro
335  
336 =head1 AUTHOR
337  
338 Hagen Geissler <santex@cpan.org>
339  
340 =head1 COPYRIGHT AND LICENCE
341  
342 Hagen Geissler <santex@cpan.org>
343  
344 =head1 SUPPORT AND DOCUMENTATION
345  
346 [sample using concepts](http://quantup.com)
347  
348 [PDF info on my works](https://github.com/santex)
349  
350  
351 =head1 SEE ALSO
352  
353 AI-MicroStructure
354  
355 =cut
356  
357  
358 __DATA__
359  
360  
361 new Cache::Memcached::Fast({
362 servers => [ { address => 'localhost:11211',
363 weight => 2.5 }],
364 namespace => 'my:',
365 connect_timeout => 0.2,
366 io_timeout => 0.5,
367 close_on_error => 1,
368 compress_threshold => 100_000,
369 compress_ratio => 0.9,
370 compress_methods =>
371 [ \&IO::Compress::Gzip::gzip,
372 \&IO::Uncompress::Gunzip::gunzip ],
373 max_failures => 3,
374 failure_timeout => 2,
375 ketama_points => 150,
376 nowait => 1,
377 hash_namespace => 1,
378 serialize_methods => [ \&Storable::freeze,
379 \&Storable::thaw ],
380 utf8 => ($^V ge v5.8.1 ? 1 : 0),
381 max_size => 4*512 * 1024,
382 });
383