wordA wordB wordC
wordD wordA wordE wordF
####
my $pos_file = '/path/to/positive.txt';
my $neg_file = '/path/to/negative.txt';
my $categorizer = Algorithm::NaiveBayes->new;
my $fh;
open($fh,"<",$pos_file) or die "Could not open $pos_file: $!";
while (my $sentence = <$fh>) {
chomp $sentence;
my @words = split(' ',$sentence);
my %positive;
$positive{$_}++ for @words;
$categorizer->add_instance(
attributes => \%positive,
label => 'positive');
}
close($fh);
open($fh,"<",$neg_file) or die "Could not open $neg_file: $!";
while (my $sentence = <$fh>) {
chomp $sentence;
my @words = split(' ',$sentence);
my %negative;
$negative{$_}++ for @words;
$categorizer->add_instance(
attributes => \%negative,
label => 'negative');
}
close($fh);
$categorizer->train;
####
my $sentence_file = '/path/to/sentence.txt';
open($fh,"<",$sentence_file) or die "Could not open $sentence_file: $!";
while (my $sentence = <$fh>) {
chomp $sentence;
my @words = split(' ',$sentence);
my %test;
$test{$_}++ for @words;
my $probability = $categorizer->predict(attributes => \%test);
# ...
# do what you need with $probability
}
close($fh);