| 1 | package Redis::Bayes; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Redis; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use List::Util qw/sum/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Lingua::StopWords qw/getStopWords/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.024'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | has stopwords => ( | |||||||||||||||||||||||||||
| 13 | is => 'rw', | ||||||||||||||||||||||||||||
| 14 | lazy => 1, | ||||||||||||||||||||||||||||
| 15 | builder => '_build_stopwords', | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | has redis => ( | |||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | builder => '_build_redis', | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | has prefix => ( | ||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | 0 | ▶ | default => sub {'bayes:'}, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 26 | 1 | ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | has tokenizer => ( | |||||||||||||||||||||||||||
| 29 | is => 'rw', | ||||||||||||||||||||||||||||
| 30 | lazy => 1, | ||||||||||||||||||||||||||||
| 31 | builder => '_build_tokenizer', | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | sub _build_redis { | ||||||||||||||||||||||||||
|
Subroutine
_build_redis: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 35 | 0 | return Redis->new( | |||||||||||||||||||||||||||
| 36 | reconnect => 2, | ||||||||||||||||||||||||||||
| 37 | every => 100 | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | sub _build_stopwords { | ||||||||||||||||||||||||||
|
Subroutine
_build_stopwords: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 0 | return getStopWords('en'); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _build_tokenizer { | ||||||||||||||||||||||||||
|
Subroutine
_build_tokenizer: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 46 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 48 | 0 | my ($txt) = @_; | |||||||||||||||||||||||||||
| 49 | my @words = grep { | ||||||||||||||||||||||||||||
| 50 | 0 | !$self->stopwords->{$_} | |||||||||||||||||||||||||||
| 51 | 0 | } grep { $_ } split /[\W_-]+/, lc $txt; | |||||||||||||||||||||||||||
| 52 | 0 | return @words; | |||||||||||||||||||||||||||
| 53 | 0 | }; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | sub _tokenize { | ||||||||||||||||||||||||||
|
Subroutine
_tokenize: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 57 | 0 | my ($self,$txt) = @_; | |||||||||||||||||||||||||||
| 58 | 0 | return $self->tokenizer->($txt); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub _word_counts { | ||||||||||||||||||||||||||
|
Subroutine
_word_counts: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 62 | 0 | my ($self, @words) = @_; | |||||||||||||||||||||||||||
| 63 | 0 | my %counts; | |||||||||||||||||||||||||||
| 64 | 0 | $counts{$_}++ for @words; | |||||||||||||||||||||||||||
| 65 | 0 | return \%counts; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub flush { | ||||||||||||||||||||||||||
|
Subroutine
flush: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 70 | 0 | for my $cat ($self->redis->smembers($self->prefix . 'categories')) { | |||||||||||||||||||||||||||
| 71 | 0 | $self->redis->del($self->prefix . $cat); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | $self->redis->del($self->prefix . 'categories'); | |||||||||||||||||||||||||||
| 74 | 0 | return 1; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub train { | ||||||||||||||||||||||||||
|
Subroutine
train: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | my ($self,$category,$text) = @_; | |||||||||||||||||||||||||||
| 79 | 0 | $self->redis->sadd($self->prefix . 'categories', $category); | |||||||||||||||||||||||||||
| 80 | 0 | my $wc = $self->_word_counts($self->_tokenize($text)); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | while (my ($w,$c) = each(%$wc)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $self->redis->hincrby($self->prefix . $category, $w, $c); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | return 1; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | sub untrain { | ||||||||||||||||||||||||||
|
Subroutine
untrain: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 0 | my ($self, $category, $text) = @_; | |||||||||||||||||||||||||||
| 89 | 0 | my $wc = $self->_word_counts($self->_tokenize($text)); | |||||||||||||||||||||||||||
| 90 | 0 | my ($cur,$new); | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | while (my ($w,$c) = each(%$wc)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | $cur = $self->redis->hget($self->prefix . $category, $w); | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | if ($cur) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | $new = int($cur) - $c; | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($new > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 0 | $self->redis->hset($self->prefix . $category, $w, $new) | |||||||||||||||||||||||||||
| 97 | }else{ | ||||||||||||||||||||||||||||
| 98 | 0 | $self->redis->hdel($self->prefix . $category, $w); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ($self->_total($category) == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | $self->redis->del($self->prefix . $category); | |||||||||||||||||||||||||||
| 104 | 0 | $self->redis->srem($self->prefix . 'categories', $category); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | 0 | return 1; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub classify { | ||||||||||||||||||||||||||
|
Subroutine
classify: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | my ($self, $text, $args) = @_; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | $args //= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args //= {}
MC/DC0%0/1
$args //= {}
$args
Truth table0%0/2
A
A= $args
| |||||||||||||||||||||||||||||
| 112 | 0 | my $scores = $self->_score($text); | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | return if (not $scores); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | my @d = sort { $scores->{$b} <=> $scores->{$a} } keys %$scores; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | return 'empty data store' if (not @d); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ($args->{return_all}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | return join ',', map { "$_:" . $scores->{$_} } @d; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return ($scores->{$d[0]} ? $d[0] : 'unclassified'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub _score { | ||||||||||||||||||||||||||
|
Subroutine
_score: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($self, $text) = @_; | |||||||||||||||||||||||||||
| 124 | 0 | my $wc = $self->_word_counts($self->_tokenize($text)); | |||||||||||||||||||||||||||
| 125 | 0 | my %scores; | |||||||||||||||||||||||||||
| 126 | 0 | for my $category ($self->redis->smembers($self->prefix . 'categories')) { | |||||||||||||||||||||||||||
| 127 | 0 | my $total = $self->_total($category); | |||||||||||||||||||||||||||
| 128 | 0 | ▶ | next if ($total == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | $scores{$category} = 0.0; | |||||||||||||||||||||||||||
| 130 | 0 | ▶ | while (my ($w,$c) = each %$wc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my $score = $self->redis->hget($self->prefix . $category, $w); | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | croak "invalid values in Redis" unless (not $score or $score > 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $score or $score > 0
MC/DC0%0/2
not $score or $score > 0
not $score
$score > 0
Truth table0%0/3
not(A) or B
A= not $scoreB= $score > 0
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | next if not $score; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $scores{$category} += $score / $total; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 0 | return \%scores; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | sub _total { | ||||||||||||||||||||||||||
|
Subroutine
_total: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 141 | 0 | my ($self, $category) = @_; | |||||||||||||||||||||||||||
| 142 | 0 | my $total = sum($self->redis->hvals($self->prefix . $category)); | |||||||||||||||||||||||||||
| 143 | 0 | ▶ | return 0 if (not $total); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | croak "error in hvals" unless ($total >= 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | return $total; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 1 | 1; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||