lib/Serge/Engine/Plugin/transform.pm

filter: Statementstmt 17.8% 27 / 151 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 31.8% 7 / 22 Podpod 0.0% 0 / 10 totaltotal 12.2% 34 / 277 SCAR 74.9
CC
50
Cov
11%
guess_translation
462.0
get_translation
72.0
lazy_initialize
42.0
CRAP 1789.2
« lib/Serge/Util.pm ↑ Summary lib/Serge/Sync/Plugin/Base/VCS.pm »
1 package Serge::Engine::Plugin::transform;
2 1 ▶use parent Serge::Plugin::Base::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode qw(encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Serge::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 my @transforms = ('wrappertag', 'tags', 'whitespace', 'case', 'endpunc'); # ordering is important!
14 1 my @combinations;
15 1 my $plugins = {};
16 1 my $initialized = undef;
17 1 my $mappings = {};
18  
19 sub name {
20 0 ▶ return 'Guess translations from similar already translated strings';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
21 }
22  
23 sub init {
24 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 0 $self->SUPER::init(@_);
27  
28 0 $self->merge_schema({
29 as_fuzzy_default => 'BOOLEAN', # DEPRECATED; kept for backward compatibility for a while
30 as_fuzzy => 'ARRAY', # DEPRECATED; kept for backward compatibility for a while
31 as_not_fuzzy => 'ARRAY' # DEPRECATED; kept for backward compatibility for a while
32 });
33  
34 0 $self->{keys} = {};
35  
36 0 $self->add('get_translation', \&get_translation);
37 }
38  
39 sub validate_data {
40 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=2 SCAR=17.9
Pod undocumented
41  
42 0 $self->SUPER::validate_data;
43  
44 map {
45 0 ▶ if (exists $self->{data}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"data"}{$_}) 0 0 0
46 0 warn "NOTICE: $_ parameter is deprecated; please remove it from the config";
47 };
48 } qw(
49 as_fuzzy_default
50 as_fuzzy
51 as_not_fuzzy
52 );
53 }
54  
55 sub adjust_phases {
56 0 ▶ my ($self, $phases) = @_;
Subroutine adjust_phases: not called CC=1 SCAR=6.9
Pod undocumented
57  
58 # always tie to 'get_translation' phase
59 0 set_flag($phases, 'get_translation');
60 }
61  
62 sub _get_combinations {
63 0 ▶ my ($base) = @_;
Subroutine _get_combinations: not called CC=3 SCAR=24.8
64  
65 0 my %result;
66  
67 0 for (0 .. $base**$base-1) {
68 0 $result{_encode_unique_digits($_, $base)} = 1;
69 }
70  
71 # sort by string length and by number
72 return sort {
73 0 my $result = length($a) <=> length($b);
74 0 ▶ return $result == 0 ? $a cmp $b : $result
Branch: 1 branch
BranchTrueFalseTotal
$result == 0 ? : 0 0 0
75 } keys %result;
76 }
77  
78 sub _encode_unique_digits {
79 0 ▶ my ($num, $base) = @_;
Subroutine _encode_unique_digits: not called CC=4 SCAR=30.0
80 0 my $out = '';
81 0 my %digits;
82  
83 0 ▶ return 0 if $num == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num == 0 0 0 0
84  
85 0 ▶ while ($num > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num > 0) 0 0 0
86 0 my $digit = $num % $base;
87 0 $num = int($num / $base);
88 0 ▶ if (!exists $digits{$digit}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $digits{$digit}) 0 0 0
89 0 $out = $digit . $out;
90 0 $digits{$digit} = 1;
91 }
92 }
93  
94 0 return $out;
95 }
96  
97 sub _load_plugins {
98 0 ▶ print "Loading string transformation plugins\n";
Subroutine _load_plugins: not called CC=3 SCAR=24.8
99  
100 0 foreach my $plugin (@transforms) {
101 0 eval(qq|use Serge::Engine::Plugin::Transform::$plugin; \$plugins->{$plugin} = Serge::Engine::Plugin::Transform::$plugin|.qq|->new();|);
102 0 ▶ ($@) && die "Can't load callback plugin [Serge::Engine::Plugin::Transform::$plugin]: $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "Can't load callback plugin [Serge::Engine::Plugin::Transform::$plugin]: $@" 0 0 0
103 0 print "\t'$plugin': ".$plugins->{$plugin}->name."\n";
104 }
105 }
106  
107 sub _filter_string {
108 0 ▶ my $string = shift;
Subroutine _filter_string: not called CC=2 SCAR=17.9
109 0 foreach my $plugin (@transforms) {
110 0 $string = $plugins->{$plugin}->filter_key($string);
111 }
112 0 return $string;
113 }
114  
115 sub _make_key {
116 0 ▶ my ($string) = @_;
Subroutine _make_key: not called CC=2 SCAR=17.9
117 0 $string = _filter_string($string);
118 0 ▶ return unless ($string ne '');
Branch: 1 branch
BranchTrueFalseTotal
unless $string ne "" 0 0 0
119 0 return md5(encode_utf8($string));
120 }
121  
122 sub make_key {
123 0 ▶ my ($self, $string) = @_;
Subroutine make_key: not called CC=2 SCAR=17.9
Pod undocumented
124 0 my $key = md5(encode_utf8($string)); # original, unfiltered string
125 0 ▶ return $self->{keys}->{$key} if exists $self->{keys}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"keys"}{$key} 0 0 0
126 0 return $self->{keys}->{$key} = _make_key($string);
127 }
128  
129 sub lazy_initialize {
130 0 ▶ my ($self) = @_;
Subroutine lazy_initialize: not called CC=6 SCAR=37.4
Pod undocumented
131  
132 0 ▶ return if $initialized;
Branch: 1 branch
BranchTrueFalseTotal
if $initialized 0 0 0
133  
134 0 _load_plugins;
135  
136 0 print "Calculating string transformation plugin combinations\n";
137  
138 0 @combinations = _get_combinations(scalar @transforms);
139  
140 0 print "Preloading cache for string transformations\n";
141  
142 0 my $start = [gettimeofday];
143  
144 0 my $sqlquery =
145 "SELECT DISTINCT string ".
146 "FROM strings ".
147 "WHERE skip = 0";
148 0 my $sth = $self->{parent}->{engine}->{db}->prepare($sqlquery);
149 0 ▶ $sth->execute || die $sth->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or die $sth->errstr 0 0 0
150  
151 0 ▶ while (my $ar = $sth->fetchrow_arrayref()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ar = $sth->fetchrow_arrayref) 0 0 0
152 0 my $key = $self->make_key($ar->[0]);
153 0 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
154 0 my $h = $mappings->{$key};
155 0 ▶ $h = $mappings->{$key} = {} unless $h;
Branch: 1 branch
BranchTrueFalseTotal
unless $h 0 0 0
156 0 $h->{$ar->[0]} = 1;
157 }
158 }
159  
160 0 $initialized = 1;
161  
162 0 print "Cache preloaded in ", tv_interval($start), " seconds\n";
163 }
164  
165 sub get_translation {
166 0 ▶ my ($self, $phase, $string, $context, $namespace, $filepath, $lang, $disallow_similar_lang, $item_id) = @_;
Subroutine get_translation: not called CC=8 SCAR=42.8
Pod undocumented
167  
168 0 $self->lazy_initialize;
169  
170 0 my $key = $self->make_key($string);
171 # if key is not defined (the string was empty after filtering), then
172 # simply return the original string
173 0 ▶ if (!$key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 0 0
174 # return: (translation, fuzzy_state, comment, db_save_flag)
175 0 return ($string, undef, undef, 1); # return as NOT fuzzy, save to database
176 }
177  
178 0 ▶ if (defined $key && exists $mappings->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and exists $mappings->{$key}) { } 0 0 0
Condition0%0/3
defined $key and exists $mappings->{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and exists $mappings->{$key}
defined $key exists $mappings->{$key}
Truth table0%0/3
A and B
A= defined $keyB= exists $mappings->{$key}
ABresult
0X0
100
111
179 0 my $guessed_translation = $self->guess_translation($string, $context, $namespace, $filepath, $lang);
180 0 ▶ if (defined $guessed_translation) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $guessed_translation) { } 0 0 0
181 0 ▶ print "Guessed translation: '$guessed_translation'\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
182 # guessed translations are always returned as fuzzy
183 0 return ($guessed_translation, 1, undef, 1); # fuzzy; empty comment; save to database
184 } else {
185 0 ▶ print "Failed to guess translation\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
186 }
187 } else {
188 0 ▶ print "No similar strings for string '$string'\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
189 }
190  
191 0 return (); # return empty array, not an 'undef' value and neither the original string
192 }
193  
194 sub find_solution {
195 0 ▶ my ($self, $a, $b) = @_;
Subroutine find_solution: not called CC=5 SCAR=34.0
Pod undocumented
196 0 ▶ print "Finding solution to transform '$a' into '$b'\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
197  
198 0 foreach my $sequence (@combinations) {
199 0 my $result = $self->apply_solution($a, $b, $sequence);
200  
201 0 ▶ if ($result && ($result eq $b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result and $result eq $b) 0 0 0
Condition0%0/3
$result and $result eq $b
!ll&&!rl&&r
000
MC/DC0%0/2
$result and $result eq $b
$result $result eq $b
Truth table0%0/3
A and B
A= $resultB= $result eq $b
ABresult
0X0
100
111
202 0 return $sequence;
203 }
204 }
205 0 return undef;
206 }
207  
208 sub apply_solution {
209 0 ▶ my ($self, $a, $b, $solution, $lang) = @_;
Subroutine apply_solution: not called CC=3 SCAR=24.8
Pod undocumented
210  
211 0 my @c = split('', $solution);
212 0 my $s = $a;
213  
214 0 my $ok = 1;
215 0 foreach my $n (@c) {
216 0 my $name = $transforms[$n];
217 0 $s = $plugins->{$name}->transform($s, $b, $lang);
218 }
219 0 ▶ return $ok ? $s : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
220 }
221  
222 sub guess_translation {
223 0 ▶ my ($self, $source, $context, $namespace, $filepath, $lang) = @_;
Subroutine guess_translation: not called CC=21 SCAR=61.4
Pod undocumented
224  
225 0 my $key = $self->make_key($source);
226  
227 0 my $candidates = $mappings->{$key};
228 # filter out candidates that have no translations (or have multiple translations and reuse_uncertain is disabled)
229 0 foreach my $candidate (keys %$candidates) {
230 my ($translation, $fuzzy, $comment, $multiple_variants) =
231 $self->{parent}->{engine}->{db}->find_best_translation(
232 $namespace, $filepath, $candidate, $context, $lang,
233 $self->{parent}->{reuse_orphaned},
234 $self->{parent}->{reuse_fuzzy},
235 $self->{parent}->{reuse_uncertain}
236 0 );
237  
238 0 ▶ if ($multiple_variants && !$self->{parent}->{reuse_uncertain}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiple_variants and not $self->{"parent"}{"reuse_uncertain"}) 0 0 0
Condition0%0/3
$multiple_variants and not $self->{"parent"}{"reuse_uncertain"}
!ll&&!rl&&r
000
MC/DC0%0/2
$multiple_variants and not $self->{"parent"}{"reuse_uncertain"}
$multiple_variants not $self->{"parent"}{"reuse_uncertain"}
Truth table0%0/3
A and not(B)
A= $multiple_variantsB= not $self->{"parent"}{"reuse_uncertain"}
ABresult
0X0
100
111
239 0 ▶ print "Multiple translations found, will skip the candidate because 'reuse_uncertain' mode is set to NO\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
240 }
241  
242 0 ▶ if ($translation eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($translation eq "") 0 0 0
243 0 delete $candidates->{$candidate};
244 }
245 }
246  
247 0 ▶ if (keys %$candidates > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$candidates > 0) { } 0 0 0
248 0 ▶ if ($self->{parent}->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"parent"}{"debug"}) 0 0 0
249 0 print "Trying to guess translation for string '$source'; candidates are:\n";
250 0 foreach my $candidate (keys %$candidates) {
251 0 ▶ print "\t* '".$candidate."'\n" if $candidate ne $source;
Branch: 1 branch
BranchTrueFalseTotal
if $candidate ne $source 0 0 0
252 }
253 }
254  
255 0 foreach my $candidate (keys %$candidates) {
256 0 ▶ next if ($candidate eq $source); # exact matches should be substituted outside this code
Branch: 1 branch
BranchTrueFalseTotal
if $candidate eq $source 0 0 0
257  
258 0 my $solution = $self->find_solution($candidate, $source);
259 0 ▶ if (defined $solution) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $solution) 0 0 0
260 0 my $solution_text = join(' -> ', map { $transforms[$_] } split(//, $solution));
261 0 ▶ print "\nSolution: '$candidate' -> $solution_text\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
262  
263 my ($translation, $fuzzy, $comment, $multiple_variants) =
264 $self->{parent}->{engine}->{db}->find_best_translation(
265 $namespace, $filepath, $candidate, $context, $lang,
266 $self->{parent}->{reuse_orphaned},
267 $self->{parent}->{reuse_fuzzy},
268 $self->{parent}->{reuse_uncertain}
269 0 );
270  
271 0 ▶ if ($multiple_variants && !$self->{parent}->{reuse_uncertain}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($multiple_variants and not $self->{"parent"}{"reuse_uncertain"}) 0 0 0
Condition0%0/3
$multiple_variants and not $self->{"parent"}{"reuse_uncertain"}
!ll&&!rl&&r
000
MC/DC0%0/2
$multiple_variants and not $self->{"parent"}{"reuse_uncertain"}
$multiple_variants not $self->{"parent"}{"reuse_uncertain"}
Truth table0%0/3
A and not(B)
A= $multiple_variantsB= not $self->{"parent"}{"reuse_uncertain"}
ABresult
0X0
100
111
272 0 ▶ print "Multiple translations found, won't reuse any because 'reuse_uncertain' mode is set to NO\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
273 }
274  
275 0 ▶ if ($translation ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($translation ne '') { } 0 0 0
276 0 ▶ print "Applying this solution to translation '$translation'\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
277 0 return $self->apply_solution($translation, $source, $solution, $lang);
278 } else {
279 0 ▶ print "There's no translation for the candidate string\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
280 }
281 }
282 }
283 } else {
284 0 ▶ print "There are no candidates to guess from\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
285 }
286  
287 0 return undef;
288 }
289  
290 1 1;
« lib/Serge/Util.pm ↑ Summary lib/Serge/Sync/Plugin/Base/VCS.pm »