| 1 | package Hailo::Engine::Default; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 23 | our $AUTHORITY = 'cpan:AVAR'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 23 | $Hailo::Engine::Default::VERSION = '0.73'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 23 | ▶ | use v5.28.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 23 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 23 | ▶ | use List::Util qw<min first shuffle>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 23 | ▶ | use List::MoreUtils qw<uniq>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 23 | with qw[ Hailo::Role::Arguments Hailo::Role::Engine ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | has repeat_limit => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | isa => 'Int', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | default => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 29 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 29 | my $order = $self->order; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 29 | return min(($order * 10), 50); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 23 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 46 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # This performance hack is here because in our tight loops calling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # $self->storage->sth->{...} is actually a significant part of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # overall program execution time since we're doing two method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # calls and hash dereferences for each call to the database. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 46 | my $sth = $self->storage->sth; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 46 | ▶ | while (my ($k, $v) = each %$sth) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1337 | $self->{"_sth_$k"} = $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 46 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | ## no critic (Subroutines::ProhibitExcessComplexity) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | sub reply { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1218 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reply: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1218 | ▶ | my $tokens = shift // []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() // []
MC/DC100%1/1
shift() // []
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # we will favor these tokens when making the reply. Shuffle them | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # and discard half. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1218 | my @key_tokens = do { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1218 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1218 | grep { $i++ % 2 == 0 } shuffle(@$tokens); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1218 | my $token_cache = $self->_resolve_input_tokens($tokens); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 1218 | my @key_ids = keys %$token_cache; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | # sort the rest by rareness | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1218 | @key_ids = $self->_find_rare_tokens(\@key_ids, 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # get the middle expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 1218 | my $pivot_token_id = shift @key_ids; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 1218 | my ($pivot_expr_id, @token_ids) = $self->_random_expr($pivot_token_id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 1218 | ▶ | return unless defined $pivot_expr_id; # we don't know any expressions yet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # remove key tokens we're already using | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 1214 | ▶ | @key_ids = grep { my $used = $_; !first { $_ == $used } @token_ids } @key_ids; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 1214 | my %expr_cache; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # construct the end of the reply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 1214 | $self->_construct_reply('next', $pivot_expr_id, \@token_ids, \%expr_cache, \@key_ids); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # construct the beginning of the reply | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 1214 | $self->_construct_reply('prev', $pivot_expr_id, \@token_ids, \%expr_cache, \@key_ids); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | # translate token ids to token spacing/text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | my @output = map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1214 | ▶ | $token_cache->{$_} // ($token_cache->{$_} = $self->_token_info($_)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$token_cache->{$_} // ($token_cache->{$_} = $self->_token_info($_))
MC/DC0%0/2
$token_cache->{$_} // ($token_cache->{$_} = $self->_token_info($_))
$token_cache->{$_}
($token_cache->{$_} = $self->_token_info($_))
Truth table66%2/3
A // B
A= $token_cache->{$_}B= ($token_cache->{$_} = $self->_token_info($_))
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } @token_ids; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 1214 | return \@output; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | sub _resolve_input_tokens { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1218 | ▶ | my ($self, $tokens) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_resolve_input_tokens: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 1218 | my %token_cache; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1218 | ▶ | if (@$tokens == 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 914 | my ($spacing, $text) = @{ $tokens->[0] }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 914 | my $token_info = $self->_token_resolve($spacing, $text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 914 | ▶ | if (defined $token_info) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 778 | my ($id, $count) = @$token_info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 778 | $token_cache{$id} = [$spacing, $text, $count]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | # when there's just one token, it could be ';' for example, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # which will have normal spacing when it appears alone, but | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # suffix spacing in a sentence like "those things; foo, bar", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # so we'll be a bit more lax here by also looking for any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # token that has the same text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 136 | $token_info = $self->_token_similar($text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 136 | ▶ | if (defined $token_info) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 119 | my ($id, $spacing, $count) = @$token_info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 119 | $token_cache{$id} = [$spacing, $text, $count]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 304 | for my $token (@$tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 3609 | my ($spacing, $text) = @$token; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 3609 | my $token_info = $self->_token_resolve($spacing, $text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 3609 | ▶ | next if !defined $token_info; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 3601 | my ($id, $count) = @$token_info; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 3601 | $token_cache{$id} = [$spacing, $text, $count]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 1218 | return \%token_cache; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | sub _token_resolve { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 4523 | ▶ | my ($self, $spacing, $text) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token_resolve: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4523 | $self->{_sth_token_resolve}->execute($spacing, $text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 4523 | return $self->{_sth_token_resolve}->fetchrow_arrayref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | sub _token_info { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 9467 | ▶ | my ($self, $id) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token_info: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 9467 | $self->{_sth_token_info}->execute($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 9467 | my @res = $self->{_sth_token_info}->fetchrow_array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 9467 | return \@res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | sub learn { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 3619 | ▶ | my ($self, $tokens) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
learn: called CC=10 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 3619 | my $order = $self->order; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | # only learn from inputs which are long enough | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 3619 | ▶ | return if @$tokens < $order; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 3324 | my (%token_cache, %expr_cache); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # resolve/add tokens and update their counter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 3324 | for my $token (@$tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 32012 | my $key = join '', @$token; # the key is "$spacing$text" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 32012 | ▶ | if (!exists $token_cache{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 28146 | $token_cache{$key} = $self->_token_id_add($token); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 32012 | $self->{_sth_inc_token_count}->execute(1, $token_cache{$key}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | # process every expression of length $order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 3324 | for my $i (0 .. @$tokens - $order) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 28525 | my @expr = map { $token_cache{ join('', @{ $tokens->[$_] }) } } $i .. $i+$order-1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 28525 | my $key = join('_', @expr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 28525 | ▶ | if (!defined $expr_cache{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 27430 | $expr_cache{$key} = $self->_expr_id_add(\@expr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 28525 | my $expr_id = $expr_cache{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | # add link to next token for this expression, if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 28525 | ▶ | if ($i < @$tokens - $order) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 25201 | my $next_id = $token_cache{ join('', @{ $tokens->[$i+$order] }) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 25201 | $self->_inc_link('next_token', $expr_id, $next_id, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | # add link to previous token for this expression, if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 28525 | ▶ | if ($i > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 25201 | my $prev_id = $token_cache{ join('', @{ $tokens->[$i-1] }) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 25201 | $self->_inc_link('prev_token', $expr_id, $prev_id, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # add links to boundary token if appropriate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 28525 | my $b = $self->storage->_boundary_token_id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 28525 | ▶ | $self->_inc_link('prev_token', $expr_id, $b, 1) if $i == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 28525 | ▶ | $self->_inc_link('next_token', $expr_id, $b, 1) if $i == @$tokens-$order; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 3324 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | sub learn_cached { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | ▶ | my ($self, $tokens) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
learn_cached: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | my $order = $self->order; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # only learn from inputs which are long enough | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | return if @$tokens < $order; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my (%token_cache, %expr_cache); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # resolve/add tokens and update their counter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | for my $token (@$tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 0 | my $key = join '', @$token; # the key is "$spacing$text" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | ▶ | if (!exists $token_cache{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | my $token_id = $self->_token_id_add($token); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | $token_cache{$key} = $token_id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | $self->{_updates}{token_count}{$token_id}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | # process every expression of length $order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | for my $i (0 .. @$tokens - $order) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my @expr = map { $token_cache{ join('', @{ $tokens->[$_] }) } } $i .. $i+$order-1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | my $key = join('_', @expr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | if (!defined $expr_cache{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | $expr_cache{$key} = $self->_expr_id_add(\@expr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 0 | my $expr_id = $expr_cache{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | # add link to next token for this expression, if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | if ($i < @$tokens - $order) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | my $next_id = $token_cache{ join('', @{ $tokens->[$i+$order] }) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | $self->{_updates}{next_token}{$expr_id}{$next_id}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | # add link to previous token for this expression, if any | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | if ($i > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | my $prev_id = $token_cache{ join('', @{ $tokens->[$i-1] }) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | $self->{_updates}{prev_token}{$expr_id}{$prev_id}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # add links to boundary token if appropriate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | my $b = $self->storage->_boundary_token_id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | $self->{_updates}{prev_token}{$expr_id}{$b}++ if $i == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | $self->{_updates}{next_token}{$expr_id}{$b}++ if $i == @$tokens-$order; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | sub flush_cache { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 1985 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
flush_cache: called CC=7 SCAR=36.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 1985 | my $updates = $self->{_updates}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1985 | ▶ | return if !$updates; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | while (my ($token_id, $count) = each %{ $updates->{token_count} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | $self->{_sth_inc_token_count}->execute($count, $token_id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | while (my ($expr_id, $links) = each %{ $updates->{next_token} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | while (my ($next_token_id, $count) = each %$links) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | $self->_inc_link('next_token', $expr_id, $next_token_id, $count); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | while (my ($expr_id, $links) = each %{ $updates->{prev_token} }) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | while (my ($prev_token_id, $count) = each %$links) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | $self->_inc_link('prev_token', $expr_id, $prev_token_id, $count); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | # sort token ids based on how rare they are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | sub _find_rare_tokens { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 1218 | ▶ | my ($self, $token_ids, $min) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_rare_tokens: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 1218 | ▶ | return unless @$token_ids; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 1193 | my %links; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 1193 | for my $id (@$token_ids) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 4254 | ▶ | next if exists $links{$id}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 4254 | $self->{_sth_token_count}->execute($id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 4254 | $links{$id} = $self->{_sth_token_count}->fetchrow_array; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | # remove tokens which are too rare | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 1193 | my @ids = grep { $links{$_} >= $min } @$token_ids; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 1193 | @ids = sort { $links{$a} <=> $links{$b} } @ids; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 1193 | return @ids; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | # increase the link weight between an expression and a token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | sub _inc_link { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 57050 | ▶ | my ($self, $type, $expr_id, $token_id, $count) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inc_link: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 57050 | $self->{"_sth_${type}_inc"}->execute($count, $expr_id, $token_id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 57050 | ▶ | if (!$self->{"_sth_${type}_inc"}->rows) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 44457 | $self->{"_sth_${type}_add"}->execute($expr_id, $token_id, $count); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 57050 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # look up/add an expression id based on tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | sub _expr_id_add { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 38098 | ▶ | my ($self, $token_ids) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_expr_id_add: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 38098 | $self->{_sth_expr_id}->execute(@$token_ids); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 38098 | my $expr_id = $self->{_sth_expr_id}->fetchrow_array(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 38098 | ▶ | return $expr_id if defined $expr_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 18290 | $self->{_sth_add_expr}->execute(@$token_ids); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 18290 | return $self->storage->dbh->last_insert_id(undef, undef, "expr", undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | # return token id if the token exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | sub _token_id { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 28146 | ▶ | my ($self, $token_info) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token_id: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 28146 | $self->{_sth_token_id}->execute(@$token_info); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 28146 | my $token_id = $self->{_sth_token_id}->fetchrow_array(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 28146 | ▶ | return unless defined $token_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 19584 | return $token_id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # get token id (adding the token if it doesn't exist) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | sub _token_id_add { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 28146 | ▶ | my ($self, $token_info) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token_id_add: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 28146 | my $token_id = $self->_token_id($token_info); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 28146 | ▶ | $token_id = $self->_add_token($token_info) unless defined $token_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 28146 | return $token_id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # return all tokens (regardless of spacing) that consist of this text | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | sub _token_similar { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 136 | ▶ | my ($self, $token_text) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_token_similar: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 136 | $self->{_sth_token_similar}->execute($token_text); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 136 | return $self->{_sth_token_similar}->fetchrow_arrayref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | # add a new token and return its id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | sub _add_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 8562 | ▶ | my ($self, $token_info) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_add_token: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 8562 | $self->{_sth_add_token}->execute(@$token_info); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 8562 | return $self->storage->dbh->last_insert_id(undef, undef, "token", undef); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | # return a random expression containing the given token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | sub _random_expr { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 1218 | ▶ | my ($self, $token_id) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_random_expr: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 1218 | my $expr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 1218 | ▶ | if (!defined $token_id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 169 | $self->{_sth_random_expr}->execute(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 169 | $expr = $self->{_sth_random_expr}->fetchrow_arrayref(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # try the positions in a random order | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 1049 | for my $pos (shuffle 0 .. $self->order-1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 1059 | my $column = "token${pos}_id"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | # get a random expression which includes the token at this position | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 1059 | $self->{"_sth_expr_by_$column"}->execute($token_id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 1059 | $expr = $self->{"_sth_expr_by_$column"}->fetchrow_arrayref(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 1059 | ▶ | last if defined $expr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 1218 | ▶ | return unless defined $expr; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 1214 | return @$expr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | # return a new next/previous token | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | sub _pos_token { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 13274 | ▶ | my ($self, $pos, $expr_id, $key_tokens) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pos_token: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 13274 | $self->{"_sth_${pos}_token_get"}->execute($expr_id); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 13274 | my $pos_tokens = $self->{"_sth_${pos}_token_get"}->fetchall_arrayref(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 13274 | ▶ | if (defined $key_tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 13274 | for my $i (0 .. $#{ $key_tokens }) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 28303 | my $want_id = $key_tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 28303 | my @ids = map { $_->[0] } @$pos_tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 28303 | my $has_id = grep { $_ == $want_id } @ids; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 28303 | ▶ | next unless $has_id; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 1246 | return splice @$key_tokens, $i, 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 12028 | my @novel_tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 12028 | for my $token (@$pos_tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 16775 | push @novel_tokens, ($token->[0]) x $token->[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 12028 | return $novel_tokens[rand @novel_tokens]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | sub _construct_reply { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 2428 | ▶ | my ($self, $what, $expr_id, $token_ids, $expr_cache, $key_ids) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_construct_reply: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 2428 | my $order = $self->order; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 2428 | my $repeat_limit = $self->repeat_limit; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 2428 | my $boundary_token = $self->storage->_boundary_token_id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 2428 | my $i = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 2428 | while (1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 13276 | ▶ | if (($i % $order) == 0 and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$i % $order == 0 and $i >= $repeat_limit * 3 || $i >= $repeat_limit && &uniq(@$token_ids) <= $order
$i >= $repeat_limit * 3 or $i >= $repeat_limit and &uniq(@$token_ids) <= $order
$i >= $repeat_limit and &uniq(@$token_ids) <= $order
MC/DC50%2/4
$i % $order == 0 and $i >= $repeat_limit * 3 || $i >= $repeat_limit && &uniq(@$token_ids) <= $order
$i % $order == 0
$i >= $repeat_limit * 3
$i >= $repeat_limit
&uniq(@$token_ids) <= $order
Truth table60%3/5
A and B or C and D
A= $i % $order == 0B= $i >= $repeat_limit * 3C= $i >= $repeat_limitD= &uniq(@$token_ids) <= $order
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | (($i >= $repeat_limit * 3) || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | ($i >= $repeat_limit and uniq(@$token_ids) <= $order))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 2 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 13274 | my $id = $self->_pos_token($what, $expr_id, $key_ids); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 13274 | ▶ | last if $id == $boundary_token; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 10848 | my @ids; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 10848 | ▶ | if ($what eq 'next') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 5402 | push @$token_ids, $id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 5402 | @ids = @$token_ids[-$order..-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } elsif ($what eq 'prev') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 5446 | unshift @$token_ids, $id; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 5446 | @ids = @$token_ids[0..$order-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | die "PANIC: Internal Error: Don't know what the '$what' argument means"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 10848 | my $key = join '_', @ids; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 10848 | ▶ | if (!defined $expr_cache->{$key}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | 10668 | $expr_cache->{$key} = $self->_expr_id_add(\@ids); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 10848 | $expr_id = $expr_cache->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } continue { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 10848 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 2428 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 23 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | Hailo::Engine::Default - The default engine backend for L<Hailo|Hailo> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | This backend implements the logic of replying to and learning from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | input using the resources given to the L<engine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | roles|Hailo::Role::Engine>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | It generates the reply in one go, while favoring some of the tokens in the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | input, and returns it. It is fast and the replies are decent, but you can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | get better replies (at the cost of speed) with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | L<Scored|Hailo::Engine::Scored> engine. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | Copyright 2010 Hinrik E<Ouml>rn SigurE<eth>sson and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | E<AElig>var ArnfjE<ouml>rE<eth> Bjarmason <avar@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | This program is free software, you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||