forked from y-tag/perl-Algorithm-ComplementNB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample2.pl
46 lines (32 loc) · 835 Bytes
/
sample2.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use lib('lib');
use Algorithm::TWCNB;
my $cnb = Algorithm::TWCNB->new();
my @pos_data = qw(a b ab bb aaab);
my @neg_data = qw(cc d cd ddd ccd);
foreach my $d (@pos_data) {
my $tmp = make_attributes($d);
$cnb->add_instance(attributes => $tmp, label => 'positive');
}
foreach my $d (@neg_data) {
my $tmp = make_attributes($d);
$cnb->add_instance(attributes => $tmp, label =>'negative');
}
$cnb->train();
foreach my $d qw(abccd adf) {
my $tmp = make_attributes($d);
my $score = $cnb->predict(attributes => $tmp);
print Dumper($d, $score);
}
print Dumper($cnb->labels);
sub make_attributes {
my $data = shift;
my $attributes = {};
foreach my $chr (split(//, $data)) {
$attributes->{$chr}++;
}
$attributes;
}