| 1 | package PDL::Tokenizers; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use JSON::MaybeXS qw(decode_json); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use PDL::Core (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use PDL::Lite (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=22.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | my $vocab = $args{vocab} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'vocab'} || {}
MC/DC0%0/1
$args{'vocab'} || {}
$args{'vocab'}
Truth table50%1/2
A
A= $args{'vocab'}
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | my $merges = $args{merges} || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{'merges'} || []
MC/DC0%0/1
$args{'merges'} || []
$args{'merges'}
Truth table50%1/2
A
A= $args{'merges'}
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | my $unk_id = $args{unk_id} // ($vocab->{'<unk>'} // $vocab->{'[UNK]'} // 0); | ||||||||||||||||||||||||||||||||||||||||
|
Condition42%3/7
$args{"unk_id"} // (($vocab->{"<unk>"} // $vocab->{"[UNK]"}) // 0)
($vocab->{"<unk>"} // $vocab->{"[UNK]"}) // 0
$vocab->{"<unk>"} // $vocab->{"[UNK]"}
MC/DC0%0/1
$args{"unk_id"} // (($vocab->{"<unk>"} // $vocab->{"[UNK]"}) // 0)
$args{"unk_id"}
Truth table50%1/2
A
A= $args{"unk_id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | my %id_to_tok = reverse %{$vocab}; | |||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | my %merge_rank; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | for my $i (0 .. $#{$merges}) { | |||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | my $m = $merges->[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | my $key = ref($m) eq 'ARRAY' ? join(' ', @{$m}) : $m; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 4 | $merge_rank{$key} = $i; | |||||||||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | return bless { | |||||||||||||||||||||||||||||||||||||||||
| 28 | vocab => $vocab, | ||||||||||||||||||||||||||||||||||||||||||
| 29 | id_to_tok => \%id_to_tok, | ||||||||||||||||||||||||||||||||||||||||||
| 30 | merge_rank => \%merge_rank, | ||||||||||||||||||||||||||||||||||||||||||
| 31 | unk_id => $unk_id, | ||||||||||||||||||||||||||||||||||||||||||
| 32 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | sub from_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_file: called CC=6 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | my ($class, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | croak 'Filename required' unless defined $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | open my $fh, '<:encoding(UTF-8)', $file or croak "Cannot open '$file': $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | local $/; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | my $raw = <$fh>; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | my $data = decode_json($raw); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | my $model = $data->{model} || $data; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$data->{'model'} || $data
MC/DC0%0/2
$data->{'model'} || $data
$data->{'model'}
$data
Truth table33%1/3
A || B
A= $data->{'model'}B= $data
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | return $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 45 | vocab => $model->{vocab} || {}, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | ▶ | merges => $model->{merges} || [], | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$model->{'vocab'} || {}
$model->{'merges'} || []
MC/DC0%0/1
$model->{'vocab'} || {}
$model->{'vocab'}
MC/DC0%0/1
$model->{'merges'} || []
$model->{'merges'}
Truth table50%1/2
A
A= $model->{'vocab'}
Truth table50%1/2
A
A= $model->{'merges'}
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub _bpe_word { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bpe_word: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my ($self, $word) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | my @syms = split //, $word; | |||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return @syms if @syms <= 1 || !%{ $self->{merge_rank} }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@syms <= 1 or not %{$self->{"merge_rank"};}
MC/DC0%0/2
@syms <= 1 or not %{$self->{"merge_rank"};}
@syms <= 1
not %{$self->{"merge_rank"};}
Truth table0%0/3
A or not(B)
A= @syms <= 1B= not %{$self->{"merge_rank"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | while (@syms > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my ($best_rank, $best_idx); | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | for my $i (0 .. $#syms - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my $pair = $syms[$i] . ' ' . $syms[$i + 1]; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $rank = $self->{merge_rank}{$pair}; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if (defined $rank && (!defined $best_rank || $rank < $best_rank)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $rank and !defined($best_rank) || $rank < $best_rank
not defined $best_rank or $rank < $best_rank
MC/DC0%0/3
defined $rank and !defined($best_rank) || $rank < $best_rank
defined $rank
not defined $best_rank
$rank < $best_rank
Truth table0%0/4
A and not(B) or C
A= defined $rankB= not defined $best_rankC= $rank < $best_rank
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $best_rank = $rank; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $best_idx = $i; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | last unless defined $best_idx; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | splice @syms, $best_idx, 2, $syms[$best_idx] . $syms[$best_idx + 1]; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | return @syms; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | sub encode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode: called CC=7 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | my ($self, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 2 | ▶ | $text = '' unless defined $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | my @ids; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | my $vocab = $self->{vocab}; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | for my $word (split /\s+/, $text) { | |||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | next unless length $word; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | if (exists $vocab->{$word}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | push @ids, $vocab->{$word}; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | for my $piece ($self->_bpe_word($word)) { | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | push @ids, exists($vocab->{$piece}) ? $vocab->{$piece} : $self->{unk_id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 2 | return PDL::Core::pdl(PDL::long(), \@ids); | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | sub decode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode: called CC=5 SCAR=19.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | my ($self, $ids_pdl) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | ▶ | my @ids = ref($ids_pdl) && eval { $ids_pdl->isa('PDL') } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | ? $ids_pdl->list | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | : (ref($ids_pdl) eq 'ARRAY' ? @{$ids_pdl} : ()); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $ids_pdl and eval {
do {
$ids_pdl->isa("PDL")
}
}
MC/DC0%0/2
ref $ids_pdl and eval {
do {
$ids_pdl->isa("PDL")
}
}
ref $ids_pdl
eval {
do {
$ids_pdl->isa("PDL")
}
}
Truth table33%1/3
A and B
A= ref $ids_pdlB= eval {
do {
$ids_pdl->isa("PDL")
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | ▶ | my @pieces = map { $self->{id_to_tok}{$_} // '' } @ids; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"id_to_tok"}{$_} // ""
MC/DC0%0/1
$self->{"id_to_tok"}{$_} // ""
$self->{"id_to_tok"}{$_}
Truth table50%1/2
A
A= $self->{"id_to_tok"}{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | return join(' ', grep { length } @pieces); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | sub vocab_size { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
vocab_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | return scalar keys %{ $self->{vocab} }; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||||||||||||||||