⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hypothesis.pm

📁 AI::Categorizer is a framework for automatic text categorization. It consists of a collection of Per
💻 PM
字号:
package AI::Categorizer::Hypothesis;use strict;use Class::Container;use base qw(Class::Container);use Params::Validate qw(:types);__PACKAGE__->valid_params  (   all_categories => {type => ARRAYREF},   scores => {type => HASHREF},   threshold => {type => SCALAR},   document_name => {type => SCALAR, optional => 1},  );sub all_categories { @{$_[0]->{all_categories}} }sub document_name  { $_[0]->{document_name} }sub threshold      { $_[0]->{threshold} }sub best_category {  my ($self) = @_;  my $sc = $self->{scores};  return unless %$sc;  my ($best_cat, $best_score) = each %$sc;  while (my ($key, $val) = each %$sc) {    ($best_cat, $best_score) = ($key, $val) if $val > $best_score;  }  return $best_cat;}sub in_category {  my ($self, $cat) = @_;  return '' unless exists $self->{scores}{$cat};  return $self->{scores}{$cat} > $self->{threshold};}sub categories {  my $self = shift;  return @{$self->{cats}} if $self->{cats};  $self->{cats} = [sort {$self->{scores}{$b} <=> $self->{scores}{$a}}                   grep {$self->{scores}{$_} >= $self->{threshold}}                   keys %{$self->{scores}}];  return @{$self->{cats}};}sub scores {  my $self = shift;  return @{$self->{scores}}{@_};}1;__END__=head1 NAMEAI::Categorizer::Hypothesis - Embodies a set of category assignments=head1 SYNOPSIS use AI::Categorizer::Hypothesis;  # Hypotheses are usually created by the Learner's categorize() method. # (assume here that $learner and $document have been created elsewhere) my $h = $learner->categorize($document);  print "Assigned categories: ", join ', ', $h->categories, "\n"; print "Best category: ", $h->best_category, "\n"; print "Assigned scores: ", join ', ', $h->scores( $h->categories ), "\n"; print "Chosen from: ", join ', ', $h->all_categories, "\n"; print +($h->in_category('geometry') ? '' : 'not '), "assigned to geometry\n";=head1 DESCRIPTIONA Hypothesis embodies a set of category assignments that a categorizermakes about a single document.  Because one may be interested inknowing different kinds of things about the assignments (for instance,what categories were assigned, which category had the highest score,whether a particular category was assigned), we provide a simple classto help facilitate these scenarios.=head1 METHODS=over 4=item new(%parameters)Returns a new Hypothesis object.  Generally a user ofC<AI::Categorize> doesn't create a Hypothesis object directly - theyare returned by the Learner's C<categorize()> method.  However, if youwish to create a Hypothesis directly (maybe passing it some fake datafor testing purposes) you may do so using the C<new()> method.The following parameters are accepted when creating a new Hypothesis:=over 4=item all_categoriesA required parameter which gives the set of all categories that couldpossibly be assigned to.  The categories should be specified as areference to an array of category names (as strings).=item scoresA hash reference indicating the assignment score for each category.Any score higher than the C<threshold> will be considered to beassigned.=item thresholdA number controlling which categories should be assigned - anycategory whose score is greater than or equal to C<threshold> will beassigned, any category whose score is lower than C<threshold> will notbe assigned.=item document_nameAn optional string parameter indicating the name of the document aboutwhich this hypothesis was made.=back=item categories()Returns an ordered list of the categories the document was placed in,with best matches first.  Categories are returned by their string names.=item best_category()Returns the name of the category with the highest score in thishypothesis.  Bear in mind that this category may not actually beassigned if no categories' scores exceed the threshold.=item in_category($name)Returns true or false depending on whether the document was placed inthe given category.=item scores(@names)Returns a list of result scores for the given categories.  Since theinterface is still changing, and since different Learners implementscoring in different ways, not very much can officially be saidabout the scores, except that a good score is higher than a badscore.  Individual Learners will have their own procedures fordetermining scores, so you cannot compare one Learner's score withanother Learner's - for instance, one Learner might always give scoresbetween 0 and 1, and another Learner might always return scores lessthan 0.  You often cannot compare scores from a single Learner on twodifferent categorization tasks either.=item all_categories()Returns the list of category names specified with theC<all_categories> constructor parameter.=item document_name()Returns the value of the C<document_name> parameter specified as aconstructor parameter, or C<undef> if none was specified.=back=head1 AUTHORKen Williams <ken@mathforum.org>=head1 COPYRIGHTThis distribution is free software; you can redistribute it and/ormodify it under the same terms as Perl itself.  These terms apply toevery file in the distribution - if you have questions, please contactthe author.=cut

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -