Algorithm-ContextVector
view release on metacpan or search on metacpan
lib/Algorithm/ContextVector.pm view on Meta::CPAN
package Algorithm::ContextVector;
use strict;
use warnings;
our $VERSION = 0.01;
=head1 NAME
Algorithm::ContextVector - Simple implementation based on Data::CosineSimilarity
=head1 SYNOPSIS
my $cv = Algorithm::ContextVector->new( top => 300 );
$cs->add_instance( label => 'label1', attributes => { feature1 => 3, feature2 => 1, feature3 => 10 } );
$cs->add_instance( label => [ 'label2', 'label3' ], attributes => { ... } );
$cs->add_instance( label => ..., attributes => ... );
...
$cv->train;
my $results = $cv->predict( attributes => { ... } );
=head1 DESCRIPTION
Simple implementation based on Data::CosineSimilarity
=head2 $class->new( top => ... )
During the training, keeps the $top most heavy weighted features.
Keeps the complete feature set if omitted.
=cut
use Data::CosineSimilarity;
use Storable;
sub new {
my $class = shift;
my %opts = @_;
return bless {
top => $opts{top},
labels => {},
}, $class;
}
=head2 $class->new_from_file( $filename )
Returns the instance of Algorithm::ContextVector stored in $filename.
=cut
sub new_from_file {
my $class = shift;
my ($file) = @_;
return retrieve($file);
}
=head2 $self->save_to_file( $filename )
Save the $self to $filename using Storable.
=cut
sub save_to_file {
my $self = shift;
my ($file) = @_;
store($self, $file);
}
sub _add_hashrefs {
my $self = shift;
my @list = @_;
my %r;
for my $h (@list) {
for my $key (keys %$h) {
$r{$key} ||= 0;
$r{$key} = $r{$key} + $h->{$key};
}
}
return \%r;
}
=head2 $self->add_instance( label => [ ... ], attributes => { ... } )
=cut
sub add_instance {
my $self = shift;
my %args = @_;
my $attr = $args{attributes} or die 'attributes required';
return unless keys %$attr;
my $labels = $args{label} or die 'label required';
$labels = [ $labels ] unless ref $labels;
for $_ (@$labels) {
$self->{labels}{$_}{features} ||= {};
$self->{labels}{$_}{features} = $self->_add_hashrefs(
$self->{labels}{$_}{features}, $attr
( run in 2.400 seconds using v1.01-cache-2.11-cpan-5b529ec07f3 )