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

filter: Statementstmt 43.4% 43 / 99 Branchbran 0.0% 0 / 22 Conditioncond 12.5% 2 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 64.7% 11 / 17 Podpod 0.0% 0 / 4 totaltotal 33.7% 56 / 166 CC 17 SCAR 35.3
CC
17
Cov
33%
xlate
90.0
gpty
42.0
xlate_each
12.0
CRAP 34.1
« blib/lib/App/Greple/xlate/gpty/gpt3.pm ↑ Summary lib/App/Greple/xlate.pm »
1 package App::Greple::xlate::gpty::gpt4;
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 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 0 ▶ *Data::Dumper::qquote = sub { qq["${\(shift)}"] };
Subroutine __ANON__: not called CC=1 SCAR=2.6
13 1 $Data::Dumper::Useperl = 1;
14 }
15  
16 1 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Command::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use App::Greple::xlate qw(%opt &opt);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use App::Greple::xlate::Lang qw(%LANGNAME);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 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
23 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
24 1 our $auth_key;
25 1 our $method = __PACKAGE__ =~ s/.*://r;
26  
27 1 my %param = (
28 gpt4 => { engine => 'gpt-4.1', temp => '0.0', max => 3000, sub => \&gpty,
29 prompt => <<END
30 Translate the following JSON array into %s.
31 For each input array element, output only the corresponding translated element at the same array index.
32 Leave blank strings unchanged. Within every element, preserve XML-style marker tags (e.g., '<m id="1" />') exactly and translate only the surrounding text.
33 Do not output the original (pre-translation) text under any circumstances.
34 The number and order of output elements must always match the input exactly: output element n must correspond to input element n.
35 Output only the translated elements or unchanged tags/blank strings as a JSON array.
36 Do not leave any unnecessary spaces or tabs at the end of any array element in your output.
37 Before finishing, carefully check that there are absolutely no omissions, duplicate content, or trailing spaces of any kind in your output.
38  
39 Return the result as a JSON array and nothing else.
40 Your entire output must be valid JSON.
41 Do not include any explanations, code blocks, or text outside of the JSON array.
42 If you cannot produce a valid JSON array, return an empty JSON array ([]).
43 END
44 },
45 );
46  
47 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
48 0 my($mod, $argv) = @_;
49 0 $mod->setopt(default => "-Mxlate --xlate-engine=$method");
50 }
51  
52 0 ▶sub gpty {
Subroutine gpty: not called CC=6 SCAR=37.4
Pod undocumented
53 0 state $gpty = Command::Run->new;
54 0 my $text = shift;
55 0 my $param = $param{$method};
56 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
57 0 my @vars = do {
58 0 ▶ if ($prompt =~ /%s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prompt =~ /%s/u) { } 0 0 0
59 0 ▶ $LANGNAME{$lang_to} // die "$lang_to: unknown lang.\n"
Condition0%0/2
$App::Greple::xlate::gpty::gpt4::LANGNAME{$App::Greple::xlate::gpty::gpt4::lang_to} // die("$App::Greple::xlate::gpty::gpt4::lang_to: unknown lang.\n")
l!l
00
MC/DC0%0/1
$App::Greple::xlate::gpty::gpt4::LANGNAME{$App::Greple::xlate::gpty::gpt4::lang_to} // die("$App::Greple::xlate::gpty::gpt4::lang_to: unknown lang.\n")
$App::Greple::xlate::gpty::gpt4::LANGNAME{$App::Greple::xlate::gpty::gpt4::lang_to}
Truth table0%0/2
A
A= $App::Greple::xlate::gpty::gpt4::LANGNAME{$App::Greple::xlate::gpty::gpt4::lang_to}
Aresult
11
00
60 } else {
61 0 ();
62 }
63 };
64 0 my $system = sprintf($prompt, @vars);
65 my @command = (
66 'gpty',
67 -e => $param->{engine},
68 -t => $param->{temp},
69 0 -s => $system,
70 );
71 0 ▶ if (my @contexts = @{$opt{contexts}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@contexts) = @{$App::Greple::xlate::gpty::gpt4::opt{"contexts"};}) 0 0 0
72 0 push @command, map { (-s => "Translation context: $_") } @contexts;
73 }
74 0 push @command, '-';
75 0 ▶ warn Dumper \@command if opt('debug');
Branch: 1 branch
BranchTrueFalseTotal
if opt("debug") 0 0 0
76 0 $gpty->command(@command)->with(stdin => $text)->update->data;
77 }
78  
79 0 ▶sub _progress {
Subroutine _progress: not called CC=2 SCAR=17.9
80 0 ▶ print STDERR @_ if opt('progress');
Branch: 1 branch
BranchTrueFalseTotal
if opt("progress") 0 0 0
81 }
82  
83 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 1 my $json = JSON->new->canonical->pretty;
85  
86 0 ▶sub xlate_each {
Subroutine xlate_each: not called CC=3 SCAR=24.8
Pod undocumented
87 0 ▶ my $call = $param{$method}->{sub} // die;
Condition0%0/2
$param{$App::Greple::xlate::gpty::gpt4::method}{"sub"} // die()
l!l
00
MC/DC0%0/1
$param{$App::Greple::xlate::gpty::gpt4::method}{"sub"} // die()
$param{$App::Greple::xlate::gpty::gpt4::method}{"sub"}
Truth table0%0/2
A
A= $param{$App::Greple::xlate::gpty::gpt4::method}{"sub"}
Aresult
11
00
88 0 my @count = map { int tr/\n/\n/ } @_;
89 0 _progress("From:\n", map s/^/\t< /mgr, @_);
90 0 my($in, $out);
91 0 my @in = map { m/.*\n/mg } @_;
92 0 my $obj = $json->decode($out = $call->($in = $json->encode(\@in)));
93 0 my @out = map { s/(?<!\n)\z/\n/r } @$obj;
94 0 _progress("To:\n", map s/^/\t> /mgr, @out);
95 0 ▶ if (@out < @in) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out < @in) 0 0 0
96 0 my $to = join '', @out;
97 0 die sprintf("Unexpected response (%d < %d):\n\n%s\n",
98 int(@out), int(@in), $to);
99 }
100 0 map { join '', splice @out, 0, $_ } @count;
101 }
102  
103 0 ▶sub xlate {
Subroutine xlate: not called CC=9 SCAR=45.0
Pod undocumented
104 0 ▶ my @from = map { /\n\z/ ? $_ : "$_\n" } @_;
Branch: 1 branch
BranchTrueFalseTotal
/\n\z/u ? : 0 0 0
105 0 my @to;
106 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::gpt4::method}{"max"}) // die()
l!l
00
$App::Greple::xlate::max_length or $param{$App::Greple::xlate::gpty::gpt4::method}{"max"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::gpty::gpt4::method}{"max"}) // die()
$App::Greple::xlate::max_length $param{$App::Greple::xlate::gpty::gpt4::method}{"max"}
Truth table0%0/3
A or B
A= $App::Greple::xlate::max_lengthB= $param{$App::Greple::xlate::gpty::gpt4::method}{"max"}
ABresult
1X1
011
000
107 0 ▶ if (my @len = grep { $_ > $max } map length, @from) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@len) = grep({$_ > $max;} map(length($_), @from))) 0 0 0
108 0 die "Contain lines longer than max length (@len > $max).\n";
109 }
110 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
111 0 my @tmp;
112 0 my $len = 0;
113 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
114 0 my $next = length $from[0];
115 0 ▶ last if $len + $next > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $len + $next > $max 0 0 0
116 0 $len += $next;
117 0 push @tmp, shift @from;
118 }
119 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
120 0 push @to, xlate_each @tmp;
121 }
122 0 @to;
123 }
124  
125 1 1;
126  
127 __DATA__
« blib/lib/App/Greple/xlate/gpty/gpt3.pm ↑ Summary lib/App/Greple/xlate.pm »