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