lib/AI/MicroStructure/Collection.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 90 CC 3 SCAR 11.5
CC
3
Cov
0%
new
6.0
parse
6.0
new
2.0
CRAP 3.1
1 package AI::MicroStructure::Collection;
2 use Data::Dumper;
3 use strict;
4  
5  
6 sub new {
7  
8 my($class,$args) = @_;
9  
10 my $self = bless { cache => [] }, $class;
11  
12 $self->import;
13 $self->connectDriver;
14  
15  
16 return $self;
17 }
18  
19  
20  
21 sub reduction_function {
22 my ($self,$term,$N,$allFeaturesSum,
23 $coll_features,$cat_features,$cat_features_sum) = @_;
24 my $CHI2SUM = 0;
25 my $nbcats = 0;
26 foreach my $catname (keys %{$cat_features}) {
27 # while ( my ($catname,$catfeatures) = each %{$cat_features}) {
28 my ($A,$B,$C,$D); # A = number of times where t and c co-occur
29 # B = " " " t occurs without c
30 # C = " " " c occurs without t
31 # D = " " " neither c nor t occur
32 $A = $cat_features->{$catname}->value($term);
33 $B = $coll_features->value($term) - $A;
34 $C = $cat_features_sum->{$catname} - $A;
35 $D = $allFeaturesSum - ($A+$B+$C);
36 my $ADminCB = ($A*$D)-($C*$B);
37 my $CHI2 = $N*$ADminCB*$ADminCB / (($A+$C)*($B+$D)*($A+$B)*($C+$D));
38 $CHI2SUM += $CHI2;
39 $nbcats++;
40 }
41 return $CHI2SUM/$nbcats;
42 }
43  
44  
45 sub new {
46 my $pkg = shift;
47 my $self = bless {}, $pkg;
48 $self->insert(@_) if @_;
49 return $self;
50 }
51  
52 sub members {
53 return values %{$_[0]};
54 }
55  
56 sub size {
57 return scalar keys %{$_[0]};
58 }
59  
60 sub insert {
61 my $self = shift;
62 foreach my $element (@_) {
63 #warn "types are ", @_;
64 $self->{ $element->name } = $element;
65 }
66 }
67  
68 sub retrieve { $_[0]->{$_[1]} }
69  
70 sub includes { exists $_[0]->{ $_[1]->name } }
71 sub includes_name { exists $_[0]->{ $_[1] } }
72  
73  
74 sub parse {
75 my ($self, %args) = @_;
76  
77  
78 my %_operations = (
79 '&' => {
80 min => sub { (sort {$a <=> $b} @_)[0] },
81 product => sub { my $p = 1; $p *= $_ for @_; $p },
82 default => 'min',
83 },
84 '|' => {
85 max => sub { (sort {$a <=> $b} @_)[-1] },
86 sum => sub { my $s = 0; $s += $_ for @_; $s > 1 ? 1 : $s },
87 default => 'max',
88 },
89 '!' => {
90 complement => sub { 1 - $_[0] },
91 custom => sub {},
92 default => 'complement',
93 },
94 );
95  
96  
97 $args{content} =~ s{
98 ^(?:\.I)?\s+(\d+)\n # ID number - becomes document name
99 \.C\n
100 ([^\n]+)\n # Categories
101 \.T\n
102 (.+)\n+ # Title
103 \.W\n
104 }
105 {}sx
106  
107 or warn "Malformed record: $args{content}";
108  
109 my ($id, $categories, $title) = ($1, $2, $3);
110  
111 $self->{name} = $id;
112 $self->{content} = { title => $title,
113 body => $args{content} };
114  
115 my @categories = $categories =~ m/(.*?)\s+\d+[\s;]*/g;
116 @categories = map AI::Categorizer::Category->by_name(name => $_), @categories;
117 $self->{categories} = \@categories;
118 }
119  
120 1;
121  
122  
123 1;
124  
125  
126 =head1 NAME
127  
128 AI::MicroStructure::Collection
129  
130 =head1 DESCRIPTION
131  
132 Context driven scoreing
133  
134 =head1 SYNOPSIS
135  
136 ~$ micro new world
137  
138 ~$ micro structures
139  
140 ~$ micro any 2
141  
142 ~$ micro drop world
143  
144 ~$ micro
145  
146 =head1 AUTHOR
147  
148 Hagen Geissler <santex@cpan.org>
149  
150 =head1 COPYRIGHT AND LICENCE
151  
152 Hagen Geissler <santex@cpan.org>
153  
154 =head1 SUPPORT AND DOCUMENTATION
155  
156 [sample using concepts](http://quantup.com)
157  
158 [PDF info on my works](https://github.com/santex)
159  
160  
161 =head1 SEE ALSO
162  
163 AI-MicroStructure
164  
165 =cut