AI-CBR

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

use strict;
use warnings;
use Module::Build;

my $builder = Module::Build->new(
    module_name         => 'AI::CBR',
    license             => 'perl',
    dist_author         => 'Darko Obradovic <dobradovic@gmx.de>',
    dist_version_from   => 'lib/AI/CBR.pm',
    build_requires => {
        'Test::More' => 0,
    },
    add_to_cleanup      => [ 'AI-CBR-*' ],
    create_makefile_pl => 'traditional',
);

$builder->create_build_script();

Changes  view on Meta::CPAN

Revision history for AI-CBR

0.02    July 31, 2009
        Some documentation fixes.

0.01    July 30, 2009
        First version, released on an unsuspecting world.

META.yml  view on Meta::CPAN

---
name: AI-CBR
version: 0.02
author:
  - 'Darko Obradovic <dobradovic@gmx.de>'
abstract: Framework for Case-Based Reasoning
license: perl
resources:
  license: http://dev.perl.org/licenses/
build_requires:
  Test::More: 0
provides:
  AI::CBR:
    file: lib/AI/CBR.pm
    version: 0.02
  AI::CBR::Case:
    file: lib/AI/CBR/Case.pm
  AI::CBR::Case::Compound:
    file: lib/AI/CBR/Case/Compound.pm
  AI::CBR::Retrieval:
    file: lib/AI/CBR/Retrieval.pm
  AI::CBR::Sim:
    file: lib/AI/CBR/Sim.pm
generated_by: Module::Build version 0.2808
meta-spec:
  url: http://module-build.sourceforge.net/META-spec-v1.2.html
  version: 1.2

Makefile.PL  view on Meta::CPAN

# Note: this file was auto-generated by Module::Build::Compat version 0.03
use ExtUtils::MakeMaker;
WriteMakefile
(
          'NAME' => 'AI::CBR',
          'VERSION_FROM' => 'lib/AI/CBR.pm',
          'PREREQ_PM' => {
                           'Test::More' => '0'
                         },
          'INSTALLDIRS' => 'site',
          'EXE_FILES' => [],
          'PL_FILES' => {}
        )
;

README  view on Meta::CPAN

file from a module distribution so that people browsing the archive
can use it to get an idea of the module's uses. It is usually a good idea
to provide version information here so that people can decide whether
fixes for the module are worth downloading.


INSTALLATION

To install this module, run the following commands:

	perl Makefile.PL
	make
	make test
	make install

Alternatively, to install with Module::Build, you can use the following commands:

	perl Build.PL
	./Build
	./Build test
	./Build install

SUPPORT AND DOCUMENTATION

After installing, you can find documentation for this module with the
perldoc command.

    perldoc AI::CBR::Case

You can also look for information at:

    RT, CPAN's request tracker
        http://rt.cpan.org/NoAuth/Bugs.html?Dist=AI-CBR

    AnnoCPAN, Annotated CPAN documentation
        http://annocpan.org/dist/AI-CBR

    CPAN Ratings
        http://cpanratings.perl.org/d/AI-CBR

    Search CPAN
        http://search.cpan.org/dist/AI-CBR


COPYRIGHT AND LICENCE

Copyright (C) 2009 Darko Obradovic

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

lib/AI/CBR.pm  view on Meta::CPAN


Version 0.02

=cut

our $VERSION = '0.02';


=head1 SYNOPSIS

    use AI::CBR::Sim qw(sim_eq ...);
    use AI::CBR::Case;
    use AI::CBR::Retrieval;

    my $case = AI::CBR::Case->new(...);
    my $r = AI::CBR::Retrieval->new($case, \@case_base);
    ...


=head1 DESCRIPTION

Framework for Case-Based Reasoning in Perl.
For an overview, please see my slides from YAPC::EU 2009.

In brief, you need to specifiy an L<AI::CBR::Case>
with the help of similarity functions from L<AI::CBR::Sim>.
Then you can find similar cases from a case-base

lib/AI/CBR/Case/Compound.pm  view on Meta::CPAN


AI::CBR::Case::Compound - compound case definition and representation


=head1 SYNOPSIS

