lib/AI/MicroStructure/Hypothesis.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 66 CC 5 SCAR 16.2
CC
5
Cov
0%
best_category
12.0
in_category
6.0
categories
6.0
CRAP 5.1
1 package AI::MicroStructure::Hypothesis;
2 use base("AI::Categorizer::Hypothesis");
3 use Class::Container;
4 use Params::Validate qw(:types);
5  
6  
7  
8  
9 __PACKAGE__->valid_params
10 (
11 # all_categories => {type => ARRAYREF},
12 # scores => {type => HASHREF},
13 # threshold => {type => SCALAR},
14 document_name => {type => SCALAR, optional => 1},
15 );
16  
17  
18  
19  
20 sub all_categories { @{$_[0]->{all_categories}} }
21 sub document_name { $_[0]->{document_name} }
22 sub threshold { $_[0]->{threshold} }
23  
24 sub new {}
25 sub best_category {
26 my ($self) = @_;
27 my $sc = $self->{scores};
28 return unless %$sc;
29  
30 my ($best_cat, $best_score) = each %$sc;
31 while (my ($key, $val) = each %$sc) {
32 ($best_cat, $best_score) = ($key, $val) if $val > $best_score;
33 }
34 return $best_cat;
35 }
36  
37 sub in_category {
38 my ($self, $cat) = @_;
39 return '' unless exists $self->{scores}{$cat};
40 return $self->{scores}{$cat} > $self->{threshold};
41 }
42  
43 sub categories {
44 my $self = shift;
45 return @{$self->{cats}} if $self->{cats};
46 $self->{cats} = [sort {$self->{scores}{$b} <=> $self->{scores}{$a}}
47 grep {$self->{scores}{$_} >= $self->{threshold}}
48 keys %{$self->{scores}}];
49 return @{$self->{cats}};
50 }
51  
52 sub scores {
53 my $self = shift;
54 return @{$self->{scores}}{@_};
55 }
56  
57 1;
58 # Hypotheses are usually created by the Learner's categorize() method.
59 # (assume here that $learner and $document have been created elsewhere)
60  
61 __END__
62  
63 =head1 NAME
64  
65 AI::Categorizer::Hypothesis - Embodies a set of category assignments
66  
67 =head1 SYNOPSIS
68  
69  
70 =head1 DESCRIPTION
71  
72 A Hypothesis embodies a set of category assignments that a categorizer
73 makes about a single document. Because one may be interested in
74 knowing different kinds of things about the assignments (for instance,
75 what categories were assigned, which category had the highest score,
76 whether a particular category was assigned), we provide a simple class
77 to help facilitate these scenarios.
78  
79 =head1 METHODS
80  
81 =over 4
82  
83 =item new(%parameters)
84  
85 Returns a new Hypothesis object. Generally a user of
86 C<AI::Categorize> doesn't create a Hypothesis object directly - they
87 are returned by the Learner's C<categorize()> method. However, if you
88 wish to create a Hypothesis directly (maybe passing it some fake data
89 for testing purposes) you may do so using the C<new()> method.
90  
91 The following parameters are accepted when creating a new Hypothesis:
92  
93 =over 4
94  
95 =item all_categories
96  
97 A required parameter which gives the set of all categories that could
98 possibly be assigned to. The categories should be specified as a
99 reference to an array of category names (as strings).
100  
101 =item scores
102  
103 A hash reference indicating the assignment score for each category.
104 Any score higher than the C<threshold> will be considered to be
105 assigned.
106  
107 =item threshold
108  
109 A number controlling which categories should be assigned - any
110 category whose score is greater than or equal to C<threshold> will be
111 assigned, any category whose score is lower than C<threshold> will not
112 be assigned.
113  
114 =item document_name
115  
116 An optional string parameter indicating the name of the document about
117 which this hypothesis was made.
118  
119 =back
120  
121  
122 =item categories()
123  
124 Returns an ordered list of the categories the document was placed in,
125 with best matches first. Categories are returned by their string names.
126  
127 =item best_category()
128  
129 Returns the name of the category with the highest score in this
130 hypothesis. Bear in mind that this category may not actually be
131 assigned if no categories' scores exceed the threshold.
132  
133 =item in_category($name)
134  
135 Returns true or false depending on whether the document was placed in
136 the given category.
137  
138 =item scores(@names)
139  
140 Returns a list of result scores for the given categories. Since the
141 interface is still changing, and since different Learners implement
142 scoring in different ways, not very much can officially be said
143 about the scores, except that a good score is higher than a bad
144 score. Individual Learners will have their own procedures for
145 determining scores, so you cannot compare one Learner's score with
146 another Learner's - for instance, one Learner might always give scores
147 between 0 and 1, and another Learner might always return scores less
148 than 0. You often cannot compare scores from a single Learner on two
149 different categorization tasks either.
150  
151 =item all_categories()
152  
153 Returns the list of category names specified with the
154 C<all_categories> constructor parameter.
155  
156 =item document_name()
157  
158 Returns the value of the C<document_name> parameter specified as a
159 constructor parameter, or C<undef> if none was specified.
160  
161 =back
162  
163 =head1 AUTHOR
164  
165 Ken Williams <ken@mathforum.org>
166  
167 =head1 COPYRIGHT
168  
169 This distribution is free software; you can redistribute it and/or
170 modify it under the same terms as Perl itself. These terms apply to
171 every file in the distribution - if you have questions, please contact
172 the author.
173  
174 =cut