| 1 | package AI::Categorizer::Document; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 11 | ▶ | use Class::Container; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use base qw(Class::Container); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 11 | ▶ | use Params::Validate qw(:types); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use AI::Categorizer::ObjectSet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use AI::Categorizer::FeatureVector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | __PACKAGE__->valid_params | ||||||||||||||||||||||||||||
| 12 | ( | ||||||||||||||||||||||||||||
| 13 | name => { | ||||||||||||||||||||||||||||
| 14 | type => SCALAR, | ||||||||||||||||||||||||||||
| 15 | }, | ||||||||||||||||||||||||||||
| 16 | categories => { | ||||||||||||||||||||||||||||
| 17 | type => ARRAYREF, | ||||||||||||||||||||||||||||
| 18 | default => [], | ||||||||||||||||||||||||||||
| 19 | callbacks => { 'all are Category objects' => | ||||||||||||||||||||||||||||
| 20 | 66 | ▶ | sub { ! grep !UNIVERSAL::isa($_, 'AI::Categorizer::Category'), @{$_[0]} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | }, | ||||||||||||||||||||||||||||
| 22 | 11 | public => 0, | |||||||||||||||||||||||||||
| 23 | }, | ||||||||||||||||||||||||||||
| 24 | stopwords => { | ||||||||||||||||||||||||||||
| 25 | type => ARRAYREF|HASHREF, | ||||||||||||||||||||||||||||
| 26 | default => {}, | ||||||||||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||||||||||
| 28 | content => { | ||||||||||||||||||||||||||||
| 29 | type => HASHREF|SCALAR, | ||||||||||||||||||||||||||||
| 30 | default => undef, | ||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||
| 32 | parse => { | ||||||||||||||||||||||||||||
| 33 | type => SCALAR, | ||||||||||||||||||||||||||||
| 34 | optional => 1, | ||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||
| 36 | parse_handle => { | ||||||||||||||||||||||||||||
| 37 | type => HANDLE, | ||||||||||||||||||||||||||||
| 38 | optional => 1, | ||||||||||||||||||||||||||||
| 39 | }, | ||||||||||||||||||||||||||||
| 40 | features => { | ||||||||||||||||||||||||||||
| 41 | isa => 'AI::Categorizer::FeatureVector', | ||||||||||||||||||||||||||||
| 42 | optional => 1, | ||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | content_weights => { | ||||||||||||||||||||||||||||
| 45 | type => HASHREF, | ||||||||||||||||||||||||||||
| 46 | default => {}, | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | front_bias => { | ||||||||||||||||||||||||||||
| 49 | type => SCALAR, | ||||||||||||||||||||||||||||
| 50 | default => 0, | ||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||
| 52 | use_features => { | ||||||||||||||||||||||||||||
| 53 | type => HASHREF|UNDEF, | ||||||||||||||||||||||||||||
| 54 | default => undef, | ||||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | stemming => { | ||||||||||||||||||||||||||||
| 57 | type => SCALAR|UNDEF, | ||||||||||||||||||||||||||||
| 58 | optional => 1, | ||||||||||||||||||||||||||||
| 59 | }, | ||||||||||||||||||||||||||||
| 60 | stopword_behavior => { | ||||||||||||||||||||||||||||
| 61 | type => SCALAR, | ||||||||||||||||||||||||||||
| 62 | default => "stem", | ||||||||||||||||||||||||||||
| 63 | }, | ||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 11 | __PACKAGE__->contained_objects | |||||||||||||||||||||||||||
| 67 | ( | ||||||||||||||||||||||||||||
| 68 | features => { delayed => 1, | ||||||||||||||||||||||||||||
| 69 | class => 'AI::Categorizer::FeatureVector' }, | ||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | ### Constructors | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 11 | my $NAME = 'a'; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 94 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 94 | my $pkg = shift; | |||||||||||||||||||||||||||
| 78 | 94 | my $self = $pkg->SUPER::new(name => $NAME++, # Use a default name | |||||||||||||||||||||||||||
| 79 | @_); | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Get efficient internal data structures | ||||||||||||||||||||||||||||
| 82 | 94 | $self->{categories} = new AI::Categorizer::ObjectSet( @{$self->{categories}} ); | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 94 | $self->_fix_stopwords; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # A few different ways for the caller to initialize the content | ||||||||||||||||||||||||||||
| 87 | 94 | ▶ | if (exists $self->{parse}) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 88 | 1 | $self->parse(content => delete $self->{parse}); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | } elsif (exists $self->{parse_handle}) { | ||||||||||||||||||||||||||||
| 91 | 0 | $self->parse_handle(handle => delete $self->{parse_handle}); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | } elsif (defined $self->{content}) { | ||||||||||||||||||||||||||||
| 94 | # Allow a simple string as the content | ||||||||||||||||||||||||||||
| 95 | 88 | ▶ | $self->{content} = { body => $self->{content} } unless ref $self->{content}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 94 | ▶ | $self->finish if $self->{content}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 94 | return $self; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 94 | ▶ | sub _fix_stopwords { | ||||||||||||||||||||||||||
|
Subroutine
_fix_stopwords: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||
| 103 | 94 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Convert to hash | ||||||||||||||||||||||||||||
| 106 | 123 | $self->{stopwords} = { map {($_ => 1)} @{ $self->{stopwords} } } | |||||||||||||||||||||||||||
| 107 | 94 | ▶ | if UNIVERSAL::isa($self->{stopwords}, 'ARRAY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 94 | my $s = $self->{stopwords}; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # May need to perform stemming on the stopwords | ||||||||||||||||||||||||||||
| 112 | 94 | ▶ | return unless keys %$s; # No point in doing anything if there are no stopwords | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 27 | ▶ | return unless $self->{stopword_behavior} eq 'stem'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 25 | ▶ | return if !defined($self->{stemming}) or $self->{stemming} eq 'none'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $self->{"stemming"} or $self->{"stemming"} eq "none"
MC/DC50%1/2
not defined $self->{"stemming"} or $self->{"stemming"} eq "none"
not defined $self->{"stemming"}
$self->{"stemming"} eq "none"
Truth table66%2/3
not(A) or B
A= not defined $self->{"stemming"}B= $self->{"stemming"} eq "none"
| |||||||||||||||||||||||||||||
| 115 | 1 | ▶ | return if $s->{___stemmed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 1 | my @keys = keys %$s; | |||||||||||||||||||||||||||
| 118 | 1 | %$s = (); | |||||||||||||||||||||||||||
| 119 | 1 | $self->stem_words(\@keys); | |||||||||||||||||||||||||||
| 120 | 1 | $s->{$_} = 1 foreach @keys; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # This flag is attached to the stopword structure itself so that | ||||||||||||||||||||||||||||
| 123 | # other documents will notice it. | ||||||||||||||||||||||||||||
| 124 | 1 | $s->{___stemmed} = 1; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 92 | ▶ | sub finish { | ||||||||||||||||||||||||||
|
Subroutine
finish: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 92 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | 92 | $self->create_feature_vector; | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | # Now we're done with all the content stuff | ||||||||||||||||||||||||||||
| 132 | 92 | delete @{$self}{'content', 'content_weights', 'stopwords', 'use_features'}; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # Parse a document format - a virtual method | ||||||||||||||||||||||||||||
| 137 | sub parse; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | ▶ | sub parse_handle { | ||||||||||||||||||||||||||
|
Subroutine
parse_handle: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 3 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 141 | 3 | ▶ | my $fh = $args{handle} or die "No 'handle' argument given to parse_handle()"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 3 | return $self->parse( content => join '', <$fh> ); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | ### Accessors | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 157 | ▶ | sub name { $_[0]->{name} } | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 3 | ▶ | sub stopword_behavior { $_[0]->{stopword_behavior} } | ||||||||||||||||||||||||||
|
Subroutine
stopword_behavior: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 198 | ▶ | sub features { | ||||||||||||||||||||||||||
|
Subroutine
features: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 151 | 198 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 198 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $self->{features} = shift; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 198 | return $self->{features}; | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 140 | ▶ | sub categories { | ||||||||||||||||||||||||||
|
Subroutine
categories: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | 140 | my $c = $_[0]->{categories}; | |||||||||||||||||||||||||||
| 160 | 140 | ▶ | return wantarray ? $c->members : $c->size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | ### Workers | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 92 | ▶ | sub create_feature_vector { | ||||||||||||||||||||||||||
|
Subroutine
create_feature_vector: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 92 | my $self = shift; | |||||||||||||||||||||||||||
| 168 | 92 | my $content = $self->{content}; | |||||||||||||||||||||||||||
| 169 | 92 | my $weights = $self->{content_weights}; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | die "'stopword_behavior' must be one of 'stem', 'no_stem', or 'pre_stemmed'" | ||||||||||||||||||||||||||||
| 172 | 92 | ▶ | unless $self->{stopword_behavior} =~ /^stem|no_stem|pre_stemmed$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 92 | $self->{features} = $self->create_delayed_object('features'); | |||||||||||||||||||||||||||
| 175 | 92 | ▶ | while (my ($name, $data) = each %$content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 92 | my $t = $self->tokenize($data); | |||||||||||||||||||||||||||
| 177 | 92 | ▶ | $t = $self->_filter_tokens($t) if $self->{stopword_behavior} eq 'no_stem'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 92 | $self->stem_words($t); | |||||||||||||||||||||||||||
| 179 | 92 | ▶ | $t = $self->_filter_tokens($t) if $self->{stopword_behavior} =~ /^stem|pre_stemmed$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 92 | ▶ | my $h = $self->vectorize(tokens => $t, weight => exists($weights->{$name}) ? $weights->{$name} : 1 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 92 | $self->{features}->add($h); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 16 | ▶ | sub is_in_category { | ||||||||||||||||||||||||||
|
Subroutine
is_in_category: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 186 | return (ref $_[1] | ||||||||||||||||||||||||||||
| 187 | ? $_[0]->{categories}->includes( $_[1] ) | ||||||||||||||||||||||||||||
| 188 | 16 | ▶ | : $_[0]->{categories}->includes_name( $_[1] )); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 92 | ▶ | sub tokenize { | ||||||||||||||||||||||||||
|
Subroutine
tokenize: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 193 | 92 | my $self = shift; | |||||||||||||||||||||||||||
| 194 | 92 | my @tokens; | |||||||||||||||||||||||||||
| 195 | 92 | ▶ | while ($_[0] =~ /([-\w]+)/g) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 670 | my $word = lc $1; | |||||||||||||||||||||||||||
| 197 | 670 | ▶ | next unless $word =~ /[a-z]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 670 | $word =~ s/^[^a-z]+//; # Trim leading non-alpha characters (helps with ordinals) | |||||||||||||||||||||||||||
| 199 | 670 | push @tokens, $word; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 92 | return \@tokens; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 93 | ▶ | sub stem_words { | ||||||||||||||||||||||||||
|
Subroutine
stem_words: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | 93 | my ($self, $tokens) = @_; | |||||||||||||||||||||||||||
| 206 | 93 | ▶ | return unless $self->{stemming}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 4 | ▶ | return if $self->{stemming} eq 'none'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | die "Unknown stemming option '$self->{stemming}' - options are 'porter' or 'none'" | ||||||||||||||||||||||||||||
| 209 | 4 | ▶ | unless $self->{stemming} eq 'porter'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 4 | ▶ | eval {require Lingua::Stem; 1} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | or die "Porter stemming requires the Lingua::Stem module, available from CPAN.\n"; | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 4 | @$tokens = @{ Lingua::Stem::stem(@$tokens) }; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 92 | ▶ | sub _filter_tokens { | ||||||||||||||||||||||||||
|
Subroutine
_filter_tokens: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||
| 218 | 92 | my ($self, $tokens_in) = @_; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 92 | ▶ | if ($self->{use_features}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 221 | 0 | my $f = $self->{use_features}->as_hash; | |||||||||||||||||||||||||||
| 222 | 0 | return [ grep exists($f->{$_}), @$tokens_in ]; | |||||||||||||||||||||||||||
| 223 | 92 | ▶ | } elsif ($self->{stopwords} and keys %{$self->{stopwords}}) { | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"stopwords"} and keys %{$$self{"stopwords"};}
MC/DC50%1/2
$self->{"stopwords"} and keys %{$$self{"stopwords"};}
$self->{"stopwords"}
keys %{$$self{"stopwords"};}
Truth table66%2/3
A and B
A= $self->{"stopwords"}B= keys %{$$self{"stopwords"};}
| |||||||||||||||||||||||||||||
| 224 | 27 | my $s = $self->{stopwords}; | |||||||||||||||||||||||||||
| 225 | 27 | return [ grep !exists($s->{$_}), @$tokens_in ]; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | 65 | return $tokens_in; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 92 | ▶ | sub _weigh_tokens { | ||||||||||||||||||||||||||
|
Subroutine
_weigh_tokens: called CC=6 SCAR=28.1
| |||||||||||||||||||||||||||||
| 231 | 92 | my ($self, $tokens, $weight) = @_; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 92 | my %counts; | |||||||||||||||||||||||||||
| 234 | 92 | ▶ | if (my $b = 0+$self->{front_bias}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 0 | ▶ | die "'front_bias' value must be between -1 and 1" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-1 < $b and $b < 1
MC/DC0%0/2
-1 < $b and $b < 1
-1 < $b
$b < 1
Truth table0%0/3
A and B
A= -1 < $bB= $b < 1
| |||||||||||||||||||||||||||||
| 236 | unless -1 < $b and $b < 1; | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | my $n = @$tokens; | |||||||||||||||||||||||||||
| 239 | 0 | my $r = ($b-1)**2 / ($b+1); | |||||||||||||||||||||||||||
| 240 | 0 | my $mult = $weight * log($r)/($r-1); | |||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | 0 | my $i = 0; | |||||||||||||||||||||||||||
| 243 | 0 | foreach my $feature (@$tokens) { | |||||||||||||||||||||||||||
| 244 | 0 | $counts{$feature} += $mult * $r**($i/$n); | |||||||||||||||||||||||||||
| 245 | 0 | $i++; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | } else { | ||||||||||||||||||||||||||||
| 249 | 92 | foreach my $feature (@$tokens) { | |||||||||||||||||||||||||||
| 250 | 632 | $counts{$feature} += $weight; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 92 | return \%counts; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 92 | ▶ | sub vectorize { | ||||||||||||||||||||||||||
|
Subroutine
vectorize: called CC=2 SCAR=10.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 258 | 92 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 259 | 92 | ▶ | if ($self->{stem_stopwords}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 0 | my $s = $self->stem_tokens([keys %{$self->{stopwords}}]); | |||||||||||||||||||||||||||
| 261 | 0 | $self->{stopwords} = { map {+$_, 1} @$s }; | |||||||||||||||||||||||||||
| 262 | 0 | $args{tokens} = $self->_filter_tokens($args{tokens}); | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | 92 | return $self->_weigh_tokens($args{tokens}, $args{weight}); | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 3 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 268 | 3 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 269 | 3 | ▶ | my $path = delete $args{path} or die "Must specify 'path' argument to read()"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 3 | my $self = $class->new(%args); | |||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 3 | ▶ | open my($fh), "< $path" or die "$path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 3 | $self->parse_handle(handle => $fh); | |||||||||||||||||||||||||||
| 275 | 3 | close $fh; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 3 | $self->finish; | |||||||||||||||||||||||||||
| 278 | 3 | return $self; | |||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 0 | ▶ | sub dump_features { | ||||||||||||||||||||||||||
|
Subroutine
dump_features: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 282 | 0 | my ($self, %args) = @_; | |||||||||||||||||||||||||||
| 283 | 0 | ▶ | my $path = $args{path} or die "No 'path' argument given to dump_features()"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | open my($fh), "> $path" or die "Can't create $path: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 0 | my $f = $self->features->as_hash; | |||||||||||||||||||||||||||
| 286 | 0 | ▶ | while (my ($k, $v) = each %$f) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 287 | 0 | print $fh "$k\t$v\n"; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 11 | 1; | |||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | __END__ | ||||||||||||||||||||||||||||