Define and initialise a compound (or object-oriented) case.
This is a case consisting of multiple object definitions related in some way.
In a productive system, you will want to encapsulate this.

    use AI::CBR::Case::Compound;
    use AI::CBR::Sim qw(sim_eq sim_dist);

    # assume we sell travels with flight and hotel
    # shortcut one-time generated case
    my $case = AI::CBR::Case::Compound->new(
    	# flight object
    	{
			flight_start  => { value => 'FRA', sim => \&sim_eq },
			flight_target => { value => 'LIS', sim => \&sim_eq },
			price         => { value => 300,   sim => \&sim_dist, param => 200 },
		},
		# hotel object
		{
			stars => { value => 3,  sim => \&sim_dist, param => 2 },
			rate  => { value => 60, sim => \&sim_dist, param => 200 },		
		},
    );

    ...

=head1 METHODS

=head2 new

Creates a new compound case specification.
Pass a list of hash references as argument.
Each hash reference is the same specification as passed to L<AI::CBR::Case>.

=cut

sub new {
	my ($class, @definitions) = @_;
	
	# set default weights if unspecified
	foreach my $attributes (@definitions) {
		foreach (keys %$attributes) {
			$attributes->{$_}->{weight} = $DEFAULT_WEIGHT unless defined $attributes->{$_}->{weight};
		}
	}
	
	my $self = \@definitions;
	bless $self, $class;
	return $self;
}


=head2 set_values

Pass a flat hash of attribute keys and values.
This will overwrite existing values, and can thus be used as a faster method
for generating new cases with the same specification.
Notice that keys in the different specifications of the compound object may not have the same name!

=cut

sub set_values {
	my ($self, %values) = @_;
	foreach my $spec (@$self) {
		foreach (keys %$spec) {
			$spec->{$_}->{value} = $values{$_};
		}
	}
}


=head1 SEE ALSO

See L<AI::CBR> for an overview of the framework.


=head1 AUTHOR

t/00-load.t  view on Meta::CPAN

#!perl -T

use Test::More tests => 4;

BEGIN {
	use_ok( 'AI::CBR::Sim' );
	use_ok( 'AI::CBR::Case' );
	use_ok( 'AI::CBR::Retrieval' );
	use_ok( 'AI::CBR::Case::Compound' );
}

diag( "Testing AI::CBR::Case $AI::CBR::Case::VERSION, Perl $], $^X" );

t/02-case.t  view on Meta::CPAN

#!perl -T

use Test::More tests => 2;

use AI::CBR::Sim qw(sim_dist sim_frac sim_eq sim_set);
use AI::CBR::Case;


my $case1 = AI::CBR::Case->new(
	age      => { value => 30,             sim => \&sim_amount },
	gender   => { value => 'male',         sim => \&sim_eq     },
	job      => { value => 'programmer',   sim => \&sim_eq     },
	symptoms => { value => [qw(headache)], sim => \&sim_set,   weight =>2 },
);

my $weights_at_1 = int grep { $case1->{$_}->{weight} == 1 } keys %$case1;
my $weights_at_2 = int grep { $case1->{$_}->{weight} == 2 } keys %$case1;
is($weights_at_1, 3, 'default weights set to 1');
is($weights_at_2, 1, 'symptom weight set to 2');

t/03-retrieval.t  view on Meta::CPAN

#!perl -T

use Test::More tests => 7;

use AI::CBR::Sim qw(sim_frac sim_eq sim_set);
use AI::CBR::Case;
use AI::CBR::Retrieval;


my $case_base = [
	{id=>1, age=>25, gender=>'male',   job=>'manager',    symptoms=>[qw(headache)],       reason=>'stress' },
	{id=>2, age=>40, gender=>'male',   job=>'programmer', symptoms=>[qw(headache cough)], reason=>'flu'    },
	{id=>3, age=>30, gender=>'female', job=>'programmer', symptoms=>[qw(cough)],          reason=>'flu'    },
	{id=>4, age=>25, gender=>'male',   job=>'programmer', symptoms=>[qw(headache)],       reason=>'alcohol'},
];

my $case1 = AI::CBR::Case->new(
	age      => { value => 30,             sim => \&sim_frac },
	gender   => { value => 'male',         sim => \&sim_eq   },
	job      => { value => 'programmer',   sim => \&sim_eq   },
	symptoms => { value => [qw(headache)], sim => \&sim_set,   weight =>2 },
);


