| 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'
MC/DC50%1/2
/^--/ || $_ eq '-h'
/^--/
$_ eq '-h'
Truth table66%2/3
A || B
A= /^--/B= $_ eq '-h'
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 3 | ▶ | my @files = grep { !/^--/ && $_ ne '-h' } @ARGV; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
!/^--/ && $_ ne '-h'
MC/DC50%1/2
!/^--/ && $_ ne '-h'
!/^--/
$_ ne '-h'
Truth table66%2/3
not(A) && B
A= !/^--/B= $_ ne '-h'
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | my %has = map { $_ => 1 } @flags; | |||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | if ($has{'--help'} || $has{'-h'} || @files != 2) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$has{"--help"} or $has{"-h"} or @files != 2
$has{"--help"} or $has{"-h"}
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
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | say $USAGE; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | exit(@files == 2 || $has{'--help'} || $has{'-h'} ? 0 : 2); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
@files == 2 or $has{"--help"} or $has{"-h"}
@files == 2 or $has{"--help"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | $out{ops} = $r->{ops} if $has{'--ops'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | print JSON::PP->new->canonical->pretty->indent_length(2)->encode(\%out); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | ▶ | exit(defined $r->{wer} ? 0 : 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | if ($r->{too_long}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | print STDERR "Тексты слишком длинные для выравнивания: разбейте запись на части примерно по 4 000 слов.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | if (!$r->{n}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | print STDERR "В эталоне не осталось слов после нормализации.\n"; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | say report($r); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | if (@{ $r->{pairs} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | say 'Частые замены:'; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | say " $_->[1]× $_->[0]" for @{ $r->{pairs} }; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | if ($has{'--ops'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | say join ' ', map { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | ▶ | $_->[0] eq '=' ? $_->[1] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | : $_->[0] eq 's' ? "[$_->[1]→$_->[2]]" | ||||||||||||||||||||||||||||||||||||||||||
| 91 | : $_->[0] eq 'd' ? "[-$_->[1]]" | ||||||||||||||||||||||||||||||||||||||||||
| 92 | : "[+$_->[2]]" | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | } @{ $r->{ops} }; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||||||||||||||