blib/lib/Langertha/Role/Transcription.pm

filter: Statementstmt 76.9% 80 / 104 Branchbran 43.1% 19 / 44 Conditioncond 43.7% 7 / 16 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 78.9% 15 / 19 Podpod 100% 11 / 11 totaltotal 66.6% 136 / 204 CC 31 SCAR 26.7
CC
31
Cov
65%
transcription_file_part
30.4
transcription_file_part
30.4
_build_transcription_model
5.9
CRAP 14.5
« blib/lib/Langertha/Manifest/Builder.pm ↑ Summary blib/lib/Langertha/Role/AsyncHTTP.pm »
1 package Langertha::Role::Transcription;
2 # ABSTRACT: Role for APIs with transcription functionality
3 115 our $VERSION = '0.505';
4 115 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 115 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 115 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 115 ▶use Scalar::Util qw( openhandle );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 115 ▶use Time::HiRes qw( gettimeofday tv_interval );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 115 ▶use Langertha::CallResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # The _f methods send through the engine's async backend (k292): injected
12 # client > Net::Async::HTTP > the sync LWP shim (ADR 0027).
13 115 with 'Langertha::Role::AsyncHTTP';
14  
15 115 requires qw(
16 transcription_request
17 transcription_response
18 );
19  
20 115 has transcription_model => (
21 is => 'ro',
22 isa => 'Str',
23 lazy_build => 1,
24 );
25 41 ▶sub _build_transcription_model {
Subroutine _build_transcription_model: called CC=3 SCAR=17.8
26 0 ▶ my ( $self ) = @_;
Subroutine _build_transcription_model: not called CC=3 SCAR=17.8
27 0 ▶ croak "".(ref $self)." can't handle models!" unless $self->does('Langertha::Role::Models');
Branch: 2 branches
BranchTrueFalseTotal
unless $self->does("Langertha::Role::Models") 0 41 41
unless $self->does("Langertha::Role::Models") 0 0 0
28 0 ▶ return $self->default_transcription_model if $self->can('default_transcription_model');
Branch: 2 branches
BranchTrueFalseTotal
if $self->can("default_transcription_model") 41 0 41
if $self->can("default_transcription_model") 0 0 0
29 0 return $self->model;
30 }
31  
32  
33 64 ▶sub transcription_file_part {
Subroutine transcription_file_part: called CC=11 SCAR=34.1
Pod documented
34 0 ▶ my ( $self, $input, $filename ) = @_;
Subroutine transcription_file_part: not called CC=11 SCAR=34.1
Pod documented
35 0 ▶ croak "".(ref $self).": no audio given for transcription" unless defined $input;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $input 0 64 64
unless defined $input 0 0 0
36 0 my $bytes;
37 0 ▶ if ( ref $input eq 'SCALAR' ) {
Branch: 6 branches
BranchTrueFalseTotal
if (ref $input eq 'SCALAR') { } 28 36 64
elsif (&openhandle($input)) { } 1 35 36
elsif (not ref $input and index($input, "\000") != -1) { } 1 34 35
if (ref $input eq 'SCALAR') { } 0 0 0
elsif (&openhandle($input)) { } 0 0 0
elsif (not ref $input and index($input, "\000") != -1) { } 0 0 0
38 0 $bytes = $$input;
39 }
40 elsif ( openhandle($input) ) {
41 0 binmode $input;
42 0 local $/;
43 0 $bytes = readline $input;
44 0 ▶ croak "".(ref $self).": cannot read audio from filehandle: $!" unless defined $bytes;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $bytes 0 1 1
unless defined $bytes 0 0 0
45 }
46 ▶ elsif ( !ref $input && index( $input, "\0" ) >= 0 ) {
Condition50%3/6
not ref $input and index($input, "\0") != -1
!ll&&!rl&&r
3041
not ref $input and index($input, "\0") != -1
!ll&&!rl&&r
000
MC/DC100%2/2
not ref $input and index($input, "\0") != -1
not ref $input index($input, "\0") != -1
MC/DC0%0/2
not ref $input and index($input, "\0") != -1
not ref $input index($input, "\0") != -1
Truth table100%3/3
not(A) and B
A= not ref $inputB= index($input, "\0") != -1
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not ref $inputB= index($input, "\0") != -1
ABresult
0X0
100
111
47 # No path contains a NUL byte, and nearly every audio container header
48 # does (RIFF sizes, ID3, OggS, fLaC) -- so this is content, and it never
49 # reaches open() or a file test. -- karr k287
50 0 $bytes = $input;
51 }
52 else {
53 0 ▶ return [ $input, defined $filename ? ( $filename ) : () ];
Branch: 2 branches
BranchTrueFalseTotal
defined $filename ? : 1 33 34
defined $filename ? : 0 0 0
54 }
55 0 ▶ croak "".(ref $self).": audio content must be bytes, not a character string"
Branch: 2 branches
BranchTrueFalseTotal
if utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1) 1 29 30
if utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1) 0 0 0
Condition33%2/6
utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1)
!ll&&!rl&&r
2901
utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1)
!ll&&!rl&&r
000
MC/DC50%1/2
utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1)
utf8::is_utf8($bytes) not utf8::downgrade($bytes, 1)
MC/DC0%0/2
utf8::is_utf8($bytes) and not utf8::downgrade($bytes, 1)
utf8::is_utf8($bytes) not utf8::downgrade($bytes, 1)
Truth table66%2/3
A and not(B)
A= utf8::is_utf8($bytes)B= not utf8::downgrade($bytes, 1)
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= utf8::is_utf8($bytes)B= not utf8::downgrade($bytes, 1)
ABresult
0X0
100
111
56 if utf8::is_utf8($bytes) && !utf8::downgrade( $bytes, 1 );
57 0 ▶ return [ undef, $filename // 'audio',
Condition50%2/4
$filename // "audio"
l!l
281
$filename // "audio"
l!l
00
MC/DC100%1/1
$filename // "audio"
$filename
MC/DC0%0/1
$filename // "audio"
$filename
Truth table100%2/2
A
A= $filename
Aresult
11
00
Truth table0%0/2
A
A= $filename
Aresult
11
00
58 'Content-Type' => 'application/octet-stream', Content => $bytes ];
59 }
60  
61  
62 64 ▶sub transcription {
Subroutine transcription: called CC=1 SCAR=1.2
Pod documented
63 0 ▶ my ( $self, $file_or_content, %extra ) = @_;
Subroutine transcription: not called CC=1 SCAR=1.2
Pod documented
64 0 return $self->transcription_request($file_or_content, %extra);
65 }
66  
67  
68 3 ▶sub simple_transcription {
Subroutine simple_transcription: called CC=1 SCAR=1.2
Pod documented
69 0 ▶ my ( $self, $file_or_content, %extra ) = @_;
Subroutine simple_transcription: not called CC=1 SCAR=1.2
Pod documented
70 0 my $request = $self->transcription($file_or_content, %extra);
71 0 my $response = $self->user_agent->request($request);
72 0 return $request->response_call->($response);
73 }
74  
75  
76 9 ▶async sub simple_transcription_f {
Subroutine simple_transcription_f: called CC=1 SCAR=0.0
Pod documented
77 9 my ( $self, $file_or_content, %extra ) = @_;
78 9 my $request = $self->transcription($file_or_content, %extra);
79 9 my $response = await $self->_async_do_request_f( request => $request );
80 8 return $request->response_call->($response);
81 115 }
82  
83  
84 3 ▶sub simple_transcription_result {
Subroutine simple_transcription_result: called CC=2 SCAR=7.0
Pod documented
85 3 my ( $self, $file_or_content, %extra ) = @_;
86 3 ▶ croak "".(ref $self)." has no transcription_result" unless $self->can('transcription_result');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("transcription_result") 0 3 3
87 3 my $request = $self->transcription($file_or_content, %extra);
88 3 return $self->transcription_result( $self->user_agent->request($request) );
89 }
90  
91  
92 7 ▶async sub simple_transcription_result_f {
Subroutine simple_transcription_result_f: called CC=2 SCAR=7.0
Pod documented
93 7 my ( $self, $file_or_content, %extra ) = @_;
94 7 ▶ croak "".(ref $self)." has no transcription_result" unless $self->can('transcription_result');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("transcription_result") 0 7 7
95 7 my $request = $self->transcription($file_or_content, %extra);
96 7 my $response = await $self->_async_do_request_f( request => $request );
97 6 return $self->transcription_result($response);
98 115 }
99  
100  
101 1 ▶sub simple_transcription_call {
Subroutine simple_transcription_call: called CC=2 SCAR=7.0
Pod documented
102 1 my ( $self, $file_or_content, %extra ) = @_;
103 1 ▶ my $model = exists $extra{model} ? $extra{model} : $self->transcription_model;
Branch: 1 branch
BranchTrueFalseTotal
exists $extra{'model'} ? : 0 1 1
104 1 my $request = $self->transcription($file_or_content, %extra);
105 1 my $t0 = [gettimeofday];
106 1 my $response = $self->user_agent->request($request);
107 1 my $elapsed = tv_interval($t0);
108 1 my $value = $request->response_call->($response);
109 1 return Langertha::CallResult->from_http_response( $self, $response,
110 value => $value, model => $model, total_seconds => $elapsed );
111 }
112  
113  
114 6 ▶async sub simple_transcription_call_f {
Subroutine simple_transcription_call_f: called CC=2 SCAR=6.9
Pod documented
115 6 my ( $self, $file_or_content, %extra ) = @_;
116 6 ▶ my $model = exists $extra{model} ? $extra{model} : $self->transcription_model;
Branch: 1 branch
BranchTrueFalseTotal
exists $extra{'model'} ? : 2 4 6
117 6 my $request = $self->transcription($file_or_content, %extra);
118 6 my $t0 = [gettimeofday];
119 6 my $response = await $self->_async_do_request_f( request => $request );
120 6 my $elapsed = tv_interval($t0);
121 6 my $value = $request->response_call->($response);
122 6 return Langertha::CallResult->from_http_response( $self, $response,
123 value => $value, model => $model, total_seconds => $elapsed );
124 115 }
125  
126  
127  
128 115 1;
129  
130 __END__
« blib/lib/Langertha/Manifest/Builder.pm ↑ Summary blib/lib/Langertha/Role/AsyncHTTP.pm »