my $retrieval = AI::CBR::Retrieval->new($case1, $case_base);

$retrieval->compute_sims();

# check similarities
is($case_base->[0]->{_sim}, (5/6+1+0+2*1/1)/5, 'sim 1 correct'); # ~0.77
is($case_base->[1]->{_sim}, (3/4+1+1+2*1/2)/5, 'sim 2 correct'); # 0.75

t/04-case-compound.t  view on Meta::CPAN

#!perl -T

use Test::More tests => 5;

use AI::CBR::Sim qw(sim_dist sim_frac sim_eq sim_set);
use AI::CBR::Case::Compound;
use AI::CBR::Retrieval;


my $case1 = AI::CBR::Case::Compound->new(
	# flight object
	{
		start  => { value => 'FRA', sim => \&sim_eq },
		target => { value => 'LIS', sim => \&sim_eq },
		price  => { value => 300,   sim => \&sim_dist, param => 200 },
	},
	# hotel object
	{
		stars => { value => 3,  sim => \&sim_dist, param => 2 },
		rate  => { value => 60, sim => \&sim_dist, param => 200 },		
	},
);

is(int @$case1, 2, '2 specs');


my @case_base = (
	{id=>1, start=>'FRA', target=>'DBV', price=>200, stars=>5, rate=>160}, # ~0.35
	{id=>2, start=>'FRA', target=>'LIS', price=>350, stars=>4, rate=>80},  # ~0.80
);

my $r = AI::CBR::Retrieval->new($case1, \@case_base);
$r->compute_sims();

is($r->{candidates}->[0]->{id}, 2, 'sim of id 2 is higher');
is($r->{candidates}->[1]->{id}, 1, 'sim of id 1 is lower');

is($case_base[0]->{_sim}, sqrt(0.5*0.25), 'sim of id 1 correct');
is($case_base[1]->{_sim}, sqrt((2.75/3)*(1.4/2)), 'sim of id 2 correct');

t/boilerplate.t  view on Meta::CPAN

#!perl -T

use strict;
use warnings;
use Test::More tests => 4;

sub not_in_file_ok {
    my ($filename, %regex) = @_;
    open( my $fh, '<', $filename )
        or die "couldn't open $filename for reading: $!";

    my %violated;

    while (my $line = <$fh>) {
        while (my ($desc, $regex) = each %regex) {
            if ($line =~ $regex) {
                push @{$violated{$desc}||=[]}, $.;
            }
        }
    }

    if (%violated) {
        fail("$filename contains boilerplate text");
        diag "$_ appears on lines @{$violated{$_}}" for keys %violated;
    } else {
        pass("$filename contains no boilerplate text");
    }
}

sub module_boilerplate_ok {
    my ($module) = @_;
    not_in_file_ok($module =>
        'the great new $MODULENAME'   => qr/ - The great new /,
        'boilerplate description'     => qr/Quick summary of what the module/,
        'stub function definition'    => qr/function[12]/,
    );
}

TODO: {
  local $TODO = "Need to replace the boilerplate text";

  not_in_file_ok(README =>
    "The README is used..."       => qr/The README is used/,
    "'version information here'"  => qr/to provide version information/,
  );

  not_in_file_ok(Changes =>
    "placeholder date/time"       => qr(Date/time)
  );

  module_boilerplate_ok('lib/AI/CBR/Case.pm');
  module_boilerplate_ok('lib/AI/CBR/Sim.pm');


}

t/pod-coverage.t  view on Meta::CPAN

use strict;
use warnings;
use Test::More;

# Ensure a recent version of Test::Pod::Coverage
my $min_tpc = 1.08;
eval "use Test::Pod::Coverage $min_tpc";
plan skip_all => "Test::Pod::Coverage $min_tpc required for testing POD coverage"
    if $@;

# Test::Pod::Coverage doesn't require a minimum Pod::Coverage version,
# but older versions don't recognize some common documentation styles
my $min_pc = 0.18;
eval "use Pod::Coverage $min_pc";
plan skip_all => "Pod::Coverage $min_pc required for testing POD coverage"
    if $@;

all_pod_coverage_ok();

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.990 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )