| 1 | package AI::NaiveBayes::Classification; | ||||||||||
| 2 | 3 | $AI::NaiveBayes::Classification::VERSION = '0.02'; | |||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 3 | has features => (is => 'ro', isa => 'HashRef[HashRef]', required => 1); | |||||||||
| 9 | 3 | has label_sums => (is => 'ro', isa => 'HashRef', required => 1); | |||||||||
| 10 | 3 | has best_category => (is => 'ro', isa => 'Str', lazy_build => 1); | |||||||||
| 11 | |||||||||||
| 12 | sub _build_best_category { | ||||||||||
| 13 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_best_category: called CC=3 SCAR=11.0
| |||||||||||
| 14 | 1 | my $sc = $self->label_sums; | |||||||||
| 15 | |||||||||||
| 16 | 1 | my ($best_cat, $best_score) = each %$sc; | |||||||||
| 17 | 1 | ▶ | while (my ($key, $val) = each %$sc) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 1 | ▶ | ($best_cat, $best_score) = ($key, $val) if $val > $best_score; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | } | ||||||||||
| 20 | 1 | return $best_cat; | |||||||||
| 21 | } | ||||||||||
| 22 | |||||||||||
| 23 | sub find_predictors{ | ||||||||||
| 24 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
find_predictors: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 25 | |||||||||||
| 26 | 1 | my $best_cat = $self->best_category; | |||||||||
| 27 | 1 | my $features = $self->features; | |||||||||
| 28 | 1 | my @predictors; | |||||||||
| 29 | 1 | for my $feature ( keys %$features ) { | |||||||||
| 30 | 2 | for my $cat ( keys %{ $features->{$feature } } ){ | |||||||||
| 31 | 4 | ▶ | next if $cat eq $best_cat; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 2 | push @predictors, [ $feature, $features->{$feature}{$best_cat} - $features->{$feature}{$cat} ]; | |||||||||
| 33 | } | ||||||||||
| 34 | } | ||||||||||
| 35 | 1 | @predictors = sort { abs( $b->[1] ) <=> abs( $a->[1] ) } @predictors; | |||||||||
| 36 | 1 | return $best_cat, @predictors; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | |||||||||||
| 40 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||
| 41 | |||||||||||
| 42 | 3 | 1; | |||||||||
| 43 | |||||||||||
| 44 | =pod | ||||||||||
| 45 | |||||||||||
| 46 | =encoding UTF-8 | ||||||||||
| 47 | |||||||||||
| 48 | =head1 NAME | ||||||||||
| 49 | |||||||||||
| 50 | AI::NaiveBayes::Classification - The result of a bayesian classification | ||||||||||
| 51 | |||||||||||
| 52 | =head1 VERSION | ||||||||||
| 53 | |||||||||||
| 54 | version 0.02 | ||||||||||
| 55 | |||||||||||
| 56 | =head1 SYNOPSIS | ||||||||||
| 57 | |||||||||||
| 58 | my $result = $classifier->classify({bar => 3, blurp => 2}); | ||||||||||
| 59 | # $result is an AI::NaiveBayes::Classification object | ||||||||||
| 60 | say $result->best_category; | ||||||||||
| 61 | my $predictors = $result->find_predictors; | ||||||||||
| 62 | |||||||||||
| 63 | =head1 DESCRIPTION | ||||||||||
| 64 | |||||||||||
| 65 | AI::NaiveBayes::Classification represents the result of a bayesian classification, | ||||||||||
| 66 | produced by AI::NaiveBayes classifier. | ||||||||||
| 67 | |||||||||||
| 68 | =head1 METHODS | ||||||||||
| 69 | |||||||||||
| 70 | =over 4 | ||||||||||
| 71 | |||||||||||
| 72 | =item C<best_category()> | ||||||||||
| 73 | |||||||||||
| 74 | Returns a string being a label that suits given document the best. | ||||||||||
| 75 | |||||||||||
| 76 | =item C<find_predictors()> | ||||||||||
| 77 | |||||||||||
| 78 | This method returns the C<best_category()>, as well as the list of all the predictors | ||||||||||
| 79 | along with their influence on the best category selected. So the second value | ||||||||||
| 80 | returned is a list of array references, where each one contains a string being a | ||||||||||
| 81 | single feature and a number describing its influence on the result. So the | ||||||||||
| 82 | second part of the result may look like this: | ||||||||||
| 83 | |||||||||||
| 84 | ( | ||||||||||
| 85 | [ 'activities', 1.2511540632952 ], | ||||||||||
| 86 | [ 'over', -1.0269523272981 ], | ||||||||||
| 87 | [ 'provide', 0.8280157033269 ], | ||||||||||
| 88 | [ 'natural', 0.7361042359385 ], | ||||||||||
| 89 | [ 'against', -0.6923354975173 ], | ||||||||||
| 90 | ) | ||||||||||
| 91 | |||||||||||
| 92 | =back | ||||||||||
| 93 | |||||||||||
| 94 | =head1 SEE ALSO | ||||||||||
| 95 | |||||||||||
| 96 | AI::NaiveBayes (3), AI::Classifier(3) | ||||||||||
| 97 | |||||||||||
| 98 | =head1 AUTHORS | ||||||||||
| 99 | |||||||||||
| 100 | =over 4 | ||||||||||
| 101 | |||||||||||
| 102 | =item * | ||||||||||
| 103 | |||||||||||
| 104 | Zbigniew Lukasiak <zlukasiak@opera.com> | ||||||||||
| 105 | |||||||||||
| 106 | =item * | ||||||||||
| 107 | |||||||||||
| 108 | Tadeusz Sośnierz <tsosnierz@opera.com> | ||||||||||
| 109 | |||||||||||
| 110 | =item * | ||||||||||
| 111 | |||||||||||
| 112 | Ken Williams <ken@mathforum.org> | ||||||||||
| 113 | |||||||||||
| 114 | =back | ||||||||||
| 115 | |||||||||||
| 116 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 117 | |||||||||||
| 118 | This software is copyright (c) 2012 by Opera Software ASA. | ||||||||||
| 119 | |||||||||||
| 120 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 121 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | __END__ | ||||||||||
| 126 | |||||||||||
| 127 | # ABSTRACT: The result of a bayesian classification | ||||||||||
| 128 | |||||||||||