Algorithm-Bayesian

 view release on metacpan or  search on metacpan

lib/Algorithm/Bayesian.pm  view on Meta::CPAN

package Algorithm::Bayesian;

use Carp;
use Math::BigFloat;
use strict;
use warnings;

use constant HAMSTR => '*ham';
use constant SPAMSTR => '*spam';

our $VERSION = '0.5';

=head1 NAME

Algorithm::Bayesian - Bayesian Spam Filtering Algorithm

=head1 SYNOPSIS

    use Algorithm::Bayesian;
    use Tie::Foo;

    my %storage;
    tie %storage, 'Tie:Foo', ...;
    my $b = Algorithm::Bayesian->new(\%storage);

    $b->spam('spamword1', 'spamword2', ...);
    $b->ham('hamword1', 'hamword2', ...);

    my $pr = $b->test('word1', 'word2', ...);

=head1 DESCRIPTION

Algorithm::Bayesian provide an easy way to handle Bayesian spam filtering algorithm.

=head1 SUBROUTINES/METHODS

=head2 new

    my $b = Algorithm::Bayesian->new(\%hash);

Constructor. Simple hash would be fine. You can use L<Tie::DBI> to store data to RDBM, or other key-value storage.

=cut

sub new {
    my $self = shift or croak;

    my $s = shift;
    $s->{HAMSTR} = 0 if !defined $s->{HAMSTR};
    $s->{SPAMSTR} = 0 if !defined $s->{SPAMSTR};

    bless {storage => $s}, $self;
}

=head2 getHam

    my $num = $b->getHam($word);

Get C<$word> count in Ham.

=cut

sub getHam {
    my $self = shift or croak;
    my $s = $self->{storage} or croak;

    my $w = shift;

    return $s->{HAMSTR} if !defined $w;
    return $s->{"h$w"} || 0;
}

=head2 getSpam

    my $num = $b->getSpam($word);

Get C<$word> count in Spam.

=cut

sub getSpam {
    my $self = shift or croak;
    my $s = $self->{storage} or croak;

    my $w = shift;

    return $s->{SPAMSTR} if !defined $w;
    return $s->{"s$w"} || 0;
}

=head2 ham

    $b->ham(@words);

Train C<@words> as Ham.

=cut

sub ham {
    my $self = shift or croak;
    my $s = $self->{storage} or croak;

    foreach my $w (@_) {
	$s->{"h$w"}++;
    }

    $s->{HAMSTR}++;
}

=head2 spam

    $b->spam(@words);



( run in 1.071 second using v1.01-cache-2.11-cpan-39bf76dae61 )