| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | for (@lines) { $_ = '' if /^\s*[0-9]{1,5}\s*$/ } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$l =~ /$LABEL_TC/ or $l =~ /$LABEL/
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/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
$count{lc $name} // 0
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}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 37 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | 37 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 37 | for my $l (@lines) { | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 43 | ▶ | if ($l =~ $LABEL_TC) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $name = $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | push @out, $is_label->($name) ? '' : $l; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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 //= {}
MC/DC100%1/1
$o //= {}
$o
Truth table100%2/2
A
A= $o
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 23 | ▶ | croak 'options must be a hash reference' unless ref $o eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 23 | ▶ | return { map { $_ => (defined $o->{$_} ? ($o->{$_} ? 1 : 0) : $DEFAULTS{$_}) } keys %DEFAULTS }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 37 | $t =~ s/\r\n?/\n/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 37 | ▶ | $t = strip_markup($t) if $o->{markup}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 37 | ▶ | $t =~ s/$NOTES/ /g if $o->{notes}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 37 | ▶ | $t =~ tr/ёЁ/еЕ/ if $o->{yo}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 37 | ▶ | $t = lc $t if $o->{lower}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 37 | $t =~ s/[’ʼ`]/'/g; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 37 | ▶ | if ($o->{punct}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 37 | my @words = grep { length } split /\s+/, $t; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 37 | ▶ | @words = grep { lc($_) !~ $HES } @words if $o->{hes}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$d <= $u and $d <= $l
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$i > 0 or $j > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 60 | my $k = vec($bt, $i * $w + $j, 8); | |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 60 | ▶ | if ($i > 0 && $j > 0 && $k == 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition83%5/6
$i > 0 and $j > 0 and $k == 0
$i > 0 and $j > 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 55 | ▶ | push @ops, [$ref->[$i - 1] eq $hyp->[$j - 1] ? '=' : 's', $ref->[$i - 1], $hyp->[$j - 1]]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
$k == 1 or $j == 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 13 | ▶ | return $n unless $m; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 13 | my $ops = align(\@ref, \@hyp); | |||||||||||||||||||||||||||||||||||||||||||||
| 184 | 13 | ▶ | unless ($ops) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 12 | ▶ | splice @pairs, 10 if @pairs > 10; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$n % 10 == 1 and $n % 100 != 11
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||
| 244 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||