File: 03-state.t

package info (click to toggle)
libalgorithm-naivebayes-perl 0.04-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 108 kB
  • sloc: perl: 253; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 644 bytes parent folder | download | duplicates (3)
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

use strict;
use Test;
BEGIN { plan tests => 6 };
use Algorithm::NaiveBayes;
use File::Spec;
ok(1); # If we made it this far, we're loaded.

my $nb = Algorithm::NaiveBayes->new(purge => 0);
ok $nb;

# Populate
$nb->add_instance( attributes => _hash(qw(sheep very valuable farming)),
		   label => 'farming' );
ok $nb->labels, 1;

# Train
$nb->train;

# Save
my $file = File::Spec->catfile('t', 'model.dat');
$nb->save_state($file);
ok -e $file, 1;

# Restore
$nb = Algorithm::NaiveBayes->restore_state($file);
ok $nb;
ok !!$nb->can('predict');


################################################################
sub _hash { +{ map {$_,1} @_ } }