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

filter: Statementstmt 38.8% 33 / 85 Branchbran 0.0% 0 / 28 Conditioncond 8.3% 2 / 24 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 64.2% 9 / 14 Podpod 0.0% 0 / 4 totaltotal 26.1% 44 / 168 CC 23 SCAR 42.2
CC
23
Cov
26%
xlate
156.0
xlate_each
56.0
gpty
30.0
CRAP 67.7
↑ Summary blib/lib/App/Greple/xlate/gpty/gpt5.pm »
1 package App::Greple::xlate::gpty::gpt4o;
2  
3 2 our $VERSION = "2.03";
4  
5 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use List::Util qw(sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Command::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use App::Greple::xlate qw(opt);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use App::Greple::xlate::Lang qw(%LANGNAME);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶our $lang_from //= 'ORIGINAL';
Condition50%1/2
our $lang_from //= "ORIGINAL"
l!l
02
MC/DC0%0/1
our $lang_from //= "ORIGINAL"
our $lang_from
Truth table50%1/2
A
A= our $lang_from
Aresult
11
00
18 2 ▶our $lang_to //= 'JA';
Condition50%1/2
our $lang_to //= "JA"
l!l
02
MC/DC0%0/1
our $lang_to //= "JA"
our $lang_to
Truth table50%1/2
A
A= our $lang_to
Aresult
11
00
19 2 our $auth_key;
20 2 our $method = __PACKAGE__ =~ s/.*://r;
21  
22 2 my %param = (
23 gpt4o => { engine => 'gpt-4o-mini', temp => '0.0', max => 10000, sub => \&gpty,
24 prompt => <<END
25 Translate the following text into %s, preserving the line structure.
26 For each input line, output only the corresponding translated line in the same line position.
27 Leave blank lines unchanged. Within every line, preserve XML-style tags (e.g., <m id="1" />, <tag>, </tag>) exactly and translate only the surrounding text.
28 Do not output the original (pre-translation) text under any circumstances.
29 The number and order of output lines must always match the input exactly: output line n must correspond to input line n.
30 Output only the translated lines or unchanged tags/blank lines.
31 **Before finishing, carefully check that there are absolutely no omissions or duplicate content of any kind in your output.**
32 END
33 },
34 );
35  
36 0 ▶sub initialize {
Subroutine initialize: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my($mod, $argv) = @_;
38 0 $mod->setopt(default => "-Mxlate --xlate-engine=$method");
39 }
40  
41 0 ▶sub gpty {
Subroutine gpty: not called CC=5 SCAR=34.0
Pod undocumented
42 0 state $gpty = Command::Run->new;
43 0 my $text = shift;
44 0 my $param = $param{$method};
45 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
46 0 my @vars = do {
47 0 ▶ if ($prompt =~ /%s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prompt =~ /%s/u) { } 0 0 0
48 0 ▶ $LANGNAME{$lang_to} // die "$lang_to: unknown lang.\n"
Condition0%0/2
$App::Greple::xlate::gpty::gpt4o::LANGNAME{$App::Greple::xlate::gpty::gpt4o::lang_to} // die("$App::Greple::xlate::gpty::gpt4o::lang_to: unknown lang.\n")
l!l
00
MC/DC0%0/1
$App::Greple::xlate::gpty::gpt4o::LANGNAME{$App::Greple::xlate::gpty::gpt4o::lang_to} // die("$App::Greple::xlate::gpty::gpt4o::lang_to: unknown lang.\n")
$App::Greple::xlate::gpty::gpt4o::LANGNAME{$App::Greple::xlate::gpty::gpt4o::lang_to}
Truth table0%0/2
A
A= $App::Greple::xlate::gpty::gpt4o::LANGNAME{$App::Greple::xlate::gpty::gpt4o::lang_to}
Aresult
11
00
49 } else {
50 0 ();
51 }
52 };
53 0 my $system = sprintf($prompt, @vars);
54 my @command = (
55 'gpty',
56 -e => $param->{engine},
57 -t => $param->{temp},
58 0 -s => $system,
59 '-',
60 );
61 0 ▶ warn Dumper \@command if opt('debug');
Branch: 1 branch
BranchTrueFalseTotal
if opt("debug") 0 0 0
62 0 $gpty->command(@command)->with(stdin => $text)->update->data;
63 }
64  
65 0 ▶sub _progress {
Subroutine _progress: not called CC=2 SCAR=17.9
66 0 ▶ print STDERR @_ if opt('progress');
Branch: 1 branch
BranchTrueFalseTotal
if opt("progress") 0 0 0
67 }
68  
69 0 ▶sub xlate_each {
Subroutine xlate_each: not called CC=7 SCAR=40.3
Pod undocumented
70 0 ▶ my $call = $param{$method}->{sub} // die;
Condition0%0/2
$param{$App::Greple::xlate::gpty::gpt4o::method}{"sub"} // die()
l!l
00
MC/DC0%0/1
$param{$App::Greple::xlate::gpty::gpt4o::method}{"sub"} // die()
$param{$App::Greple::xlate::gpty::gpt4o::method}{"sub"}
Truth table0%0/2
A
A= $param{$App::Greple::xlate::gpty::gpt4o::method}{"sub"}
Aresult
11
00
71 0 my @count = map { int tr/\n/\n/ } @_;
72 0 my $lines = sum @count;
73 0 _progress("From:\n", map s/^/\t< /mgr, @_);
74 0 my $to = $call->(join '', @_);
75 0 ▶ if ((my @to = split /(?<=\n)\n+/, $to) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my @to = split(/(?<=\n)\n+/u, $to, 0)) > 1) 0 0 0
76 0 ▶ $to = join '', map { /(.+\n)\z/ ? $1 : die } @to;
Branch: 1 branch
BranchTrueFalseTotal
/(.+\n)\z/u ? : 0 0 0
77 }
78 0 my @out = $to =~ /^.+\n/mg;
79 0 _progress("To:\n", map s/^/\t> /mgr, @out);
80 0 ▶ if ($lines == 1 and @out > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines == 1 and @out > 1) 0 0 0
Condition0%0/3
$lines == 1 and @out > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$lines == 1 and @out > 1
$lines == 1 @out > 1
Truth table0%0/3
A and B
A= $lines == 1B= @out > 1
ABresult
0X0
100
111
81 0 @out = ( join "", splice @out );
82 }
83 0 ▶ if (@out != $lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out != $lines) 0 0 0
84 0 die sprintf("\nUnexpected response: [ %d != %d ]\n\n%s\n",
85 @out+0, $lines, $to);
86 }
87 0 map { join '', splice @out, 0, $_ } @count;
88 }
89  
90 0 ▶sub xlate {
Subroutine xlate: not called CC=12 SCAR=50.5
Pod undocumented
91 0 ▶ my @from = map { /\n\z/ ? $_ : "$_\n" } @_;
Branch: 1 branch
BranchTrueFalseTotal
/\n\z/u ? : 0 0 0
92 0 my @to;
93 0 ▶ my $maxsize = $App::Greple::xlate::max_length || $param{$method}->{max} // die;
Condition0%0/5
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::gpty::gpt4o::method}{"max"}) // die()
l!l
00
$App::Greple::xlate::max_length or $param{$App::Greple::xlate::gpty::gpt4o::method}{"max"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($App::Greple::xlate::max_length || $param{$App::Greple::xlate::gpty::gpt4o::method}{"max"}) // die()
$App::Greple::xlate::max_length $param{$App::Greple::xlate::gpty::gpt4o::method}{"max"}
Truth table0%0/3
A or B
A= $App::Greple::xlate::max_lengthB= $param{$App::Greple::xlate::gpty::gpt4o::method}{"max"}
ABresult
1X1
011
000
94 0 ▶ my $maxline = $App::Greple::xlate::max_line || 1;
Condition0%0/2
$App::Greple::xlate::max_line || 1
l!l
00
MC/DC0%0/1
$App::Greple::xlate::max_line || 1
$App::Greple::xlate::max_line
Truth table0%0/2
A
A= $App::Greple::xlate::max_line
Aresult
11
00
95 0 ▶ if (my @len = grep { $_ > $maxsize } map length, @from) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@len) = grep({$_ > $maxsize;} map(length($_), @from))) 0 0 0
96 0 die "Contain lines longer than max length (@len > $maxsize).\n";
97 }
98 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
99 0 my @tmp;
100 0 my $len = 0;
101 0 ▶ while (@from) {
Branch: 1 branch
BranchTrueFalseTotal
if (@from) 0 0 0
102 0 my $next = length $from[0];
103 0 ▶ last if $len + $next > $maxsize;
Branch: 1 branch
BranchTrueFalseTotal
if $len + $next > $maxsize 0 0 0
104 0 $len += $next;
105 0 push @tmp, shift @from;
106 0 ▶ last if $maxline > 0 and @tmp >= $maxline;
Branch: 1 branch
BranchTrueFalseTotal
if $maxline > 0 and @tmp >= $maxline 0 0 0
Condition0%0/3
$maxline > 0 and @tmp >= $maxline
!ll&&!rl&&r
000
MC/DC0%0/2
$maxline > 0 and @tmp >= $maxline
$maxline > 0 @tmp >= $maxline
Truth table0%0/3
A and B
A= $maxline > 0B= @tmp >= $maxline
ABresult
0X0
100
111
107 }
108 0 ▶ @tmp > 0 or die "Probably text is longer than max length ($maxsize).\n";
Branch: 1 branch
BranchTrueFalseTotal
@tmp > 0 or die "Probably text is longer than max length ($maxsize).\n" 0 0 0
109 0 push @to, xlate_each @tmp;
110 }
111 0 @to;
112 }
113  
114 2 1;
115  
116 __DATA__
↑ Summary blib/lib/App/Greple/xlate/gpty/gpt5.pm »