blib/lib/App/Greple/xlate/gpty/gpt3.pm

filter: Statementstmt 42.8% 33 / 77 Branchbran 0.0% 0 / 20 Conditioncond 12.5% 2 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 64.2% 9 / 14 Podpod 0.0% 0 / 4 totaltotal 31.6% 44 / 139 CC 16 SCAR 35.6
CC
16
Cov
31%
xlate
90.0
gpty
30.0
xlate_each
12.0
CRAP 35.1
« blib/lib/App/Greple/xlate/gpty/gpt5.pm ↑ Summary blib/lib/App/Greple/xlate/gpty/gpt4.pm »
1 package App::Greple::xlate::gpty::gpt3;
2  
3 1 our $VERSION = "2.03";
4  
5 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Command::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use App::Greple::xlate qw(opt);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use App::Greple::xlate::Lang qw(%LANGNAME);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶our $lang_from //= 'ORIGINAL';
Condition50%1/2
our $lang_from //= "ORIGINAL"
l!l
01
MC/DC0%0/1
our $lang_from //= "ORIGINAL"
our $lang_from
Truth table50%1/2
A
A= our $lang_from
Aresult
11
00
18 1 ▶our $lang_to //= 'JA';
Condition50%1/2
our $lang_to //= "JA"
l!l
01
MC/DC0%0/1
our $lang_to //= "JA"
our $lang_to
Truth table50%1/2
A
A= our $lang_to
Aresult
11
00
19 1 our $auth_key;
20 1 our $method = __PACKAGE__ =~ s/.*://r;
21  
22 1 my %param = (
23 gpt3 => { engine => 'gpt-3.5-turbo', temp => '0.0', max => 3000, sub => \&gpty,
24 prompt => 'Translate following entire text into %s, line-by-line.',
25 },
26 gpt4 => { engine => 'gpt-4-1106-preview', temp => '0.0', max => 3000, sub => \&gpty,
27 prompt => 'Translate following entire text into %s, line-by-line.',
28 },
29 );
30  
31 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my($mod, $argv) = @_;
33 0 $mod->setopt(default => "-Mxlate --xlate-engine=$method");
34 }
35  
36 0 ▶sub gpty {
Subroutine gpty: not called CC=5 SCAR=34.0
Pod undocumented
37 0 state $gpty = Command::Run->new;
38 0 my $text = shift;
39 0 my $param = $param{$method};
40 0 ▶ my $prompt = opt('prompt') || $param->{prompt};
Condition0%0/3
opt('prompt') || $param->{'prompt'}
l!l&&r!l&&!r
000
MC/DC0%0/2
opt('prompt') || $param->{'prompt'}
opt('prompt') $param->{'prompt'}
Truth table0%0/3
A || B
A= opt('prompt')B= $param->{'prompt'}
ABresult
1X1
011
000
41 0 my @vars = do {
42 0 ▶ if ($prompt =~ /%s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prompt =~ /%s/u) { } 0 0 0
43 0 ▶ $LANGNAME{$lang_to} // die "$lang_to: unknown lang.\n"
Condition0%0/2
$App::Greple::xlate::gpty::gpt3::LANGNAME{$App::Greple::xlate::gpty::gpt3::lang_to} // die("$App::Greple::xlate::gpty::gpt3::lang_to: unknown lang.\n")
l!l
00
MC/DC0%0/1
$App::Greple::xlate::gpty::gpt3::LANGNAME{$App::Greple::xlate::gpty::gpt3::lang_to} // die("$App::Greple::xlate::gpty::gpt3::lang_to: unknown lang.\n")
$App::Greple::xlate::gpty::gpt3::LANGNAME{$App::Greple::xlate::gpty::gpt3::lang_to}
Truth table0%0/2
A
A= $App::Greple::xlate::gpty::gpt3::LANGNAME{$App::Greple::xlate::gpty::gpt3::lang_to}
Aresult
11
00
44 } else {
45 0 ();
46 }
47 };
48 0 my $system = sprintf($prompt, @vars);
49 my @command = (
50 'gpty',
51 -e => $param->{engine},
52 -t => $param->{temp},
53 0 -s => $system,
54 '-',
55 );
56 0 ▶ warn Dumper \@command if opt('debug');
Branch: 1 branch
BranchTrueFalseTotal
if opt("debug") 0 0 0
57 0 $gpty->command(@command)->with(stdin => $text)->update->data;
58 }
59  
60 0 ▶sub _progress {
Subroutine _progress: not called CC=2 SCAR=17.9
61 0 ▶ print STDERR @_ if opt('progress');
Branch: 1 branch
BranchTrueFalseTotal
if opt("progress") 0 0 0
62 }
63  
64 0 ▶sub xlate_each {
Subroutine xlate_each: not called CC=3 SCAR=24.8
Pod undocumented
65 0 ▶ my $call = $param{$method}->{sub} // die;
Condition0%0/2
$param{$App::Greple::xlate::gpty::gpt3::method}{"sub"} // die()
l!l
00
MC/DC0%0/1
$param{$App::Greple::xlate::gpty::gpt3::method}{"sub"} // die()
$param{$App::Greple::xlate::gpty::gpt3::method}{"sub"}
Truth table0%0/2
A
A= $param{$App::Greple::xlate::gpty::gpt3::method}{"sub"}
Aresult
11
00
66 0 my @count = map { int tr/\n/\n/ } @_;
67 0 _progress("From:\n", map s/^/\t< /mgr, @_);
68 0 my $to = $call->(join '', @_);
69 0 my @out = $to =~ /^.+\n/mg;
70 0 _progress("To:\n", map s/^/\t> /mgr, @out);
71 0 ▶ if (@out < sum @count) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out < &sum(@count)) 0 0 0
72 0 die "Unexpected response:\n\n$to\n";
73 }
74 0 map { join '', splice @out, 0, $_ } @count;
75 }
76  
77 0 ▶sub xlate {
Subroutine xlate: not called CC=9 SCAR=45.0
Pod undocumented
78 0 ▶ my @from = map { /\n\z/ ? $_ : "$_\n" } @_;
Branch: 1 branch
BranchTrueFalseTotal
/\n\z/u ? : 0 0 0
79 0 my @to;
80 0 ▶ my $max = $App::Greple::xlate::max_length || $param{$method}->{max} // die;
Condition0%0/5
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::gpty::gpt3::method}{"max"}) // die()
l!l
00
$App::Greple::xlate::max_length or $param{$App::Greple::xlate::gpty::gpt3::method}{"max"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::gpty::gpt3::method}{"max"}) // die()
$App::Greple::xlate::max_length $param{$App::Greple::xlate::gpty::gpt3::method}{"max"}
Truth table0%0/3
A or B
A= $App::Greple::xlate::max_lengthB= $param{$App::Greple::xlate::gpty::gpt3::method}{"max"}
ABresult
1X1
011
000
81 0 ▶ if (my @len = grep { $_ > $max } map length, @from) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@len) = grep({$_ > $max;} map(length($_), @from))) 0 0 0
82 0 die "Contain lines longer than max length (@len > $max).\n";
83 }
84 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
85 0 my @tmp;
86 0 my $len = 0;
87 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
88 0 my $next = length $from[0];
89 0 ▶ last if $len + $next > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $len + $next > $max 0 0 0
90 0 $len += $next;
91 0 push @tmp, shift @from;
92 }
93 0 ▶ @tmp > 0 or die "Probably text is longer than max length ($max).\n";
Branch: 1 branch
BranchTrueFalseTotal
@tmp > 0 or die "Probably text is longer than max length ($max).\n" 0 0 0
94 0 push @to, xlate_each @tmp;
95 }
96 0 @to;
97 }
98  
99 1 1;
100  
101 __DATA__
« blib/lib/App/Greple/xlate/gpty/gpt5.pm ↑ Summary blib/lib/App/Greple/xlate/gpty/gpt4.pm »