lib/AI/MicroStructure/Memorizer.pm untested

filter: Statementstmt 0.0% 0 / 294 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 415 CC 27 SCAR 26.2
CC
27
Cov
0%
sampleRun
42.0
search
30.0
analyseBookNames
20.0
CRAP 13.7
1 #!/usr/bin/perl -W
2 package AI::MicroStructure::Memorizer;
3 use strict;
4 use warnings;
5 use Digest::MD5 qw(md5 md5_hex md5_base64);
6 use File::Spec;
7 use Data::Dumper;
8 use Search::ContextGraph;
9 use Cache::Memcached::Fast;
10 my $count;
11 my %known_ips;
12 my %known_names;
13 my @output;
14 my @thread;
15 my $thread_support;
16 my @common_cnames;
17 my $threads;
18 my $book= "";
19 $count = 0;
20  
21 # try and load thread modules, if it works import their functions
22 BEGIN {
23 eval {
24 require threads;
25 require threads::shared;
26 require Thread::Queue;
27 $thread_support = 1;
28 };
29 if ($@) { # got errors, no ithreads :(
30 $thread_support = 0;
31 } else { #safe to haul in the threadding functions
32 import threads;
33 import threads::shared;
34 import Thread::Queue;
35 }
36 }
37  
38 # turn errors back on
39 BEGIN {
40 $SIG{__DIE__} = 'DEFAULT';
41 $SIG{__WARN__} = 'DEFAULT';
42 }
43  
44  
45 sub threadSupport{
46 my $self = shift;
47 my $bookpath = shift;
48  
49 my @books = $self->getBookList($bookpath);
50 my $buffer={};
51
52 if ($threads) {
53 share($count);
54 share(%known_ips);
55 share(%known_names);
56 share(@output);
57 my $stream = new Thread::Queue;
58 foreach my $book (@books) {
59  
60 $stream->enqueue($book);
61 }
62 for (0..$threads) {
63 my $kid = new threads($self->store($book), $stream);
64 $stream->enqueue(undef); #for each thread
65 }
66 foreach my $thread (threads->list) {
67 $thread->join if ($thread->tid && !threads::equal($thread, threads->self));
68 }
69 } else {
70 foreach my $book (@books) {
71 $self->search($book);
72 }
73 }
74 # write to file any output generated by child threads
75 print @output;
76  
77 }
78  
79  
80 sub search {
81  
82 my $self= shift;
83 my $namehex = "";
84 my $upstream;
85 my $tid;
86 my $resbf;
87 if ($threads && threads->self->tid()) {
88 $upstream = shift;
89 $tid = threads->self->tid();
90 $resbf = 1;
91 warn $tid;
92 } else {
93 $resbf = 0;
94 }
95  
96 my $i=0;
97 my $returns = {};
98 my $content = {};
99 my $name="";
100 # only runs once if not threaded
101 while (my $search_item = $threads ? $upstream->dequeue : shift) {
102  
103 $namehex = md5_hex($search_item);
104
105 next if($search_item eq "." || $search_item eq "..");
106  
107 $content = $self->{cache}->retrieve($namehex);
108 $name = sprintf("%s",$search_item);
109
110 if(defined($content->{body})){
111
112 $returns->{$namehex} =
113 {subject => $content->{subject},
114 body => $content->{body},
115 name => $name,
116 md5hex => $namehex};
117
118 }else{
119
120 $content = $self->catfile($_);
121
122 $returns->{$namehex} =
123 {subject=>$content->{subject},
124 body=>$content->{body},
125 name=>$name,
126 md5hex=>$namehex};
127
128 $self->{cache}->store($namehex,$content);
129
130 }
131
132
133 $self->{booknames}->{$namehex}=$name;
134
135 $self->{chaps}->{$namehex} = {subject=>$content->{subject},
136 body=>$content->{body}};
137 # print $search_item."\n";
138 return $self->{chaps};
139  
140
141 }
142 }
143  
144  
145 sub new {
146 my $this = shift;
147 my $class = ref($this) || $this;
148 my $self = {};
149 bless $self, $class;
150 $self->initialize(@_);
151  
152 return $self;
153 }
154  
155 sub initialize {
156 my $self = shift;
157 %$self=@_;
158
159 $self->{cache} = new Cache::Memcached::Fast({
160 servers => [ { address => 'localhost:11211',
161 weight => 2.5 }],
162 namespace => 'my:',
163 connect_timeout => 0.2,
164 io_timeout => 0.5,
165 close_on_error => 1,
166 compress_threshold => 100_000,
167 compress_ratio => 0.9,
168 compress_methods =>
169 [ \&IO::Compress::Gzip::gzip,
170 \&IO::Uncompress::Gunzip::gunzip ],
171 max_failures => 3,
172 failure_timeout => 2,
173 ketama_points => 150,
174 nowait => 1,
175 hash_namespace => 1,
176 serialize_methods => [ \&Storable::freeze,
177 \&Storable::thaw ],
178 utf8 => ($^V ge v5.8.1 ? 1 : 0),
179 max_size => 4*512 * 1024,
180 });
181  
182  
183  
184  
185 #AI::MicroStructure::Driver::Memcached->new();
186
187
188 $self->{context} = Search::ContextGraph->new( use_file => $self->{contextgraph}, auto_reweight => 0);
189  
190 }
191  
192  
193 sub trim
194 {
195 my $self = shift;
196 my $string = shift;
197 $string = "" unless $string;
198 $string =~ s/^\s+//;
199 $string =~ s/\s+$//;
200 $string =~ s/\t//;
201 $string =~ s/^\s//;
202 return $string;
203 }
204  
205  
206 sub catfile{
207 my $self = shift;
208 my $file=shift;
209 return unless($file);
210
211 my $path = sprintf("%s/%s",$self->{bookpath},$file);
212 # print $path;
213 my $cat = {};
214 my @cat = map{
215 my @x = split(":",$_);
216 $_ = $self->trim($x[1]);
217 }split("\n",
218 `microdict $path | data-freq --limit 500`);
219
220 $cat->{subject} = [@cat[0..10]];
221 $cat->{body} = [@cat];
222
223 return $cat;
224
225 }
226  
227  
228  
229 sub store{
230  
231 my $self = shift;
232 my $xname = shift;
233 my $content = {};
234 my $namehex = md5_hex($xname);
235 $content = $self->{cache}->retrieve($namehex);
236  
237
238 if(defined($content->{body})){
239
240 $self->{returns}->{$namehex} =
241 {subject => $content->{subject},
242 body => $content->{body},
243 name => $xname,
244 md5hex => $namehex};
245
246 }else{
247
248 $content = $self->catfile($_);
249
250 $self->{returns}->{$namehex} =
251 {subject=>$content->{subject},
252 body=>$content->{body},
253 name=>$xname,
254 md5hex=>$namehex};
255
256 $self->{cache}->store($namehex,$content);
257
258 }
259
260  
261  
262 }
263 sub getBookList{
264  
265 my $self = shift;
266 my $dir = shift;
267
268 $dir = $self->{bookpath} unless defined($dir);
269 die "$dir is not a directory" unless -d $dir;
270 opendir(DIR, $dir) or die $!;
271
272 my @mp3s = grep { $_ = sprintf("%s",$_); }
273 sort grep /^[\x20-\x7E]+$/,
274 readdir(DIR);
275
276 closedir DIR;
277  
278 return @mp3s;
279 }
280  
281 sub analyseBookNames{
282  
283 my $self = shift;
284 my $bookpath = shift;
285 my $returns = {};
286 my @books = $self->getBookList($bookpath);
287 my @data = ();
288 my $content = {};
289 my $name = "";
290 my $namehex = "";
291  
292  
293
294 foreach(@books) {
295 $content = {};
296 $namehex = md5_hex($_);
297
298 next if($_ eq "." || $_ eq "..");
299  
300 $content = $self->{cache}->retrieve($namehex);
301 $name = sprintf("%s",$_);
302
303 if(defined($content->{body})){
304
305 $returns->{$namehex} =
306 {subject => $content->{subject},
307 body => $content->{body},
308 name => $name,
309 md5hex => $namehex};
310
311 }else{
312
313 $content = $self->catfile($_);
314
315 $returns->{$namehex} =
316 {subject=>$content->{subject},
317 body=>$content->{body},
318 name=>$name,
319 md5hex=>$namehex};
320
321 $self->{cache}->store($namehex,$content);
322
323 }
324
325 next if($name=~/dvd/);
326
327 $self->{booknames}->{$namehex}=$name;
328
329 $self->{chaps}->{$namehex} = {subject=>$content->{subject},
330 body=>$content->{body}};
331  
332
333 }
334
335  
336 return $returns;
337
338 }
339  
340  
341  
342  
343 sub perform_standard_tests {
344  
345 my $self = shift;
346 $self->analyseBookNames();
347
348 my $booklist = $self->{chaps};
349
350 foreach my $k ( reverse sort { $b cmp $a } keys %{$self->{chaps}}){
351 if(defined($k)){
352 my @sub = @{$booklist->{$k}->{subject}};
353 my @body = @{$booklist->{$k}->{body}};
354
355
356 $self->{out} .= sprintf("%s\t[s=%d,b=%d]\t%s\n",$k,$#sub,$#body,$self->{booknames}->{$k});
357
358
359 if($#body>0){
360 $self->{context}->add_document($self->{booknames}->{$k}, \@body);
361 }
362 }
363 }
364  
365
366 return $self->{chaps};
367  
368  
369 }
370 sub getTermList{
371 my $self = shift;
372 return $self->{context}->term_list;
373  
374  
375 }
376  
377 sub query_simple_search{
378  
379 my $self = shift;
380  
381 my $term = shift;
382  
383 return $self->{context}->simple_search($term);
384
385 }
386  
387  
388  
389 sub query_similareDocs {
390  
391 my $self = shift;
392 my $term = shift;
393
394 $self->{out}="";
395
396 my @ranked_docs = $self->simple_search($term);
397
398 my ($s1,$s2) = $self->{context}->find_similar(@ranked_docs);
399
400 #print Dumper $s2;
401 foreach (sort {$a cmp $b} keys %$s1)
402 {
403 next unless($s1->{$_}>=0.75);
404
405 $self->{out} .= sprintf("\n%s = %s",$_, $s1->{$_});
406 }
407  
408 $self->{out} .= sprintf "\n<br>\n<b>compute strong similar terms</b>";
409
410 #print Dumper $s2;
411 foreach (sort {$a cmp $b} keys %$s2)
412 {
413 next unless($s2->{$_}>=0.75);
414
415 $self->{out} .= sprintf("\n%s = %s",$_, $s2->{$_});
416 }
417  
418  
419 $self->{out} .= sprintf "\n<br>\n<b>worndnet suport / compute strong similar terms</b>";
420
421 #print Dumper $s2;
422 foreach (sort {$a cmp $b} keys %$s2)
423 {
424 next unless($s2->{$_}>=0.75);
425
426 $self->{out} .= sprintf("\n(%s%s)\n%3.3f",sprintf($self->trim(`micro-wnet $_`)),$_,$s2->{$_});
427 }
428  
429  
430 return $self->{out};
431 }
432  
433 sub query_simple_intersection{
434  
435 my $self = shift;
436  
437 my @terms = @_;
438  
439 my @ranked_docs = $self->{context}->intersection(terms=>@terms);
440
441 return \@ranked_docs;
442 }
443  
444 sub query_simple_mixed{
445  
446 my $self = shift;
447  
448 my @terms = shift;
449  
450 my ( $docs, $words ) = $self->{context}->mixed_search(
451 { docs=>[$self->{context}->doc_list],
452 terms => [@terms]});
453  
454 return ( $docs, $words );
455  
456 }
457  
458 sub sampleRun{
459  
460 my $self = shift;
461 my $intrest = {};
462
463
464 $intrest->{base}="physics";
465 $intrest->{second}=lc `micro`;
466
467 $intrest->{base}=~s/_|\n/ /g;
468 $intrest->{second}=~s/_|\n/ /g;
469
470 my @ranked_docs = $self->{context}->simple_search($intrest->{base});
471
472
473  
474 my $out = "";
475 $self->{out} .= sprintf "<hr/><h1>%s only</h1>\n",$intrest->{base};
476
477 printf Dumper [
478 $self->{context}->intersection(
479 terms => [$intrest])];
480  
481 $self->{out} .= sprintf "<hr/><h1>%s + %s</h1>\n",$intrest->{base},$intrest->{second};
482
483 printf Dumper [
484 $self->{context}->intersection(
485 terms => [$intrest->{base},$intrest->{second}])];
486  
487  
488 $self->{out} .= sprintf "<hr/><h1>%s + %s + synthetic</h1>\n",$intrest->{base},$intrest->{second};
489 $self->{out} .= sprintf Dumper [
490 $self->{context}->intersection(
491 terms => [$intrest->{base},$intrest->{second}] )];
492
493 $self->{out} .= sprintf "<hr/><h1>drill down</h1>\n\n";
494
495  
496 @ranked_docs = $self->{context}->intersection(
497 terms=>[$intrest->{base},$intrest->{second}]);
498  
499 $self->{out} .= sprintf
500 "<hr/><h1>intersection[programming,computer]".
501 "</h1>\n\n",
502 join "<br />\n",@ranked_docs;
503  
504 $self->{out} .= sprintf "<hr/><h1>mixed_search on intersection</h1>\n\n";
505  
506 my ( $docs, $words ) =
507 $self->{context}->mixed_search(
508 {docs=>[@ranked_docs],
509 terms => ['genetic',
510 'dreaming',
511 'evolutionary']});
512  
513 my $similarToAi = {};
514  
515 # Print out result set of returned documents
516 foreach my $k ( sort { $docs->{$b} <=> $docs->{$a} }
517 keys %{ $docs } ) {
518
519 if($docs->{$k}>=100.01) {
520
521 $self->{out} .= sprintf "alpha\trelevance ",
522 $docs->{$k},"\tDoc ",$k,"\n";
523
524 }elsif($docs->{$k}<=101.0 && $docs->{$k}>=100.0){
525
526 $self->{out} .= sprintf "dominant\trelevance",
527 $docs->{$k},"\tDoc ",$k, "\n";
528  
529 }
530 }
531
532 push @ranked_docs , $self->{context}->simple_search({
533 docs=>[$self->{context}->doc_list] ,
534 terms=>['programming']});
535
536
537 # print Dumper @ranked_docs;
538
539 $self->{out} .= sprintf "<hr/><h1>simple_search on fuzzy $#ranked_docs</h1>".
540 "\n<br>\n<b>compute strong similar docs</b>";
541
542 #$self->{context}->reweight_graph();
543
544 my ($s1,$s2) = $self->{context}->find_similar(@ranked_docs);
545
546 #print Dumper $s2;
547 foreach (sort {$a cmp $b} keys %$s1)
548 {
549 next unless($s1->{$_}>=0.75);
550
551 $self->{out} .= sprintf("\n%s = %s",$_, $s1->{$_});
552 }
553  
554 $self->{out} .= sprintf "\n<br>\n<b>compute strong similar terms</b>";
555
556 #print Dumper $s2;
557 foreach (sort {$a cmp $b} keys %$s2)
558 {
559 next unless($s2->{$_}>=0.75);
560
561 $self->{out} .= sprintf("\n%s = %s",$_, $s2->{$_});
562 }
563  
564  
565 $self->{out} .= sprintf "\n<br>\n<b>worndnet suport / compute strong similar terms</b>";
566
567 #print Dumper $s2;
568 foreach (sort {$a cmp $b} keys %$s2)
569 {
570 next unless($s2->{$_}>=0.995);
571
572 $self->{out} .= sprintf("\n(%s=%3.3f)",$_,$s2->{$_});
573 }
574  
575 return $self->{out};
576  
577 }
578  
579 sub getDocList{
580 my $self = shift;
581 return $self->{context}->doc_list;
582  
583 }
584  
585  
586 1;
587  
588 package main;
589 use Data::Printer;
590 my $configure = {};
591 $configure->{bookpath}=sprintf("%s/data-hub/data-hub/",
592 "/home/santex");
593  
594  
595 my $memo = AI::MicroStructure::Memorizer->new(bookpath=>$configure->{bookpath});
596 $memo->getBookList();
597 p $memo
598 __DATA__
599  
600
601 new Cache::Memcached::Fast({
602 servers => [ { address => 'localhost:11211',
603 weight => 2.5 }],
604 namespace => 'my:',
605 connect_timeout => 0.2,
606 io_timeout => 0.5,
607 close_on_error => 1,
608 compress_threshold => 100_000,
609 compress_ratio => 0.9,
610 compress_methods =>
611 [ \&IO::Compress::Gzip::gzip,
612 \&IO::Uncompress::Gunzip::gunzip ],
613 max_failures => 3,
614 failure_timeout => 2,
615 ketama_points => 150,
616 nowait => 1,
617 hash_namespace => 1,
618 serialize_methods => [ \&Storable::freeze,
619 \&Storable::thaw ],
620 utf8 => ($^V ge v5.8.1 ? 1 : 0),
621 max_size => 4*512 * 1024,
622 });
623