blib/lib/Algorithm/Bayesian.pm

filter: Statementstmt 100% 72 / 72 Branchbran 68.1% 30 / 44 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 90.4% 133 / 147 CC 29 SCAR 15.7
CC
29
Cov
89%
testWord
8.0
getHam
5.1
getSpam
5.1
CRAP 4.8
↑ Summary
1 package Algorithm::Bayesian;
2  
3 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Math::BigFloat;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use constant HAMSTR => '*ham';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use constant SPAMSTR => '*spam';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = '0.5';
12  
13 =head1 NAME
14  
15 Algorithm::Bayesian - Bayesian Spam Filtering Algorithm
16  
17 =head1 SYNOPSIS
18  
19 use Algorithm::Bayesian;
20 use Tie::Foo;
21  
22 my %storage;
23 tie %storage, 'Tie:Foo', ...;
24 my $b = Algorithm::Bayesian->new(\%storage);
25  
26 $b->spam('spamword1', 'spamword2', ...);
27 $b->ham('hamword1', 'hamword2', ...);
28  
29 my $pr = $b->test('word1', 'word2', ...);
30  
31 =head1 DESCRIPTION
32  
33 Algorithm::Bayesian provide an easy way to handle Bayesian spam filtering algorithm.
34  
35 =head1 SUBROUTINES/METHODS
36  
37 =head2 new
38  
39 my $b = Algorithm::Bayesian->new(\%hash);
40  
41 Constructor. Simple hash would be fine. You can use L<Tie::DBI> to store data to RDBM, or other key-value storage.
42  
43 =cut
44  
45 3 ▶sub new {
Subroutine new: called CC=4 SCAR=14.6
Pod documented
46 3 ▶ my $self = shift or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak() 3 0 3
47  
48 3 my $s = shift;
49 3 ▶ $s->{HAMSTR} = 0 if !defined $s->{HAMSTR};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s->{"HAMSTR"} 3 0 3
50 3 ▶ $s->{SPAMSTR} = 0 if !defined $s->{SPAMSTR};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s->{"SPAMSTR"} 3 0 3
51  
52 3 bless {storage => $s}, $self;
53 }
54  
55 =head2 getHam
56  
57 my $num = $b->getHam($word);
58  
59 Get C<$word> count in Ham.
60  
61 =cut
62  
63 64 ▶sub getHam {
Subroutine getHam: called CC=5 SCAR=16.3
Pod documented
64 64 ▶ my $self = shift or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak() 64 0 64
65 64 ▶ my $s = $self->{storage} or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->{"storage"} or croak() 64 0 64
66  
67 64 my $w = shift;
68  
69 64 ▶ return $s->{HAMSTR} if !defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 34 30 64
70 30 ▶ return $s->{"h$w"} || 0;
Condition100%2/2
$$s{"h$w"} || 0
l!l
1515
MC/DC100%1/1
$$s{"h$w"} || 0
$$s{"h$w"}
Truth table100%2/2
A
A= $$s{"h$w"}
Aresult
11
00
71 }
72  
73 =head2 getSpam
74  
75 my $num = $b->getSpam($word);
76  
77 Get C<$word> count in Spam.
78  
79 =cut
80  
81 64 ▶sub getSpam {
Subroutine getSpam: called CC=5 SCAR=16.3
Pod documented
82 64 ▶ my $self = shift or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak() 64 0 64
83 64 ▶ my $s = $self->{storage} or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->{"storage"} or croak() 64 0 64
84  
85 64 my $w = shift;
86  
87 64 ▶ return $s->{SPAMSTR} if !defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 34 30 64
88 30 ▶ return $s->{"s$w"} || 0;
Condition100%2/2
$$s{"s$w"} || 0
l!l
1515
MC/DC100%1/1
$$s{"s$w"} || 0
$$s{"s$w"}
Truth table100%2/2
A
A= $$s{"s$w"}
Aresult
11
00
89 }
90  
91 =head2 ham
92  
93 $b->ham(@words);
94  
95 Train C<@words> as Ham.
96  
97 =cut
98  
99 2 ▶sub ham {
Subroutine ham: called CC=4 SCAR=14.3
Pod documented
100 2 ▶ my $self = shift or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak() 2 0 2
101 2 ▶ my $s = $self->{storage} or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->{"storage"} or croak() 2 0 2
102  
103 2 foreach my $w (@_) {
104 12 $s->{"h$w"}++;
105 }
106  
107 2 $s->{HAMSTR}++;
108 }
109  
110 =head2 spam
111  
112 $b->spam(@words);
113  
114 Train C<@words> as Spam.
115  
116 =cut
117  
118 2 ▶sub spam {
Subroutine spam: called CC=4 SCAR=14.3
Pod documented
119 2 ▶ my $self = shift or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak() 2 0 2
120 2 ▶ my $s = $self->{storage} or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $s = $self->{"storage"} or croak() 2 0 2
121  
122 2 foreach my $w (@_) {
123 11 $s->{"s$w"}++;
124 }
125  
126 2 $s->{SPAMSTR}++;
127 }
128  
129 =head2 test
130  
131 my $pr = $b->test(@words);
132  
133 Calculate the spam probability of C<@words>. The range of C<$pr> will be in 0 to 1.
134  
135 =cut
136  
137 5 ▶sub test {
Subroutine test: called CC=5 SCAR=16.1
Pod documented
138 5 ▶ my $self = shift or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak() 5 0 5
139  
140 5 my $prec = 2 * scalar @_;
141 5 my $a1 = Math::BigFloat->new('1', $prec);
142 5 my $a2 = $a1->copy;
143  
144 5 foreach my $w (@_) {
145 23 my $pr = $self->testWord($w);
146  
147 # Avoid 0/1
148 23 ▶ $pr = 0.99 if $pr > 0.99;
Branch: 1 branch
BranchTrueFalseTotal
if $pr > "0.99" 11 12 23
149 23 ▶ $pr = 0.01 if $pr < 0.01;
Branch: 1 branch
BranchTrueFalseTotal
if $pr < "0.01" 11 12 23
150  
151 23 $a1 *= 2 * $pr;
152 23 $a2 *= 2 * (1 - $pr);
153 }
154  
155 5 return ($a1 / ($a1 + $a2))->bstr;
156 }
157  
158 =head2 testWord
159  
160 my $pr = $b->testWord($word);
161  
162 Calculate the spam probability of C<$word>.
163  
164 The range of C<$pr> will be in 0 to 1. For non-existence word, it will be 0.5.
165  
166 =cut
167  
168 31 ▶sub testWord {
Subroutine testWord: called CC=8 SCAR=20.8
Pod documented
169 31 ▶ my $self = shift or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak() 31 0 31
170 31 ▶ my $w = shift or croak;
Branch: 1 branch
BranchTrueFalseTotal
my $w = shift() or croak() 31 0 31
171  
172 31 my $hamNum = $self->getHam;
173 31 my $spamNum = $self->getSpam;
174 31 my $totalNum = $hamNum + $spamNum;
175  
176 31 ▶ return 0.5 if 0 == $totalNum;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == $totalNum 1 30 31
177  
178 30 my $wSpam = $self->getSpam($w);
179 30 my $wHam = $self->getHam($w);
180  
181 30 ▶ return 0.5 if 0 == $wSpam and 0 == $wHam;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == $wSpam and 0 == $wHam 2 28 30
Condition100%3/3
0 == $wSpam and 0 == $wHam
!ll&&!rl&&r
15132
MC/DC100%2/2
0 == $wSpam and 0 == $wHam
0 == $wSpam 0 == $wHam
Truth table100%3/3
A and B
A= 0 == $wSpamB= 0 == $wHam
ABresult
0X0
100
111
182 28 ▶ return 0 if 0 == $wSpam;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == $wSpam 13 15 28
183 15 ▶ return 1 if 0 == $wHam;
Branch: 1 branch
BranchTrueFalseTotal
if 0 == $wHam 13 2 15
184  
185 2 my $hamPr = $hamNum / $totalNum;
186 2 my $spamPr = $spamNum / $totalNum;
187  
188 2 my $a1 = $wSpam * $spamPr / $spamNum;
189 2 my $a2 = $wHam * $hamPr / $hamNum;
190  
191 2 return $a1 / ($a1 + $a2);
192 }
193  
194 =head1 AUTHOR
195  
196 Gea-Suan Lin, C<< <gslin at gslin.org> >>
197  
198 =head1 LICENSE AND COPYRIGHT
199  
200 Copyright 2010 Gea-Suan Lin.
201  
202 This program is free software; you can redistribute it and/or modify it
203 under the terms of either: the GNU General Public License as published
204 by the Free Software Foundation; or the Artistic License.
205  
206 See http://dev.perl.org/licenses/ for more information.
207  
208  
209 =cut
210  
211 3 1; # End of Algorithm::Bayesian
↑ Summary