blib/lib/Lingua/RU/WER.pm

filter: Statementstmt 97.9% 191 / 195 Branchbran 84.0% 79 / 94 Conditioncond 73.5% 25 / 34 MC/DCmcdc 47.3% 9 / 19 Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 89.6% 329 / 367 CC 71 SCAR 23.9
CC
71
Cov
91%
align
18.1
strip_markup
14.8
score
14.3
CRAP 10.9
↑ Summary bin/wer-cer-ru »
1 package Lingua::RU::WER;
2  
3 4 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.01';
13  
14 4 our @EXPORT_OK = qw(score report normalize strip_markup align char_distance percent);
15 4 our %EXPORT_TAGS = (all => \@EXPORT_OK);
16  
17 4 our %DEFAULTS = (lower => 1, punct => 1, yo => 1, markup => 1, notes => 1, hes => 0, hyphen => 0);
18  
19 4 our $WORD_LIMIT = 25_000_000;
20 4 our $CHAR_LIMIT = 120_000_000;
21  
22 4 my $LET = 'A-Za-zА-Яа-яЁё';
23 4 my $TCP = '(?:[0-9]{1,2}:)?[0-9]{1,2}:[0-9]{2}(?:[.,][0-9]{1,3})?';
24 4 my $TCR = $TCP . '(?:\s*(?:-->|[-–—])\s*' . $TCP . ')?';
25 4 my $NAME = '[' . $LET . '][0-9' . $LET . '.\-]*(?:[ \t]+[0-9' . $LET . '][0-9' . $LET . '.\-]*){0,2}';
26  
27 4 my $TC_BR = qr/\[$TCR\]|\($TCR\)/;
28 4 my $TC_LEAD = qr/^[ \t]*$TCR(?=\s|$|:)[ \t]*:?/m;
29 4 my $TC_SRT = qr/$TCP\s*-->\s*$TCP/;
30 4 my $LABEL = qr/^[ \t]*($NAME)[ \t]*:(?=\s|$)/;
31 4 my $LABEL_TC = qr/^[ \t]*($NAME)[ \t]+$TCR[ \t]*:?[ \t]*$/;
32 4 my $GENERIC = qr/^(?:спикер|говорящий|говорящая|диктор|собеседник|собеседница|участник|участница|интервьюер|респондент|ведущий|ведущая|гость|модератор|журналист|эксперт|speaker|spk)(?:[ _#№-]*(?:[0-9]{1,3}|[A-ZА-Я]))?$/i;
33 4 my $NOTES = qr/\[[^\]\n]{1,80}\]|\([^)\n]{1,80}\)/;
34 4 my $HES = qr/^(?:э+(?:-э+)*|э+м+(?:-м+)*|м{2,}|м+(?:-м+)+|х+м+|г+м+)$/;
35  
36 # Timecodes, SRT numbering and speaker labels. A plain name counts as a label only when it opens
37 # at least two lines; generic labels («Спикер 1», «Ведущий») count at once.
38 37 ▶sub strip_markup {
Subroutine strip_markup: called CC=14 SCAR=26.9
Pod documented
39 37 my ($text) = @_;
40 37 ▶ my $t = defined $text ? "$text" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 37 0 37
41 37 $t =~ s/$TC_BR/ /g;
42 37 my $srt = index($t, '-->') >= 0;
43 37 $t =~ s/$TC_SRT/ /g;
44 37 my @lines = split /\n/, $t, -1;
45 37 s/$TC_LEAD//g for @lines;
46 37 ▶ if ($srt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($srt) 1 36 37
47 1 ▶ for (@lines) { $_ = '' if /^\s*[0-9]{1,5}\s*$/ }
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*[0-9]{1,5}\s*$/ 2 5 7
48 }
49 37 my %count;
50 37 for my $l (@lines) {
51 43 ▶ if ($l =~ $LABEL_TC or $l =~ $LABEL) { $count{ lc $1 }++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($l =~ /$LABEL_TC/ or $l =~ /$LABEL/) 4 39 43
Condition66%2/3
$l =~ /$LABEL_TC/ or $l =~ /$LABEL/
l!l&&r!l&&!r
0439
MC/DC50%1/2
$l =~ /$LABEL_TC/ or $l =~ /$LABEL/
$l =~ /$LABEL_TC/ $l =~ /$LABEL/
Truth table66%2/3
A or B
A= $l =~ /$LABEL_TC/B= $l =~ /$LABEL/
ABresult
1X1
011
000
52 }
53 4 ▶ my $is_label = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
54 4 my $name = shift;
55 4 (my $trimmed = $name) =~ s/^\s+|\s+$//g;
56 4 ▶ return $trimmed =~ $GENERIC || ($count{ lc $name } // 0) >= 2;
Condition80%4/5
$trimmed =~ /$GENERIC/i || ($count{lc $name} // 0) >= 2
l!l&&r!l&&!r
121
$count{lc $name} // 0
l!l
30
MC/DC0%0/2
$trimmed =~ /$GENERIC/i || ($count{lc $name} // 0) >= 2
$trimmed =~ /$GENERIC/i $count{lc $name}
Truth table66%2/3
A || B
A= $trimmed =~ /$GENERIC/iB= $count{lc $name}
ABresult
1X1
011
000
57 37 };
58 37 my @out;
59 37 for my $l (@lines) {
60 43 ▶ if ($l =~ $LABEL_TC) {
Branch: 2 branches
BranchTrueFalseTotal
if ($l =~ /$LABEL_TC/) { } 0 43 43
elsif ($l =~ /$LABEL/) { } 4 39 43
61 0 my $name = $1;
62 0 ▶ push @out, $is_label->($name) ? '' : $l;
Branch: 1 branch
BranchTrueFalseTotal
&$is_label($name) ? : 0 0 0
63 }
64 elsif ($l =~ $LABEL) {
65 4 my ($name, $end) = ($1, $+[0]);
66 4 ▶ push @out, $is_label->($name) ? substr($l, $end) : $l;
Branch: 1 branch
BranchTrueFalseTotal
&$is_label($name) ? : 3 1 4
67 }
68 else {
69 39 push @out, $l;
70 }
71 }
72 37 return join "\n", @out;
73 }
74  
75 23 ▶sub _with_defaults {
Subroutine _with_defaults: called CC=5 SCAR=16.1
76 23 my ($o) = @_;
77 23 ▶ $o //= {};
Condition100%2/2
$o //= {}
l!l
617
MC/DC100%1/1
$o //= {}
$o
Truth table100%2/2
A
A= $o
Aresult
11
00
78 23 ▶ croak 'options must be a hash reference' unless ref $o eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $o eq "HASH" 0 23 23
79 23 ▶ return { map { $_ => (defined $o->{$_} ? ($o->{$_} ? 1 : 0) : $DEFAULTS{$_}) } keys %DEFAULTS };
Branch: 2 branches
BranchTrueFalseTotal
defined $o->{$_} ? : 18 143 161
$o->{$_} ? : 17 1 18
80 }
81  
82 37 ▶sub _normalize {
Subroutine _normalize: called CC=9 SCAR=22.3
83 37 my ($text, $o) = @_;
84 37 ▶ my $t = defined $text ? "$text" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $text ? : 37 0 37
85 37 $t =~ s/\r\n?/\n/g;
86 37 ▶ $t = strip_markup($t) if $o->{markup};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"markup"} 37 0 37
87 37 ▶ $t =~ s/$NOTES/ /g if $o->{notes};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"notes"} 37 0 37
88 37 ▶ $t =~ tr/ёЁ/еЕ/ if $o->{yo};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"yo"} 36 1 37
89 37 ▶ $t = lc $t if $o->{lower};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"lower"} 37 0 37
90 37 $t =~ s/[’ʼ`]/'/g;
91 37 ▶ if ($o->{punct}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->{"punct"}) 37 0 37
92 37 $t =~ s/[^0-9$LET'\-\s]/ /g;
93 37 $t =~ s/(^|[^0-9$LET])['\-]+/$1 /g;
94 37 $t =~ s/['\-]+(?=[^0-9$LET]|$)/ /g;
95 }
96 37 ▶ $t =~ s/-/ /g if $o->{hyphen};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"hyphen"} 1 36 37
97 37 my @words = grep { length } split /\s+/, $t;
98 37 ▶ @words = grep { lc($_) !~ $HES } @words if $o->{hes};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{"hes"} 1 36 37
99 37 return @words;
100 }
101  
102 9 ▶sub normalize {
Subroutine normalize: called CC=1 SCAR=0.0
Pod documented
103 9 my ($text, $options) = @_;
104 9 return _normalize($text, _with_defaults($options));
105 }
106  
107 # Word-level Levenshtein with a backtrace. Ties prefer a match or substitution, then a deletion.
108 14 ▶sub align {
Subroutine align: called CC=18 SCAR=28.9
Pod documented
109 14 my ($ref, $hyp) = @_;
110 14 my ($n, $m) = (scalar @$ref, scalar @$hyp);
111 14 my $w = $m + 1;
112 14 ▶ return undef if ($n + 1) * $w > $WORD_LIMIT;
Branch: 1 branch
BranchTrueFalseTotal
if ($n + 1) * $w > $Lingua::RU::WER::WORD_LIMIT 1 13 14
113 13 my $bt = "\0" x (($n + 1) * $w);
114 13 my $prev = [0 .. $m];
115 13 my $cur = [];
116 13 vec($bt, $_, 8) = 2 for 0 .. $m;
117 13 for my $i (1 .. $n) {
118 58 $cur->[0] = $i;
119 58 vec($bt, $i * $w, 8) = 1;
120 58 my $ai = $ref->[$i - 1];
121 58 for my $j (1 .. $m) {
122 535 ▶ my $d = $prev->[$j - 1] + ($ai eq $hyp->[$j - 1] ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$ai eq $$hyp[$j - 1] ? : 446 89 535
123 535 my $u = $prev->[$j] + 1;
124 535 my $l = $cur->[$j - 1] + 1;
125 535 ▶ if ($d <= $u && $d <= $l) { $cur->[$j] = $d }
Branch: 2 branches
BranchTrueFalseTotal
if ($d <= $u and $d <= $l) { } 464 71 535
elsif ($u <= $l) { } 35 36 71
Condition100%3/3
$d <= $u and $d <= $l
!ll&&!rl&&r
3536464
MC/DC100%2/2
$d <= $u and $d <= $l
$d <= $u $d <= $l
Truth table100%3/3
A and B
A= $d <= $uB= $d <= $l
ABresult
0X0
100
111
126 35 elsif ($u <= $l) { $cur->[$j] = $u; vec($bt, $i * $w + $j, 8) = 1 }
127 36 else { $cur->[$j] = $l; vec($bt, $i * $w + $j, 8) = 2 }
128 }
129 58 ($prev, $cur) = ($cur, $prev);
130 }
131 13 my @ops;
132 13 my ($i, $j) = ($n, $m);
133 13 ▶ while ($i > 0 || $j > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0 or $j > 0) 60 13 73
Condition66%2/3
$i > 0 or $j > 0
l!l&&r!l&&!r
60013
MC/DC50%1/2
$i > 0 or $j > 0
$i > 0 $j > 0
Truth table66%2/3
A or B
A= $i > 0B= $j > 0
ABresult
1X1
011
000
134 60 my $k = vec($bt, $i * $w + $j, 8);
135 60 ▶ if ($i > 0 && $j > 0 && $k == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i > 0 and $j > 0 and $k == 0) { } 55 5 60
elsif ($i > 0 and $k == 1 || $j == 0) { } 3 2 5
Condition83%5/6
$i > 0 and $j > 0 and $k == 0
!ll&&!rl&&r
2355
$i > 0 and $j > 0
!ll&&!rl&&r
0258
MC/DC66%2/3
$i > 0 and $j > 0 and $k == 0
$i > 0 $j > 0 $k == 0
Truth table75%3/4
A and B and C
A= $i > 0B= $j > 0C= $k == 0
ABCresult
0XX0
10X0
1100
1111
136 55 ▶ push @ops, [$ref->[$i - 1] eq $hyp->[$j - 1] ? '=' : 's', $ref->[$i - 1], $hyp->[$j - 1]];
Branch: 1 branch
BranchTrueFalseTotal
$$ref[$i - 1] eq $$hyp[$j - 1] ? : 45 10 55
137 55 $i--;
138 55 $j--;
139 }
140 ▶ elsif ($i > 0 && ($k == 1 || $j == 0)) {
Condition66%4/6
$i > 0 and $k == 1 || $j == 0
!ll&&!rl&&r
023
$k == 1 or $j == 0
l!l&&r!l&&!r
302
MC/DC33%1/3
$i > 0 and $k == 1 || $j == 0
$i > 0 $k == 1 $j == 0
Truth table50%2/4
A and B or C
A= $i > 0B= $k == 1C= $j == 0
ABCresult
0XX0
1000
11X1
1011
141 3 push @ops, ['d', $ref->[$i - 1], ''];
142 3 $i--;
143 }
144 else {
145 2 push @ops, ['i', '', $hyp->[$j - 1]];
146 2 $j--;
147 }
148 }
149 13 return [reverse @ops];
150 }
151  
152 14 ▶sub char_distance {
Subroutine char_distance: called CC=9 SCAR=22.0
Pod documented
153 14 my ($x, $y) = @_;
154 14 my ($n, $m) = (length $x, length $y);
155 14 ▶ return $m unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1 13 14
156 13 ▶ return $n unless $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $m 0 13 13
157 13 my @x = unpack 'W*', $x;
158 13 my @y = unpack 'W*', $y;
159 13 my $prev = [0 .. $m];
160 13 my $cur = [];
161 13 for my $i (1 .. $n) {
162 294 $cur->[0] = $i;
163 294 my $c = $x[$i - 1];
164 294 for my $j (1 .. $m) {
165 17376 ▶ my $d = $prev->[$j - 1] + ($c == $y[$j - 1] ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$c == $y[$j - 1] ? : 3669 13707 17376
166 17376 my $u = $prev->[$j] + 1;
167 17376 my $l = $cur->[$j - 1] + 1;
168 17376 ▶ $cur->[$j] = $d < $u ? ($d < $l ? $d : $l) : ($u < $l ? $u : $l);
Branch: 3 branches
BranchTrueFalseTotal
$d < $u ? : 8458 8918 17376
$d < $l ? : 322 8136 8458
$u < $l ? : 8875 43 8918
169 }
170 294 ($prev, $cur) = ($cur, $prev);
171 }
172 13 return $prev->[$m];
173 }
174  
175 14 ▶sub score {
Subroutine score: called CC=14 SCAR=26.6
Pod documented
176 14 my ($reference, $hypothesis, $options) = @_;
177 14 my $o = _with_defaults($options);
178 14 my @ref = _normalize($reference, $o);
179 14 my @hyp = _normalize($hypothesis, $o);
180 14 my %r = (n => scalar @ref, m => scalar @hyp, s => 0, d => 0, i => 0, h => 0,
181 ops => undef, wer => undef, cer => undef, cer_approx => 0, pairs => []);
182 14 ▶ return \%r unless @ref;
Branch: 1 branch
BranchTrueFalseTotal
unless @ref 1 13 14
183 13 my $ops = align(\@ref, \@hyp);
184 13 ▶ unless ($ops) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ops) 1 12 13
185 1 $r{too_long} = 1;
186 1 return \%r;
187 }
188 12 $r{ops} = $ops;
189 12 my %pairs;
190 12 for my $op (@$ops) {
191 57 my $k = $op->[0];
192 57 ▶ if ($k eq '=') { $r{h}++ }
Branch: 3 branches
BranchTrueFalseTotal
if ($k eq '=') { } 44 13 57
elsif ($k eq 's') { } 9 4 13
elsif ($k eq 'd') { } 3 1 4
193 9 elsif ($k eq 's') { $r{s}++; $pairs{"$op->[1] → $op->[2]"}++ }
194 3 elsif ($k eq 'd') { $r{d}++ }
195 1 else { $r{i}++ }
196 }
197 12 $r{wer} = ($r{s} + $r{d} + $r{i}) / $r{n};
198 12 my $x = join ' ', @ref;
199 12 my $y = join ' ', @hyp;
200 12 ▶ if ((length($x) + 1) * (length($y) + 1) <= $CHAR_LIMIT) {
Branch: 1 branch
BranchTrueFalseTotal
if ((length($x) + 1) * (length($y) + 1) <= $Lingua::RU::WER::CHAR_LIMIT) { } 11 1 12
201 11 $r{cer} = char_distance($x, $y) / length $x;
202 }
203 else {
204 1 my $e = 0;
205 1 for my $op (@$ops) {
206 3 ▶ if ($op->[0] eq 's') { $e += char_distance($op->[1], $op->[2]) }
Branch: 3 branches
BranchTrueFalseTotal
if ($op->[0] eq 's') { } 1 2 3
elsif ($op->[0] eq 'd') { } 0 2 2
elsif ($op->[0] eq 'i') { } 0 2 2
207 0 elsif ($op->[0] eq 'd') { $e += length($op->[1]) + 1 }
208 0 elsif ($op->[0] eq 'i') { $e += length($op->[2]) + 1 }
209 }
210 1 $r{cer} = $e / length $x;
211 1 $r{cer_approx} = 1;
212 }
213 12 ▶ my @pairs = sort { $b->[1] <=> $a->[1] || $a->[0] cmp $b->[0] } map { [$_, $pairs{$_}] } keys %pairs;
Condition33%1/3
$Lingua::RU::WER::b->[1] <=> $Lingua::RU::WER::a->[1] || $Lingua::RU::WER::a->[0] cmp $Lingua::RU::WER::b->[0]
l!l&&r!l&&!r
010
MC/DC0%0/2
$Lingua::RU::WER::b->[1] <=> $Lingua::RU::WER::a->[1] || $Lingua::RU::WER::a->[0] cmp $Lingua::RU::WER::b->[0]
$Lingua::RU::WER::b->[1] <=> $Lingua::RU::WER::a->[1] $Lingua::RU::WER::a->[0] cmp $Lingua::RU::WER::b->[0]
Truth table33%1/3
A || B
A= $Lingua::RU::WER::b->[1] <=> $Lingua::RU::WER::a->[1]B= $Lingua::RU::WER::a->[0] cmp $Lingua::RU::WER::b->[0]
ABresult
1X1
011
000
214 12 ▶ splice @pairs, 10 if @pairs > 10;
Branch: 1 branch
BranchTrueFalseTotal
if @pairs > 10 0 12 12
215 12 $r{pairs} = \@pairs;
216 12 return \%r;
217 }
218  
219 10 ▶sub percent {
Subroutine percent: called CC=2 SCAR=6.9
Pod documented
220 10 my ($v) = @_;
221 10 ▶ return '—' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 9 10
222 9 my $x = POSIX::floor($v * 1000 + 0.5) / 10;
223 9 (my $s = "$x") =~ tr/./,/;
224 9 return "$s %";
225 }
226  
227 # «из 1 слова», «из 2 слов», «из 21 слова»: after «из» the noun is genitive.
228 4 ▶sub _words {
Subroutine _words: called CC=3 SCAR=11.1
229 4 my ($n) = @_;
230 4 ▶ return $n % 10 == 1 && $n % 100 != 11 ? 'слова' : 'слов';
Branch: 1 branch
BranchTrueFalseTotal
$n % 10 == 1 && $n % 100 != 11 ? : 1 3 4
Condition66%2/3
$n % 10 == 1 and $n % 100 != 11
!ll&&!rl&&r
301
MC/DC50%1/2
$n % 10 == 1 and $n % 100 != 11
$n % 10 == 1 $n % 100 != 11
Truth table66%2/3
A and B
A= $n % 10 == 1B= $n % 100 != 11
ABresult
0X0
100
111
231 }
232  
233 5 ▶sub report {
Subroutine report: called CC=3 SCAR=11.0
Pod documented
234 5 my ($r) = @_;
235 5 ▶ return '' unless defined $r->{wer};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{"wer"} 1 4 5
236 4 my $err = $r->{s} + $r->{d} + $r->{i};
237 return 'WER ' . percent($r->{wer}) . " ($err из $r->{n} " . _words($r->{n}) . ' эталона: замен ' . $r->{s}
238 . ', пропусков ' . $r->{d} . ', лишних слов ' . $r->{i} . '). CER ' . ($r->{cer_approx} ? '≈' : '')
239 4 ▶ . percent($r->{cer}) . '.';
Branch: 1 branch
BranchTrueFalseTotal
$r->{'cer_approx'} ? : 0 4 4
240 }
241  
242 4 1;
243  
244 __END__
↑ Summary bin/wer-cer-ru »