line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Algorithm::NaiveBayes; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
19409
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
122
|
|
4
|
3
|
|
|
3
|
|
3357
|
use Storable; |
|
3
|
|
|
|
|
10613
|
|
|
3
|
|
|
|
|
185
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
19
|
use vars qw($VERSION); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
2170
|
|
7
|
|
|
|
|
|
|
$VERSION = '0.04'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
sub new { |
10
|
6
|
|
|
6
|
1
|
1044
|
my $package = shift; |
11
|
6
|
|
|
|
|
52
|
my $self = bless { |
12
|
|
|
|
|
|
|
version => $VERSION, |
13
|
|
|
|
|
|
|
purge => 1, |
14
|
|
|
|
|
|
|
model_type => 'Frequency', |
15
|
|
|
|
|
|
|
@_, |
16
|
|
|
|
|
|
|
instances => 0, |
17
|
|
|
|
|
|
|
training_data => {}, |
18
|
|
|
|
|
|
|
}, $package; |
19
|
|
|
|
|
|
|
|
20
|
6
|
100
|
|
|
|
25
|
if ($package eq __PACKAGE__) { |
21
|
|
|
|
|
|
|
# Bless into the proper subclass |
22
|
3
|
|
|
|
|
15
|
return $self->_load_model_class->new(@_); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
3
|
|
|
|
|
22
|
return bless $self, $package; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _load_model_class { |
29
|
4
|
|
|
4
|
|
9
|
my $self = shift; |
30
|
4
|
50
|
50
|
|
|
50
|
die "model_class cannot be set to " . __PACKAGE__ if ($self->{model_class}||'') eq __PACKAGE__; |
31
|
4
|
|
33
|
|
|
34
|
my $package = $self->{model_class} || __PACKAGE__ . "::Model::" . $self->{model_type}; |
32
|
4
|
100
|
|
|
|
53
|
unless ($package->can('new')) { |
33
|
3
|
|
|
3
|
|
2094
|
eval "use $package"; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
55
|
|
|
3
|
|
|
|
|
279
|
|
34
|
3
|
50
|
|
|
|
15
|
die $@ if $@; |
35
|
|
|
|
|
|
|
} |
36
|
4
|
|
|
|
|
27
|
return $package; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub save_state { |
40
|
1
|
|
|
1
|
1
|
2
|
my ($self, $path) = @_; |
41
|
1
|
|
|
|
|
29
|
Storable::nstore($self, $path); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub restore_state { |
45
|
1
|
|
|
1
|
1
|
2
|
my ($pkg, $path) = @_; |
46
|
1
|
50
|
|
|
|
6
|
my $self = Storable::retrieve($path) |
47
|
|
|
|
|
|
|
or die "Can't restore state from $path: $!"; |
48
|
1
|
|
|
|
|
121
|
$self->_load_model_class; |
49
|
1
|
|
|
|
|
3
|
return $self; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub add_instance { |
53
|
205
|
|
|
205
|
1
|
5871
|
my ($self, %params) = @_; |
54
|
205
|
|
|
|
|
445
|
for ('attributes', 'label') { |
55
|
410
|
50
|
|
|
|
833
|
die "Missing required '$_' parameter" unless exists $params{$_}; |
56
|
|
|
|
|
|
|
} |
57
|
205
|
|
|
|
|
508
|
for ($params{label}) { |
58
|
205
|
100
|
|
|
|
612
|
$_ = [$_] unless ref; |
59
|
205
|
|
|
|
|
209
|
@{$self->{labels}}{@$_} = (); |
|
205
|
|
|
|
|
601
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
205
|
|
|
|
|
239
|
$self->{instances}++; |
63
|
205
|
|
|
|
|
560
|
$self->do_add_instance($params{attributes}, $params{label}, $self->{training_data}); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
9
|
|
|
9
|
1
|
230
|
sub labels { keys %{ $_[0]->{labels} } } |
|
9
|
|
|
|
|
43
|
|
67
|
3
|
|
|
3
|
0
|
14
|
sub instances { $_[0]->{instances} } |
68
|
4
|
|
|
4
|
0
|
20
|
sub training_data { $_[0]->{training_data} } |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub train { |
71
|
3
|
|
|
3
|
1
|
5
|
my $self = shift; |
72
|
3
|
|
|
|
|
14
|
$self->{model} = $self->do_train($self->{training_data}); |
73
|
3
|
100
|
|
|
|
22
|
$self->do_purge if $self->purge; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub do_purge { |
77
|
1
|
|
|
1
|
1
|
1
|
my $self = shift; |
78
|
1
|
|
|
|
|
9
|
delete $self->{training_data}; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub purge { |
82
|
4
|
|
|
4
|
1
|
6
|
my $self = shift; |
83
|
4
|
50
|
|
|
|
16
|
$self->{purge} = shift if @_; |
84
|
4
|
|
|
|
|
31
|
return $self->{purge}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub predict { |
88
|
3
|
|
|
3
|
1
|
118
|
my ($self, %params) = @_; |
89
|
3
|
50
|
|
|
|
11
|
my $newattrs = $params{attributes} or die "Missing 'attributes' parameter for predict()"; |
90
|
3
|
|
|
|
|
11
|
return $self->do_predict($self->{model}, $newattrs); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
__END__ |