blib/lib/PDL/Tokenizers.pm

filter: Statementstmt 76.8% 63 / 82 Branchbran 26.9% 7 / 26 Conditioncond 31.2% 10 / 32 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 92.3% 12 / 13 Podpod 0.0% 0 / 5 totaltotal 53.1% 92 / 173 CC 31 SCAR 31.8
CC
31
Cov
57%
_bpe_word
90.0
new
9.7
encode
9.5
CRAP 24.1
↑ Summary
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'} || {}
l!l
20
MC/DC0%0/1
$args{'vocab'} || {}
$args{'vocab'}
Truth table50%1/2
A
A= $args{'vocab'}
Aresult
11
00
16 2 ▶ my $merges = $args{merges} || [];
Condition50%1/2
$args{'merges'} || []
l!l
20
MC/DC0%0/1
$args{'merges'} || []
$args{'merges'}
Truth table50%1/2
A
A= $args{'merges'}
Aresult
11
00
17 2 ▶ my $unk_id = $args{unk_id} // ($vocab->{'<unk>'} // $vocab->{'[UNK]'} // 0);
Condition42%3/7
$args{"unk_id"} // (($vocab->{"<unk>"} // $vocab->{"[UNK]"}) // 0)
l!l
02
($vocab->{"<unk>"} // $vocab->{"[UNK]"}) // 0
l!l
20
$vocab->{"<unk>"} // $vocab->{"[UNK]"}
l!l&&r!l&&!r
200
MC/DC0%0/1
$args{"unk_id"} // (($vocab->{"<unk>"} // $vocab->{"[UNK]"}) // 0)
$args{"unk_id"}
Truth table50%1/2
A
A= $args{"unk_id"}
Aresult
11
00
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
BranchTrueFalseTotal
ref $m eq 'ARRAY' ? : 0 4 4
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
BranchTrueFalseTotal
unless defined $file 0 1 1
38 1 ▶ open my $fh, '<:encoding(UTF-8)', $file or croak "Cannot open '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $file or croak("Cannot open '${file}': $!") 1 0 1
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
l!l&&r!l&&!r
100
MC/DC0%0/2
$data->{'model'} || $data
$data->{'model'} $data
Truth table33%1/3
A || B
A= $data->{'model'}B= $data
ABresult
1X1
011
000
44 return $class->new(
45 vocab => $model->{vocab} || {},
46 1 ▶ merges => $model->{merges} || [],
Condition50%2/4
$model->{'vocab'} || {}
l!l
10
$model->{'merges'} || []
l!l
10
MC/DC0%0/1
$model->{'vocab'} || {}
$model->{'vocab'}
MC/DC0%0/1
$model->{'merges'} || []
$model->{'merges'}
Truth table50%1/2
A
A= $model->{'vocab'}
Aresult
11
00
Truth table50%1/2
A
A= $model->{'merges'}
Aresult
11
00
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
BranchTrueFalseTotal
if @syms <= 1 or not %{$self->{"merge_rank"};} 0 0 0
Condition0%0/3
@syms <= 1 or not %{$self->{"merge_rank"};}
l!l&&r!l&&!r
000
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"};}
ABresult
1X1
011
000
54  
55 0 ▶ while (@syms > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@syms > 1) 0 0 0
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
BranchTrueFalseTotal
if (defined $rank and !defined($best_rank) || $rank < $best_rank) 0 0 0
Condition0%0/6
defined $rank and !defined($best_rank) || $rank < $best_rank
!ll&&!rl&&r
000
not defined $best_rank or $rank < $best_rank
l!l&&r!l&&!r
000
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
ABCresult
0XX0
1000
11X1
1011
61 0 $best_rank = $rank;
62 0 $best_idx = $i;
63 }
64 }
65 0 ▶ last unless defined $best_idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $best_idx 0 0 0
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
BranchTrueFalseTotal
unless defined $text 0 2 2
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
BranchTrueFalseTotal
unless length $word 0 3 3
78 3 ▶ if (exists $vocab->{$word}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $vocab->{$word}) { } 3 0 3
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
BranchTrueFalseTotal
exists $vocab->{$piece} ? : 0 0 0
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
BranchTrueFalseTotal
ref $ids_pdl eq 'ARRAY' ? : 0 0 0
92 ? $ids_pdl->list
93 0 ▶ : (ref($ids_pdl) eq 'ARRAY' ? @{$ids_pdl} : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $ids_pdl && eval { do { $ids_pdl->isa('PDL') } } ? : 1 0 1
Condition33%1/3
ref $ids_pdl and eval { do { $ids_pdl->isa("PDL") } }
!ll&&!rl&&r
001
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") } }
ABresult
0X0
100
111
94 1 ▶ my @pieces = map { $self->{id_to_tok}{$_} // '' } @ids;
Condition50%1/2
$self->{"id_to_tok"}{$_} // ""
l!l
20
MC/DC0%0/1
$self->{"id_to_tok"}{$_} // ""
$self->{"id_to_tok"}{$_}
Truth table50%1/2
A
A= $self->{"id_to_tok"}{$_}
Aresult
11
00
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__
↑ Summary