| 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"
MC/DC0%0/1
our $lang_from //= "ORIGINAL"
our $lang_from
Truth table50%1/2
A
A= our $lang_from
| |||||||||||||||||||||||||
| 18 | 1 | ▶ | our $lang_to //= 'JA'; | ||||||||||||||||||||||
|
Condition50%1/2
our $lang_to //= "JA"
MC/DC0%0/1
our $lang_to //= "JA"
our $lang_to
Truth table50%1/2
A
A= our $lang_to
| |||||||||||||||||||||||||
| 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'}
MC/DC0%0/2
opt('prompt') || $param->{'prompt'}
opt('prompt')
$param->{'prompt'}
Truth table0%0/3
A || B
A= opt('prompt')B= $param->{'prompt'}
| |||||||||||||||||||||||||
| 41 | 0 | my @vars = do { | |||||||||||||||||||||||
| 42 | 0 | ▶ | if ($prompt =~ /%s/) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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")
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}
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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()
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"}
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 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()
$App::Greple::xlate::max_length or $param{$App::Greple::xlate::gpty::gpt3::method}{"max"}
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"}
| |||||||||||||||||||||||||
| 81 | 0 | ▶ | if (my @len = grep { $_ > $max } map length, @from) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 82 | 0 | die "Contain lines longer than max length (@len > $max).\n"; | |||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||
| 84 | 0 | ▶ | while (@from) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 85 | 0 | my @tmp; | |||||||||||||||||||||||
| 86 | 0 | my $len = 0; | |||||||||||||||||||||||
| 87 | 0 | ▶ | while (@from) { | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 88 | 0 | my $next = length $from[0]; | |||||||||||||||||||||||
| 89 | 0 | ▶ | last if $len + $next > $max; | ||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||
| 94 | 0 | push @to, xlate_each @tmp; | |||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||
| 96 | 0 | @to; | |||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||
| 99 | 1 | 1; | |||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||
| 101 | __DATA__ | ||||||||||||||||||||||||