hi~here is my code, which I ignore the category I mentioned above(revenue, cost..)
#!/usr/bin/perl
use warnings;
use Algorithm::NaiveBayes;
my $pos_file = '/Users/Agnes/Documents/positive.TXT';
my $neg_file = '/Users/Agnes/Documents/negative.txt';
my $neu_file = '/Users/Agnes/Documents/neutral.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);
open($fh,"<",$neu_file) or die "Could not open $neg_file: $!";
while (my $sentence = <$fh>) {
chomp $sentence;
my @words = split(' ',$sentence);
my %neutral;
$neutral{$_}++ for @words;
$categorizer->add_instance(
attributes => \%neutral,
label => 'neutral');
}
close($fh);
$categorizer->train;
my $sentence_file = '/Users/Agnes/Documents/process_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);
if ( $probs->{positive} > 0.33 ) {
print "%positive\n";
}
if ( $probs->{negative} > 0.33 ) {
print "%negative\n";
}
if ( $probs->{neutral} > 0.33 ) {
print "%neutral\n";
}
}
close($fh);
my positive.txt is like this:
we believ exist cash cash equiv short-term investments, togeth fund generat operations, suffici meet oper requirements, regular quarter dividends, debt.
expen will reduc cut travel expenditures, reduc spend vendor cont staff, reduc market spending, scale back capit.
revenu relat window vista no subject similar deferr no signif undeliv elements.
but when I run this program, the mistakes shows
Use of uninitialized value in numeric gt (>) at calculation.pl line 60, <$fh> line 1.
Use of uninitialized value in numeric gt (>) at calculation.pl line 63, <$fh> line 1.
Use of uninitialized value in numeric gt (>) at calculation.pl line 66, <$fh> line 1.
|