blib/lib/Langertha/Holmgang/Sim/Uebersetzung.pm

filter: Statementstmt 100% 120 / 120 Branchbran 95.2% 40 / 42 Conditioncond 57.8% 22 / 38 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 100% 20 / 20 Podpod 100% 9 / 9 totaltotal 86.7% 216 / 249 CC 40 SCAR 16.0
CC
40
Cov
91%
numbers
9.0
_t_uebersetzung_abgeben
7.1
_t_mehrdeutig_melden
6.2
CRAP 5.0
« blib/lib/Langertha/Holmgang.pm ↑ Summary blib/lib/Langertha/Holmgang/Random.pm »
1 package Langertha::Holmgang::Sim::Uebersetzung;
2 # ABSTRACT: The answer world uebersetzung — a translation job: hand in segments, report ambiguity, look up the glossary
3  
4 38 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 38 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 38 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 38 ▶use Unicode::Normalize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 38 with 'Langertha::Holmgang::Role::AnswerWorld';
11  
12 38 our $VERSION = '0.001';
13  
14  
15 # Die Werkzeuge dieser Welt mit ihren Pflichtparametern (fuer den Lader).
16 38 our %TOOLS = (
17 uebersetzung_abgeben => [qw( segment_id text )],
18 mehrdeutig_melden => [qw( segment_id lesarten frage )],
19 glossar_nachschlagen => ['begriff'],
20 );
21  
22 #### Plugin contract
23  
24 78 ▶sub device_type { 'uebersetzung' }
Subroutine device_type: called CC=1 SCAR=0.0
Pod documented
25  
26  
27 2 ▶sub tools { return \%TOOLS }
Subroutine tools: called CC=1 SCAR=0.0
Pod documented
28  
29  
30 17 ▶sub tool {
Subroutine tool: called CC=5 SCAR=16.7
Pod documented
31 17 my ( $self, $dev, $name, $args, $ctx ) = @_;
32 17 ▶ $name //= '';
Condition50%1/2
$name //= ""
l!l
170
MC/DC0%0/1
$name //= ""
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
33 17 ▶ my $code = $TOOLS{$name} && $self->can( '_t_'.$name )
Branch: 1 branch
BranchTrueFalseTotal
my $code = $Langertha::Holmgang::Sim::Uebersetzung::TOOLS{$name} && $self->can('_t_' . $name) or return q[Fehler: unbekanntes Werkzeug '] . $name . q['], 0 16 1 17
Condition66%2/3
$Langertha::Holmgang::Sim::Uebersetzung::TOOLS{$name} && $self->can('_t_' . $name)
!ll&&!rl&&r
1016
MC/DC50%1/2
$Langertha::Holmgang::Sim::Uebersetzung::TOOLS{$name} && $self->can('_t_' . $name)
$Langertha::Holmgang::Sim::Uebersetzung::TOOLS{$name} $self->can('_t_' . $name)
Truth table66%2/3
A && B
A= $Langertha::Holmgang::Sim::Uebersetzung::TOOLS{$name}B= $self->can('_t_' . $name)
ABresult
0X0
100
111
34 or return ( "Fehler: unbekanntes Werkzeug '".$name."'", 0 );
35 16 ▶ return $self->$code( $dev, $args || {} );
Condition50%1/2
$args || {}
l!l
160
MC/DC0%0/1
$args || {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
36 }
37  
38  
39 #### Textanalyse (reine Funktionen)
40  
41 74 ▶sub normalize {
Subroutine normalize: called CC=2 SCAR=6.9
Pod documented
42 74 my ( $s ) = @_;
43 74 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 1 73 74
44 73 $s = Unicode::Normalize::NFC("$s");
45 73 $s =~ tr/\x{201E}\x{201C}\x{201D}\x{00AB}\x{00BB}\x{201F}\x{2033}/"/;
46 73 $s =~ tr/\x{201A}\x{2018}\x{2019}\x{2039}\x{203A}\x{201B}\x{2032}/'/;
47 73 $s =~ s/[\x{00A0}\x{202F}\x{2007}\x{2009}\x{200A}\x{2002}\x{2003}]/ /g;
48 73 $s =~ s/[\x{200B}\x{FEFF}]//g;
49 73 $s =~ s/\s+/ /g;
50 73 $s =~ s/\A | \z//g;
51 73 return $s;
52 }
53  
54  
55 11 ▶sub codes { my ( $s ) = @_; return ( $s // '' ) =~ /`([^`]+)`/g }
Subroutine codes: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$s // ""
l!l
110
MC/DC0%0/1
$s // ""
$s
Truth table50%1/2
A
A= $s
Aresult
11
00
56  
57  
58 38 my $PLACEHOLDER = qr/\{\{\s*[A-Za-z_][\w.]*\s*\}\}|\{[A-Za-z_][\w.]*\}|%(?:\d+\$)?[sdif]|<\/?[A-Za-z][A-Za-z0-9]*(?:\s[^<>]*)?\/?>/;
59  
60 31 ▶sub _no_codes { my ( $s ) = @_; ( my $t = $s // '' ) =~ s/`[^`]*`/ /g; return $t }
Subroutine _no_codes: called CC=2 SCAR=7.1
Condition50%1/2
$s // ""
l!l
310
MC/DC0%0/1
$s // ""
$s
Truth table50%1/2
A
A= $s
Aresult
11
00
61  
62 11 ▶sub placeholders { my ( $s ) = @_; return _no_codes($s) =~ /($PLACEHOLDER)/g }
Subroutine placeholders: called CC=1 SCAR=0.0
Pod documented
63  
64  
65 20 ▶sub numbers {
Subroutine numbers: called CC=9 SCAR=22.0
Pod documented
66 20 my ( $s, $lang ) = @_;
67 20 ( my $t = _no_codes($s) ) =~ s/$PLACEHOLDER/ /g;
68 20 my @out;
69 20 ▶ while ( $t =~ /(?<![\p{L}\d_])(\d+(?:[.,]\d+)*)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /(?<![\p{L}\d_])(\d+(?:[.,]\d+)*)/g) 28 20 48
70 28 my $tok = $1;
71 28 my $v;
72 28 ▶ if ( ( $lang // 'de' ) eq 'en' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($lang // 'de') eq 'en') { } 10 18 28
Condition100%2/2
$lang // "de"
l!l
262
MC/DC100%1/1
$lang // "de"
$lang
Truth table100%2/2
A
A= $lang
Aresult
11
00
73 10 ▶ if ( $tok =~ /\A\d{1,3}(?:,\d{3})+(?:\.\d+)?\z/ ) { ( $v = $tok ) =~ tr/,//d }
Branch: 2 branches
BranchTrueFalseTotal
if ($tok =~ /\A\d{1,3}(?:,\d{3})+(?:\.\d+)?\z/) { } 3 7 10
elsif ($tok =~ /\A\d+(?:\.\d+)?\z/) { } 5 2 7
74 5 elsif ( $tok =~ /\A\d+(?:\.\d+)?\z/ ) { $v = $tok }
75 }
76 else {
77 18 ▶ if ( $tok =~ /\A\d{1,3}(?:\.\d{3})+(?:,\d+)?\z/ ) { ( $v = $tok ) =~ tr/.//d; $v =~ tr/,/./ }
Branch: 2 branches
BranchTrueFalseTotal
if ($tok =~ /\A\d{1,3}(?:\.\d{3})+(?:,\d+)?\z/) { } 4 14 18
elsif ($tok =~ /\A\d+(?:,\d+)?\z/) { } 11 3 14
78 11 elsif ( $tok =~ /\A\d+(?:,\d+)?\z/ ) { ( $v = $tok ) =~ tr/,/./ }
79 }
80 28 ▶ push @out, defined $v ? _value($v) : $tok;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 23 5 28
81 }
82 20 return @out;
83 }
84  
85  
86 23 ▶sub _value {
Subroutine _value: called CC=1 SCAR=0.0
87 23 my ( $v ) = @_;
88 23 $v =~ s/\A0+(?=\d)//;
89 23 $v =~ s/(\.\d*?)0+\z/$1/;
90 23 $v =~ s/\.\z//;
91 23 return $v;
92 }
93  
94 # Was aus @want (mit Vielfachheit) in @have fehlt.
95 15 ▶sub _missing {
Subroutine _missing: called CC=4 SCAR=13.9
96 15 my ( $want, $have ) = @_;
97 15 my %n;
98 15 $n{$_}++ for @$have;
99 15 my @miss;
100 15 ▶ for (@$want) { if ( $n{$_} ) { $n{$_}-- } else { push @miss, $_ } }
Branch: 1 branch
BranchTrueFalseTotal
if ($n{$_}) { } 6 3 9
101 15 return \@miss;
102 }
103  
104 12 ▶sub length_class {
Subroutine length_class: called CC=3 SCAR=11.0
Pod documented
105 12 my ( $src, $txt ) = @_;
106 12 my ( $n, $m ) = map { length normalize($_) } $src, $txt;
107 12 ▶ return 'kurz' if $m < 0.5 * $n - 5;
Branch: 1 branch
BranchTrueFalseTotal
if $m < "0.5" * $n - 5 1 11 12
108 11 ▶ return 'lang' if $m > 2 * $n + 10;
Branch: 1 branch
BranchTrueFalseTotal
if $m > 2 * $n + 10 1 10 11
109 10 return 'ok';
110 }
111  
112  
113 5 ▶sub analyse {
Subroutine analyse: called CC=1 SCAR=0.0
Pod documented
114 5 my ( $src, $text, $from, $to ) = @_;
115 5 my $norm = normalize($text);
116 5 my $snorm = normalize($src);
117 5 my @z = numbers( $norm, $to );
118 5 my @c = codes($norm);
119 5 my @p = placeholders($norm);
120 return {
121 5 text => "$text",
122 norm => $norm,
123 zahlen => \@z,
124 codes => \@c,
125 platzhalter => \@p,
126 fehlende_zahlen => _missing( [ numbers( $snorm, $from ) ], \@z ),
127 fehlende_codes => _missing( [ codes($snorm) ], \@c ),
128 fehlende_platzhalter => _missing( [ placeholders($snorm) ], \@p ),
129 laenge => length_class( $snorm, $norm ),
130 };
131 }
132  
133  
134 #### Werkzeuge
135  
136 11 ▶sub _segment {
Subroutine _segment: called CC=5 SCAR=16.8
137 11 my ( $self, $dev, $id ) = @_;
138 11 ▶ my $seg = $dev->{segmente} || {};
Condition50%1/2
$$dev{'segmente'} || {}
l!l
110
MC/DC0%0/1
$$dev{'segmente'} || {}
$$dev{'segmente'}
Truth table50%1/2
A
A= $$dev{'segmente'}
Aresult
11
00
139 11 ▶ $id = '' unless defined $id && !ref $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and not ref $id 0 11 11
Condition33%1/3
defined $id and not ref $id
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $id and not ref $id
defined $id not ref $id
Truth table33%1/3
A and not(B)
A= defined $idB= not ref $id
ABresult
0X0
100
111
140 11 ( my $k = $id ) =~ s/\A\s+|\s+\z//g;
141 11 $k =~ s/\A\[(.*)\]\z/$1/;
142 11 ▶ return ( $k, undef ) if exists $seg->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$seg{$k} 10 1 11
143 1 return ( undef, "Fehler: Segment '$id' gibt es in diesem Auftrag nicht (".join( ', ', sort keys %$seg ).').' );
144 }
145  
146 8 ▶sub _t_uebersetzung_abgeben {
Subroutine _t_uebersetzung_abgeben: called CC=7 SCAR=19.7
147 8 my ( $self, $dev, $p ) = @_;
148 8 my ( $id, $err ) = $self->_segment( $dev, $p->{segment_id} );
149 8 ▶ return ( $err, 0 ) unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 7 8
150 7 my $text = $p->{text};
151 7 ▶ return ( 'Fehler: text ist leer.', 0 ) unless defined $text && !ref $text && length normalize($text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text and length normalize($text) 2 5 7
Condition83%5/6
defined $text and not ref $text and length normalize($text)
!ll&&!rl&&r
115
defined $text and not ref $text
!ll&&!rl&&r
016
MC/DC66%2/3
defined $text and not ref $text and length normalize($text)
defined $text not ref $text length normalize($text)
Truth table75%3/4
A and not(B) and C
A= defined $textB= not ref $textC= length normalize($text)
ABCresult
0XX0
10X0
1100
1111
152 5 ▶ $dev->{abgaben}{$id} = analyse( $dev->{segmente}{$id}, $text, $dev->{quelle} // 'de', $dev->{ziel} // 'en' );
Condition50%2/4
$$dev{"quelle"} // "de"
l!l
50
$$dev{"ziel"} // "en"
l!l
50
MC/DC0%0/1
$$dev{"quelle"} // "de"
$$dev{"quelle"}
MC/DC0%0/1
$$dev{"ziel"} // "en"
$$dev{"ziel"}
Truth table50%1/2
A
A= $$dev{"quelle"}
Aresult
11
00
Truth table50%1/2
A
A= $$dev{"ziel"}
Aresult
11
00
153 5 return ( "Segment $id: angenommen.", 1 );
154 }
155  
156 3 ▶sub _t_mehrdeutig_melden {
Subroutine _t_mehrdeutig_melden: called CC=6 SCAR=18.3
157 3 my ( $self, $dev, $p ) = @_;
158 3 my ( $id, $err ) = $self->_segment( $dev, $p->{segment_id} );
159 3 ▶ return ( $err, 0 ) unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 3 3
160 3 ▶ my @l = grep { defined && !ref && length normalize($_) } @{ ref $p->{lesarten} eq 'ARRAY' ? $p->{lesarten} : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $$p{'lesarten'} eq 'ARRAY' ? : 2 1 3
Condition50%3/6
defined $_ && !ref($_) && length normalize($_)
!ll&&!rl&&r
013
defined $_ and not ref $_
!ll&&!rl&&r
004
MC/DC33%1/3
defined $_ && !ref($_) && length normalize($_)
defined $_ not ref $_ length normalize($_)
Truth table50%2/4
A and not(B) && C
A= defined $_B= not ref $_C= length normalize($_)
ABCresult
0XX0
10X0
1100
1111
161 3 ▶ return ( 'Fehler: mindestens zwei Lesarten angeben.', 0 ) unless @l >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @l >= 2 2 1 3
162 1 $dev->{meldungen}{$id} = { lesarten => [ map { normalize($_) } @l ], frage => normalize( $p->{frage} ) };
163 1 return ( "Meldung zu Segment $id: angenommen.", 1 );
164 }
165  
166 5 ▶sub _t_glossar_nachschlagen {
Subroutine _t_glossar_nachschlagen: called CC=5 SCAR=16.1
167 5 my ( $self, $dev, $p ) = @_;
168 5 my $q = lc normalize( $p->{begriff} );
169 5 ▶ return ( 'Fehler: begriff ist leer.', 0 ) unless length $q;
Branch: 1 branch
BranchTrueFalseTotal
unless length $q 1 4 5
170 4 push @{ $dev->{_nachschlagen} }, $q;
171 my @hit = grep {
172 8 my $e = $_;
173 8 ▶ grep { index( lc normalize( $e->{$_} // '' ), $q ) >= 0 } qw( de en );
Condition50%1/2
$$e{$_} // ""
l!l
160
MC/DC0%0/1
$$e{$_} // ""
$$e{$_}
Truth table50%1/2
A
A= $$e{$_}
Aresult
11
00
174 4 ▶ } @{ $dev->{glossar} || [] };
Condition50%1/2
$$dev{'glossar'} || []
l!l
40
MC/DC0%0/1
$$dev{'glossar'} || []
$$dev{'glossar'}
Truth table50%1/2
A
A= $$dev{'glossar'}
Aresult
11
00
175 4 ▶ return ( "Kein Eintrag für \"$q\".", 1 ) unless @hit;
Branch: 1 branch
BranchTrueFalseTotal
unless @hit 1 3 4
176 3 return ( join( "\n", map { "de: $_->{de} | en: $_->{en}" } @hit ), 1 );
177 }
178  
179 38 __PACKAGE__->meta->make_immutable;
180  
181 __END__
« blib/lib/Langertha/Holmgang.pm ↑ Summary blib/lib/Langertha/Holmgang/Random.pm »