bin/wer-cer-ru

filter: Statementstmt 90.6% 58 / 64 Branchbran 53.5% 15 / 28 Conditioncond 33.3% 6 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 69.5% 89 / 128 CC 2 SCAR 2.0
CC
2
Cov
72%
read_text
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« blib/lib/Lingua/RU/WER.pm ↑ Summary
1 #!perl
2 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Lingua::RU::WER qw(score report);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 binmode STDOUT, ':encoding(UTF-8)';
12 3 binmode STDERR, ':encoding(UTF-8)';
13  
14 3 my $USAGE = join "\n",
15 'Использование: wer-cer-ru <эталон.txt> <распознано.txt> [ключи]',
16 '',
17 ' --json результат в JSON',
18 ' --ops показать выравнивание по словам',
19 ' --keep-case не приводить к нижнему регистру',
20 ' --keep-punct не убирать знаки препинания',
21 ' --keep-yo различать «ё» и «е»',
22 ' --keep-markup не убирать таймкоды и имена спикеров',
23 ' --keep-notes не убирать пометки в скобках',
24 ' --drop-hes убрать заминки «э», «эм», «ммм», «хм»',
25 ' --split-hyphen считать «как-то» двумя словами';
26  
27 3 ▶my @flags = grep { /^--/ || $_ eq '-h' } @ARGV;
Condition66%2/3
/^--/ || $_ eq '-h'
l!l&&r!l&&!r
206
MC/DC50%1/2
/^--/ || $_ eq '-h'
/^--/ $_ eq '-h'
Truth table66%2/3
A || B
A= /^--/B= $_ eq '-h'
ABresult
1X1
011
000
28 3 ▶my @files = grep { !/^--/ && $_ ne '-h' } @ARGV;
Condition66%2/3
!/^--/ && $_ ne '-h'
!ll&&!rl&&r
206
MC/DC50%1/2
!/^--/ && $_ ne '-h'
!/^--/ $_ ne '-h'
Truth table66%2/3
not(A) && B
A= !/^--/B= $_ ne '-h'
ABresult
0X0
100
111
29 3 my %has = map { $_ => 1 } @flags;
30  
31 3 ▶if ($has{'--help'} || $has{'-h'} || @files != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has{"--help"} or $has{"-h"} or @files != 2) 0 3 3
Condition33%2/6
$has{"--help"} or $has{"-h"} or @files != 2
l!l&&r!l&&!r
003
$has{"--help"} or $has{"-h"}
l!l&&r!l&&!r
003
MC/DC0%0/3
$has{"--help"} or $has{"-h"} or @files != 2
$has{"--help"} $has{"-h"} @files != 2
Truth table25%1/4
A or B or C
A= $has{"--help"}B= $has{"-h"}C= @files != 2
ABCresult
1XX1
01X1
0011
0000
32 0 say $USAGE;
33 0 ▶ exit(@files == 2 || $has{'--help'} || $has{'-h'} ? 0 : 2);
Branch: 1 branch
BranchTrueFalseTotal
@files == 2 || $has{'--help'} || $has{'-h'} ? : 0 0 0
Condition0%0/6
@files == 2 or $has{"--help"} or $has{"-h"}
l!l&&r!l&&!r
000
@files == 2 or $has{"--help"}
l!l&&r!l&&!r
000
MC/DC0%0/3
@files == 2 or $has{"--help"} or $has{"-h"}
@files == 2 $has{"--help"} $has{"-h"}
Truth table0%0/4
A or B or C
A= @files == 2B= $has{"--help"}C= $has{"-h"}
ABCresult
1XX1
01X1
0011
0000
34 }
35  
36 6 ▶sub read_text {
Subroutine read_text: called CC=2 SCAR=6.9
37 6 my ($path) = @_;
38 6 ▶ open my $fh, '<:raw', $path or die "wer-cer-ru: $path: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or die "wer-cer-ru: ${path}: $!\n" 6 0 6
39 6 my $bytes = do { local $/; <$fh> };
40 6 close $fh;
41 6 my $text = decode('UTF-8', $bytes);
42 6 $text =~ s/^\x{FEFF}//;
43 6 return $text;
44 }
45  
46 my %options = (
47 lower => !$has{'--keep-case'},
48 punct => !$has{'--keep-punct'},
49 yo => !$has{'--keep-yo'},
50 markup => !$has{'--keep-markup'},
51 notes => !$has{'--keep-notes'},
52 hes => $has{'--drop-hes'},
53 3 hyphen => $has{'--split-hyphen'},
54 );
55 3 my $r = score(read_text($files[0]), read_text($files[1]), \%options);
56  
57 3 ▶if ($has{'--json'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has{"--json"}) 1 2 3
58 my %out = (
59 wer => $r->{wer},
60 cer => $r->{cer},
61 cerApprox => $r->{cer_approx} ? JSON::PP::true : JSON::PP::false,
62 words => $r->{n},
63 hypothesisWords => $r->{m},
64 substitutions => $r->{s},
65 deletions => $r->{d},
66 insertions => $r->{i},
67 hits => $r->{h},
68 topSubstitutions => $r->{pairs},
69 1 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$r->{'cer_approx'} ? : 0 1 1
70 1 ▶ $out{ops} = $r->{ops} if $has{'--ops'};
Branch: 1 branch
BranchTrueFalseTotal
if $has{"--ops"} 0 1 1
71 1 print JSON::PP->new->canonical->pretty->indent_length(2)->encode(\%out);
72 1 ▶ exit(defined $r->{wer} ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $r->{'wer'} ? : 1 0 1
73 }
74 2 ▶if ($r->{too_long}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"too_long"}) 0 2 2
75 0 print STDERR "Тексты слишком длинные для выравнивания: разбейте запись на части примерно по 4 000 слов.\n";
76 0 exit 1;
77 }
78 2 ▶if (!$r->{n}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"n"}) 0 2 2
79 0 print STDERR "В эталоне не осталось слов после нормализации.\n";
80 0 exit 1;
81 }
82 2 say report($r);
83 2 ▶if (@{ $r->{pairs} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$r->{"pairs"};}) 2 0 2
84 2 say 'Частые замены:';
85 2 say " $_->[1]× $_->[0]" for @{ $r->{pairs} };
86 }
87 2 ▶if ($has{'--ops'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has{"--ops"}) 1 1 2
88 say join ' ', map {
89 3 ▶ $_->[0] eq '=' ? $_->[1]
Branch: 3 branches
BranchTrueFalseTotal
$_->[0] eq '=' ? : 2 1 3
$_->[0] eq 's' ? : 1 0 1
$_->[0] eq 'd' ? : 0 0 0
90 : $_->[0] eq 's' ? "[$_->[1]→$_->[2]]"
91 : $_->[0] eq 'd' ? "[-$_->[1]]"
92 : "[+$_->[2]]"
93 1 } @{ $r->{ops} };
94 }
95  
96 __END__
« blib/lib/Lingua/RU/WER.pm ↑ Summary