line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package AI::NaiveBayes::Classification; |
2
|
|
|
|
|
|
|
$AI::NaiveBayes::Classification::VERSION = '0.04'; |
3
|
3
|
|
|
3
|
|
11
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
76
|
|
4
|
3
|
|
|
3
|
|
10
|
use warnings; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
58
|
|
5
|
3
|
|
|
3
|
|
43
|
use 5.010; |
|
3
|
|
|
|
|
6
|
|
6
|
3
|
|
|
3
|
|
934
|
use Moose; |
|
3
|
|
|
|
|
602219
|
|
|
3
|
|
|
|
|
17
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has features => (is => 'ro', isa => 'HashRef[HashRef]', required => 1); |
9
|
|
|
|
|
|
|
has label_sums => (is => 'ro', isa => 'HashRef', required => 1); |
10
|
|
|
|
|
|
|
has best_category => (is => 'ro', isa => 'Str', lazy_build => 1); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub _build_best_category { |
13
|
1
|
|
|
1
|
|
1
|
my $self = shift; |
14
|
1
|
|
|
|
|
31
|
my $sc = $self->label_sums; |
15
|
|
|
|
|
|
|
|
16
|
1
|
|
|
|
|
3
|
my ($best_cat, $best_score) = each %$sc; |
17
|
1
|
|
|
|
|
4
|
while (my ($key, $val) = each %$sc) { |
18
|
1
|
50
|
|
|
|
5
|
($best_cat, $best_score) = ($key, $val) if $val > $best_score; |
19
|
|
|
|
|
|
|
} |
20
|
1
|
|
|
|
|
25
|
return $best_cat; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub find_predictors{ |
24
|
1
|
|
|
1
|
1
|
7
|
my $self = shift; |
25
|
|
|
|
|
|
|
|
26
|
1
|
|
|
|
|
58
|
my $best_cat = $self->best_category; |
27
|
1
|
|
|
|
|
29
|
my $features = $self->features; |
28
|
1
|
|
|
|
|
1
|
my @predictors; |
29
|
1
|
|
|
|
|
3
|
for my $feature ( keys %$features ) { |
30
|
2
|
|
|
|
|
3
|
for my $cat ( keys %{ $features->{$feature } } ){ |
|
2
|
|
|
|
|
5
|
|
31
|
4
|
100
|
|
|
|
8
|
next if $cat eq $best_cat; |
32
|
2
|
|
|
|
|
4
|
push @predictors, [ $feature, $features->{$feature}{$best_cat} - $features->{$feature}{$cat} ]; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
} |
35
|
1
|
|
|
|
|
4
|
@predictors = sort { abs( $b->[1] ) <=> abs( $a->[1] ) } @predictors; |
|
1
|
|
|
|
|
5
|
|
36
|
1
|
|
|
|
|
4
|
return $best_cat, @predictors; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
1; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=pod |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=encoding UTF-8 |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 NAME |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
AI::NaiveBayes::Classification - The result of a bayesian classification |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 VERSION |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
version 0.04 |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head1 SYNOPSIS |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
my $result = $classifier->classify({bar => 3, blurp => 2}); |
59
|
|
|
|
|
|
|
# $result is an AI::NaiveBayes::Classification object |
60
|
|
|
|
|
|
|
say $result->best_category; |
61
|
|
|
|
|
|
|
my $predictors = $result->find_predictors; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 DESCRIPTION |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
AI::NaiveBayes::Classification represents the result of a bayesian classification, |
66
|
|
|
|
|
|
|
produced by AI::NaiveBayes classifier. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head1 METHODS |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=over 4 |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item C<best_category()> |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Returns a string being a label that suits given document the best. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item C<find_predictors()> |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
This method returns the C<best_category()>, as well as the list of all the predictors |
79
|
|
|
|
|
|
|
along with their influence on the best category selected. So the second value |
80
|
|
|
|
|
|
|
returned is a list of array references, where each one contains a string being a |
81
|
|
|
|
|
|
|
single feature and a number describing its influence on the result. So the |
82
|
|
|
|
|
|
|
second part of the result may look like this: |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
( |
85
|
|
|
|
|
|
|
[ 'activities', 1.2511540632952 ], |
86
|
|
|
|
|
|
|
[ 'over', -1.0269523272981 ], |
87
|
|
|
|
|
|
|
[ 'provide', 0.8280157033269 ], |
88
|
|
|
|
|
|
|
[ 'natural', 0.7361042359385 ], |
89
|
|
|
|
|
|
|
[ 'against', -0.6923354975173 ], |
90
|
|
|
|
|
|
|
) |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=back |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head1 SEE ALSO |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
AI::NaiveBayes (3), AI::Classifier(3) |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head1 AUTHORS |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=over 4 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item * |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Zbigniew Lukasiak <zlukasiak@opera.com> |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item * |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Tadeusz SoÅnierz <tsosnierz@opera.com> |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item * |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Ken Williams <ken@mathforum.org> |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=back |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Opera Software ASA. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
121
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=cut |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
__END__ |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# ABSTRACT: The result of a bayesian classification |
128
|
|
|
|
|
|
|
|