AI-CBR

 view release on metacpan or  search on metacpan

Build.PL  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use strict;
 
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();

META.yml  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
---
name: AI-CBR
version: 0.02
author:
  - 'Darko Obradovic <dobradovic@gmx.de>'
abstract: Framework for Case-Based Reasoning
license: perl
resources:
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:

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

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
=head1 AUTHOR
 
Darko Obradovic, C<< <dobradovic at gmx.de> >>
 
=head1 BUGS
 
Please report any bugs or feature requests to C<bug-ai-cbr at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AI-CBR>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
 
 
 
 
=head1 SUPPORT
 
You can find documentation for this module with the perldoc command.
 
    perldoc AI::CBR

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

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
=item * Search CPAN
 
 
=back
 
 
=head1 COPYRIGHT & LICENSE
 
Copyright 2009 Darko Obradovic, all rights reserved.
 
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
 
 
=cut
 
1; # End of AI::CBR::Case

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package AI::CBR::Case;
 
use strict;
 
our $DEFAULT_WEIGHT = 1;
 
 
=head1 NAME
 
AI::CBR::Case - case definition and representation
 
 
=head1 SYNOPSIS
 
Define and initialise a case.
In a productive system, you will want to encapsulate this.
 
    use AI::CBR::Case;
    use AI::CBR::Sim qw(sim_frac sim_eq sim_set);

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

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 
=head1 AUTHOR
 
Darko Obradovic, C<< <dobradovic at gmx.de> >>
 
=head1 BUGS
 
Please report any bugs or feature requests to C<bug-ai-cbr at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AI-CBR>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
 
 
 
 
=head1 SUPPORT
 
You can find documentation for this module with the perldoc command.
 
    perldoc AI::CBR::Case

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

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
=item * Search CPAN
 
 
=back
 
 
=head1 COPYRIGHT & LICENSE
 
Copyright 2009 Darko Obradovic, all rights reserved.
 
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
 
 
=cut
 
1; # End of AI::CBR::Case

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
use strict;
 
our $DEFAULT_WEIGHT = 1;
 
 
=head1 NAME
 
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);

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

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
 
=head1 AUTHOR
 
Darko Obradovic, C<< <dobradovic at gmx.de> >>
 
=head1 BUGS
 
Please report any bugs or feature requests to C<bug-ai-cbr at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AI-CBR>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
 
 
 
 
=head1 SUPPORT
 
You can find documentation for this module with the perldoc command.
 
    perldoc AI::CBR::Case::Compound

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

127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
=item * Search CPAN
 
 
=back
 
 
 
=head1 COPYRIGHT & LICENSE
 
Copyright 2009 Darko Obradovic, all rights reserved.
 
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
 
 
=cut
 
1; # End of AI::CBR::Case::Compound

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

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                        $candidate->{_sim} *= _nrt($num_queries, $sum_sims / $sum_weights);
                }
        }
        my @candidates_sorted = sort { $b->{_sim} <=> $a->{_sim} } @{$self->{candidates}};
        $self->{candidates} = \@candidates_sorted;
}
 
 
=head2 RETRIEVAL METHODS
 
Use one of these methods to get the similar cases you are interested into.
 
=head3 most_similar_candidate
 
Returns the most similar candidate.
No parameters.
 
=cut
 
sub most_similar_candidate {
        my ($self) = @_;

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

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
 
=head1 AUTHOR
 
Darko Obradovic, C<< <dobradovic at gmx.de> >>
 
=head1 BUGS
 
Please report any bugs or feature requests to C<bug-ai-cbr at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AI-CBR>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
 
 
 
 
=head1 SUPPORT
 
You can find documentation for this module with the perldoc command.
 
    perldoc AI::CBR::Retrieval

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

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
=item * Search CPAN
 
 
=back
 
 
=head1 COPYRIGHT & LICENSE
 
Copyright 2009 Darko Obradovic, all rights reserved.
 
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
 
 
=cut
 
1; # End of AI::CBR::Retrieval

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

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
=item * sim_set
 
=back
 
 
=head1 FUNCTIONS
 
=head2 sim_dist
 
Works for any numeric values.
Suitable when you are interested into the difference of values in a given range.
Returns the fraction of the difference of the values with respect to a given maximum range of interest.
The madatory third argument is this range.
 
        sim_dist(26, 22, 10); # returns 0.4
        sim_dist(-2, 1, 100); # returns 0.03
 
=cut
 
sub sim_dist {
        my ($a, $b, $range) = @_;
        return 1 if $a == $b;
        my $dist = abs($a - $b);
        return 0 if $dist >= $range;
        return 1 - $dist / $range;
}
 
 
=head2 sim_frac
 
Works for non-negative numeric values.
Suitable when you are only interested into their relative difference with respect to 0.
Returns the fraction of the smaller argument with respect to the higher one.
 
        sim_frac(3, 2); # returns 0.67
        sim_frac(40, 50); # returns 0.8
 
=cut
 
sub sim_frac {
        my ($a, $b) = @_;
        return 1 if $a == $b;
        return 0 if $a * $b == 0;
        return $a > $b ? $b / $a : $a / $b;
}
 
 
=head2 sim_eq
 
Works for any textual value.
Suitable when you are interested only into equality/inequality.
Returns 1 in case of equality, 0 in case of inequality.
No third argument.
 
        sim_eq('foo', 'bar'); # returns 0
        sim_eq('foo', 'foo'); # returns 1
 
=cut
 
sub sim_eq {
        return $_[0] eq $_[1] ? 1 : 0;
}
 
 
=head2 sim_set
 
Works for sets/lists of textual values.
Suitable when you are interested into overlap of the two sets.
Arguments are two array references with textual values.
Returns the number of elements in the intersection
divided by the number of elements in the union.
No third argument.
 
        sim_set([qw/a b c/], [qw/b c d/]); # returns 0.5
        sim_set([qw/a b c/], [qw/c/]); # returns 0.33
 
=cut

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

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
 
=head1 AUTHOR
 
Darko Obradovic, C<< <dobradovic at gmx.de> >>
 
=head1 BUGS
 
Please report any bugs or feature requests to C<bug-ai-cbr at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=AI-CBR>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
 
 
 
 
=head1 SUPPORT
 
You can find documentation for this module with the perldoc command.
 
    perldoc AI::CBR::Sim

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

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
=item * Search CPAN
 
 
=back
 
 
=head1 COPYRIGHT & LICENSE
 
Copyright 2009 Darko Obradovic, all rights reserved.
 
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
 
 
=cut
 
1; # End of AI::CBR::Sim

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!perl -T
 
use Test::More tests => 7;
 
use AI::CBR::Sim qw(sim_frac sim_eq sim_set);
 
 
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 },



( run in 1.086 second using v1.01-cache-2.11-cpan-26ccb49234f )