| 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 | |||
| 64 | =head1 NAME | ||
| 65 | =head1 DESCRIPTION | ||
| 66 | =head1 CONTRIBUTOR | ||
| 67 | =over 4 | ||
| 68 | =back | ||
| 69 | =head1 DEDICATION | ||
| 70 | =head1 SEE ALSO | ||
| 71 | =head1 AUTHOR | ||
| 72 | =head1 COPYRIGHT | ||
| 73 | =cut |