| 1 | package AI::Categorizer::FeatureSelector::ChiSquare; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use AI::Categorizer::FeatureSelector; | ||
| 5 | use base qw(AI::Categorizer::FeatureSelector::CategorySelector); | ||
| 6 | |||
| 7 | use Params::Validate qw(:types); | ||
| 8 | |||
| 9 | # Chi-Square function | ||
| 10 | # NB: this could probably be optimised a bit... | ||
| 11 | |||
| 12 | sub reduction_function { | ||
| 13 | my ($self,$term,$N,$allFeaturesSum, | ||
| 14 | $coll_features,$cat_features,$cat_features_sum) = @_; | ||
| 15 | my $CHI2SUM = 0; | ||
| 16 | my $nbcats = 0; | ||
| 17 | foreach my $catname (keys %{$cat_features}) { | ||
| 18 | # while ( my ($catname,$catfeatures) = each %{$cat_features}) { | ||
| 19 | my ($A,$B,$C,$D); # A = number of times where t and c co-occur | ||
| 20 | # B = " " " t occurs without c | ||
| 21 | # C = " " " c occurs without t | ||
| 22 | # D = " " " neither c nor t occur | ||
| 23 | $A = $cat_features->{$catname}->value($term); | ||
| 24 | $B = $coll_features->value($term) - $A; | ||
| 25 | $C = $cat_features_sum->{$catname} - $A; | ||
| 26 | $D = $allFeaturesSum - ($A+$B+$C); | ||
| 27 | my $ADminCB = ($A*$D)-($C*$B); | ||
| 28 | my $CHI2 = $N*$ADminCB*$ADminCB / (($A+$C)*($B+$D)*($A+$B)*($C+$D)); | ||
| 29 | $CHI2SUM += $CHI2; | ||
| 30 | $nbcats++; | ||
| 31 | } | ||
| 32 | return $CHI2SUM/$nbcats; | ||
| 33 | } | ||
| 34 | |||
| 35 | 1; | ||
| 36 | |||
| 37 | __END__ |