in reply to Re^2: how to use Algorithm::NaiveBayes module
in thread how to use Algorithm::NaiveBayes module
To train you would feed the first two files in:wordA wordB wordC wordD wordA wordE wordF
You can then feed the third file in: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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: how to use Algorithm::NaiveBayes module
by agnes (Novice) on Apr 24, 2014 at 14:28 UTC | |
|
Re^4: how to use Algorithm::NaiveBayes module
by agnes (Novice) on Apr 24, 2014 at 18:41 UTC | |
by tangent (Parson) on Apr 24, 2014 at 19:45 UTC | |
by agnes (Novice) on Apr 25, 2014 at 04:56 UTC | |
by tangent (Parson) on Apr 25, 2014 at 18:05 UTC | |
by agnes (Novice) on Apr 26, 2014 at 01:13 UTC | |
|