lib/Geo/Address/Formatter.pm

filter: Statementstmt 87.8% 537 / 611 Branchbran 76.7% 215 / 280 Conditioncond 71.9% 59 / 82 MC/DCmcdc 41.8% 18 / 43 Subroutinesub 89.1% 41 / 46 Podpod 100% 4 / 4 totaltotal 81.9% 874 / 1066 CC 208 SCAR 31.6
CC
208
Cov
83%
format_address
79.6
_apply_replacements
25.2
_determine_country_code
19.3
CRAP 23.6
↑ Summary
1 package Geo::Address::Formatter;
2 7 $Geo::Address::Formatter::VERSION = '1.9994';
3 # ABSTRACT: take structured address data and format it according to the various global/country rules
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 $Data::Dumper::Sortkeys = 1;
10 7 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use Ref::Util qw(is_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Text::Hogan::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Try::Catch;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use YAML::XS qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 my $THC = Text::Hogan::Compiler->new;
19  
20 # optional params
21 7 my $show_warnings = 1;
22 7 my $debug = 0;
23 7 my $only_address = 0;
24  
25  
26  
27  
28 7 my $instance;
29  
30 0 ▶sub instance {
Subroutine instance: not called CC=3 SCAR=24.8
Pod documented
31 0 my ($class, %params) = @_;
32
33 0 ▶ unless ($instance) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($instance) 0 0 0
34 0 $instance = $class->new(%params);
35 }
36  
37 0 ▶ say STDERR "************* in Geo::Address::Formatter::instance ***" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
38
39 # clear these fields that change on each run
40 0 $instance->{final_components} = undef;
41 0 $instance->{set_district_alias} = {};
42  
43 0 return $instance;
44 }
45  
46 20 ▶sub new {
Subroutine new: called CC=10 SCAR=23.8
Pod documented
47 20 my ($class, %params) = @_;
48  
49 20 my $self = {};
50 20 ▶ my $conf_path = $params{conf_path} || die "no conf_path set";
Condition50%1/2
$params{'conf_path'} || die('no conf_path set')
l!l
200
MC/DC0%0/1
$params{'conf_path'} || die('no conf_path set')
$params{'conf_path'}
Truth table50%1/2
A
A= $params{'conf_path'}
Aresult
11
00
51  
52 # optional params
53 20 ▶ if ( defined($params{no_warnings}) && ($params{no_warnings})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"no_warnings"} and $params{"no_warnings"}) 1 19 20
Condition66%2/3
defined $params{"no_warnings"} and $params{"no_warnings"}
!ll&&!rl&&r
1901
MC/DC50%1/2
defined $params{"no_warnings"} and $params{"no_warnings"}
defined $params{"no_warnings"} $params{"no_warnings"}
Truth table66%2/3
A and B
A= defined $params{"no_warnings"}B= $params{"no_warnings"}
ABresult
0X0
100
111
54 1 $show_warnings = 0;
55 }
56 20 ▶ $only_address = (defined($params{only_address}) && $params{only_address}) // 0;
Condition60%3/5
defined $params{"only_address"} && $params{"only_address"} // 0
l!l
200
defined $params{"only_address"} and $params{"only_address"}
!ll&&!rl&&r
1802
MC/DC50%1/2
defined $params{"only_address"} && $params{"only_address"} // 0
defined $params{"only_address"} $params{"only_address"}
Truth table66%2/3
A and B
A= defined $params{"only_address"}B= $params{"only_address"}
ABresult
111
0X0
100
57 20 ▶ $debug = (defined($params{debug}) && $params{debug}) // 0;
Condition60%3/5
defined $params{"debug"} && $params{"debug"} // 0
l!l
200
defined $params{"debug"} and $params{"debug"}
!ll&&!rl&&r
1550
MC/DC0%0/2
defined $params{"debug"} && $params{"debug"} // 0
defined $params{"debug"} $params{"debug"}
Truth table66%2/3
A and B
A= defined $params{"debug"}B= $params{"debug"}
ABresult
111
0X0
100
58  
59 20 $self->{final_components} = undef;
60 20 $self->{set_district_alias} = {};
61  
62 20 bless($self, $class);
63  
64 20 ▶ say STDERR "************* in Geo::Address::Formatter::new ***" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 20 20
65  
66 # is slow because lots of conf, and pre-compiling
67 20 ▶ if ($self->_read_configuration($conf_path)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_read_configuration($conf_path)) 19 1 20
68 19 return $self;
69 }
70 1 die 'unable to read configuration';
71 }
72  
73 20 ▶sub _read_configuration {
Subroutine _read_configuration: called CC=12 SCAR=25.5
74 20 my $self = shift;
75 20 my $path = shift;
76  
77 20 ▶ return if (! -e $path);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 20 20
78 # components file
79 20 my $compyaml = $path . '/components.yaml';
80 20 ▶ return if (! -e $compyaml);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $compyaml 1 19 20
81  
82 19 $self->{templates} = {};
83 19 $self->{component_aliases} = {};
84 19 $self->{component2type} = {};
85 19 $self->{ordered_components} = [];
86  
87 # read the config file(s)
88 19 my $loaded = 0;
89  
90 19 my $wwfile = $path . '/countries/worldwide.yaml';
91 19 ▶ say STDERR "loading templates $wwfile" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 19 19
92  
93 19 ▶ return if (! -e $wwfile);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $wwfile 0 19 19
94  
95 19 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
96 19 my $rh_templates = LoadFile($wwfile);
97 19 foreach (keys %$rh_templates) {
98 3855 $self->{templates}{$_} = $rh_templates->{$_};
99 3855 $loaded = 1;
100 }
101 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 warn "error parsing country configuration in $wwfile";
103 0 return;
104 19 };
105 19 ▶ return if ($loaded == 0); # no templates
Branch: 1 branch
BranchTrueFalseTotal
if $loaded == 0 0 19 19
106  
107 # see if we can load the components
108 19 ▶ try {
Subroutine __ANON__: called CC=10 SCAR=24.1
109 19 ▶ say STDERR "loading components" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 19 19
110 19 my @c = LoadFile($compyaml);
111  
112 19 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 19 19
113 0 say STDERR Dumper \@c;
114 }
115  
116 19 foreach my $rh_c (@c) {
117 304 ▶ if (defined($rh_c->{name})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_c->{"name"}) 304 0 304
118 304 ▶ if (defined($rh_c->{aliases})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_c->{'aliases'}) { } 187 117 304
119 187 $self->{component_aliases}{$rh_c->{name}} = $rh_c->{aliases};
120 } else {
121 117 $self->{component_aliases}{$rh_c->{name}} = [];
122 }
123 }
124 }
125  
126 19 foreach my $rh_c (@c) {
127 304 push(@{$self->{ordered_components}}, $rh_c->{name});
128 304 $self->{component2type}->{$rh_c->{name}} = $rh_c->{name};
129  
130 304 ▶ if (defined($rh_c->{aliases})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_c->{"aliases"}) 187 117 304
131 187 foreach my $alias (@{$rh_c->{aliases}}) {
132 638 push(@{$self->{ordered_components}}, $alias);
133 638 $self->{component2type}->{$alias} = $rh_c->{name};
134 }
135 }
136 }
137 19 my %h_known = map { $_ => 1 } @{$self->{ordered_components}};
138 19 $self->{h_known} = \%h_known;
139  
140 19 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 19 19
141 0 say STDERR 'component_aliases';
142 0 say STDERR Dumper $self->{component_aliases};
143 0 say STDERR 'component2type';
144 0 say STDERR Dumper $self->{component2type};
145 }
146 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 warn "error parsing component configuration: $_";
148 19 };
149  
150 # get the county and state codes and country2lang conf
151 19 my @conf_files = qw(county_codes state_codes country2lang);
152 19 foreach my $cfile (@conf_files) {
153 57 $self->{$cfile} = {};
154 57 my $yfile = $path . '/' . $cfile . '.yaml';
155 57 ▶ if (-e $yfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $yfile) 51 6 57
156 51 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
157 51 $self->{$cfile} = LoadFile($yfile);
158 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
159 0 warn "error parsing $cfile configuration: $_";
160 51 };
161 }
162 }
163  
164 19 my $abbrvdir = $path . '/abbreviations';
165  
166 19 ▶ if (-d $abbrvdir){
Branch: 1 branch
BranchTrueFalseTotal
if (-d $abbrvdir) 15 4 19
167 15 ▶ opendir my $dh, $abbrvdir
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $abbrvdir or die "Could not open '${abbrvdir}' for read: $!\n" 15 0 15
168 or die "Could not open '$abbrvdir' for read: $!\n";
169  
170 15 ▶ while (my $file = readdir $dh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = readdir $dh)) 361 15 376
171 # say STDERR "file: $file";
172 361 ▶ if ($file =~ m/^(\w\w)\.yaml$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /^(\w\w)\.yaml$/) 331 30 361
173 331 my $lang = $1; # two letter lang code like 'en'
174 331 my $abbrvfile = $abbrvdir . '/' . $file;
175 331 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
176 331 $self->{abbreviations}->{$lang} = LoadFile($abbrvfile);
177 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 warn "error parsing abbrv conf in $abbrvfile: $_";
179 331 };
180 }
181 }
182 15 closedir $dh;
183 }
184  
185 #say Dumper $self->{abbreviations};
186 #say Dumper $self->{country2lang};
187  
188 19 $self->_precompile_rules();
189 19 $self->_build_code_lookups();
190 19 $self->_precompile_abbreviations();
191  
192 19 return 1;
193 }
194  
195 19 ▶sub _precompile_rules {
Subroutine _precompile_rules: called CC=11 SCAR=24.4
196 19 my $self = shift;
197  
198 19 foreach my $cc (keys %{$self->{templates}}) {
199 3855 my $tpl = $self->{templates}{$cc};
200 3855 ▶ next unless is_hashref($tpl);
Branch: 1 branch
BranchTrueFalseTotal
unless is_hashref($tpl) 355 3500 3855
201  
202 # Pre-compile replace rules
203 3500 ▶ if (defined($tpl->{replace})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tpl->{"replace"}) 251 3249 3500
204 251 my @compiled;
205 251 foreach my $ra_fromto (@{$tpl->{replace}}) {
206 853 my $pattern = $ra_fromto->[0];
207 853 my $replacement = $ra_fromto->[1];
208 853 my ($comp_name, $exact_match, $re);
209  
210 # detect component-specific rules like "state=Some Value"
211 853 ▶ if ($pattern =~ m/^(\w+)=(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ /^(\w+)=(.+)$/) { } 208 645 853
212 208 $comp_name = $1;
213 208 my $rest = $2;
214 # try to compile as regex; if it fails, it's an exact match
215 208 eval { $re = qr/$rest/i };
216 208 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 208 208
217 0 warn "invalid replacement regex '$rest' for $cc, skipping";
218 0 next;
219 }
220 208 $exact_match = $rest;
221 } else {
222 645 eval { $re = qr/$pattern/i };
223 645 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 645 645
224 0 warn "invalid replacement regex '$pattern' for $cc, skipping";
225 0 next;
226 }
227 }
228 853 push @compiled, {
229 component => $comp_name,
230 exact_match => $exact_match,
231 re => $re,
232 replacement => $replacement,
233 };
234 }
235 251 $tpl->{_compiled_replace} = \@compiled;
236 }
237  
238 # Pre-compile postformat_replace rules
239 3500 ▶ if (defined($tpl->{postformat_replace})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tpl->{"postformat_replace"}) 461 3039 3500
240 461 my @compiled;
241 461 foreach my $ra_fromto (@{$tpl->{postformat_replace}}) {
242 801 my $pattern = $ra_fromto->[0];
243 801 my $replacement = $ra_fromto->[1];
244 801 my $re;
245 801 eval { $re = qr/$pattern/ };
246 801 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 801 801
247 0 warn "invalid postformat regex '$pattern' for $cc, skipping";
248 0 next;
249 }
250 801 push @compiled, {
251 re => $re,
252 replacement => $replacement,
253 };
254 }
255 461 $tpl->{_compiled_postformat} = \@compiled;
256 }
257 }
258 19 return;
259 }
260  
261 19 ▶sub _build_code_lookups {
Subroutine _build_code_lookups: called CC=8 SCAR=20.8
262 19 my $self = shift;
263  
264 19 foreach my $type (qw(state_codes county_codes)) {
265 38 my $reverse_key = $type . '_reverse';
266 38 my $name_key = $type . '_name';
267 38 $self->{$reverse_key} = {};
268 38 $self->{$name_key} = {};
269  
270 38 ▶ my $data = $self->{$type} // next;
Condition50%1/2
$self->{$type} // (next)
l!l
380
MC/DC0%0/1
$self->{$type} // (next)
$self->{$type}
Truth table50%1/2
A
A= $self->{$type}
Aresult
11
00
271 38 foreach my $cc (keys %$data) {
272 1593 my $mapping = $data->{$cc};
273 1593 my $rev = {};
274 1593 my $name = {};
275  
276 1593 foreach my $code (keys %$mapping) {
277 33864 my $val = $mapping->{$code};
278 33864 ▶ if (is_hashref($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hashref($val)) { } 5676 28188 33864
279 # hash with default, alt, alt_XX keys
280 5676 my $default_name = $val->{default};
281 5676 ▶ $name->{$code} = $default_name if defined $default_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default_name 5676 0 5676
282 5676 foreach my $v (values %$val) {
283 12223 $rev->{uc($v)} = $code;
284 }
285 } else {
286 # simple string value
287 28188 $name->{$code} = $val;
288 28188 $rev->{uc($val)} = $code;
289 }
290 # code-to-code identity lookup
291 33864 $rev->{$code} = $code;
292 }
293 1593 $self->{$reverse_key}{$cc} = $rev;
294 1593 $self->{$name_key}{$cc} = $name;
295 }
296 }
297 19 return;
298 }
299  
300 19 ▶sub _precompile_abbreviations {
Subroutine _precompile_abbreviations: called CC=5 SCAR=16.1
301 19 my $self = shift;
302 19 ▶ my $abbr = $self->{abbreviations} // return;
Condition100%2/2
$self->{"abbreviations"} // (return)
l!l
154
MC/DC100%1/1
$self->{"abbreviations"} // (return)
$self->{"abbreviations"}
Truth table100%2/2
A
A= $self->{"abbreviations"}
Aresult
11
00
303  
304 15 foreach my $lang (keys %$abbr) {
305 331 foreach my $comp_name (keys %{$abbr->{$lang}}) {
306 424 my $rh_pairs = $abbr->{$lang}{$comp_name};
307 424 my @compiled;
308 424 foreach my $long (keys %$rh_pairs) {
309 push @compiled, {
310 re => qr/(^|\s)\Q$long\E\b/,
311 5715 short => $rh_pairs->{$long},
312 };
313 }
314 424 $self->{compiled_abbreviations}{$lang}{$comp_name} = \@compiled;
315 }
316 }
317 15 return;
318 }
319  
320  
321 6 ▶sub final_components {
Subroutine final_components: called CC=3 SCAR=11.0
Pod documented
322 6 my $self = shift;
323 6 ▶ if (defined($self->{final_components})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"final_components"}) 5 1 6
324 5 return $self->{final_components};
325 }
326 1 ▶ warn 'final_components not yet set' if ($show_warnings);
Branch: 1 branch
BranchTrueFalseTotal
if $show_warnings 1 0 1
327 1 return;
328 }
329  
330  
331 492 ▶sub format_address {
Subroutine format_address: called CC=46 SCAR=43.8
Pod documented
332 492 my $self = shift;
333 492 ▶ my $rh_input = shift || return;
Condition50%1/2
shift() || (return)
l!l
4920
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
334 492 my $rh_components = { %$rh_input }; # make a copy
335 492 ▶ my $rh_options = shift || {};
Condition100%2/2
shift() || {}
l!l
15477
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
336  
337 # 1. make sure empty at the beginning
338 492 $self->{final_components} = undef;
339  
340 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
341 0 say STDERR "*** in format_address ***";
342 0 say STDERR Dumper $rh_options;
343 0 say STDERR Dumper $rh_components;
344 }
345  
346 # 2. deal with the options
347  
348 492 foreach (grep { ! /^[a-z0-9_]+$/ } keys %$rh_components) {
349 7 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 7 7
350 0 say STDERR "Skipping compontent $_";
351 }
352 7 delete $rh_components->{$_};
353 }
354  
355 # 2a. which country format will we use?
356 # might have been specified in options
357 # otherwise look at components
358 my $cc = $rh_options->{country}
359 492 ▶ || $self->_determine_country_code($rh_components)
Condition100%5/5
$rh_options->{'country'} || $self->_determine_country_code($rh_components) || ''
l!l
4884
$rh_options->{"country"} or $self->_determine_country_code($rh_components)
l!l&&r!l&&!r
14874
MC/DC100%2/2
$rh_options->{'country'} || $self->_determine_country_code($rh_components) || ''
$rh_options->{"country"} $self->_determine_country_code($rh_components)
Truth table100%3/3
A or B
A= $rh_options->{"country"}B= $self->_determine_country_code($rh_components)
ABresult
1X1
011
000
360 || '';
361  
362 492 ▶ if ($cc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc) 488 4 492
363 488 $rh_components->{country_code} = $cc;
364 488 $self->_set_district_alias($cc);
365 }
366  
367 # 2b. should we abbreviate?
368 492 ▶ my $abbrv = $rh_options->{abbreviate} // 0;
Condition100%2/2
$rh_options->{"abbreviate"} // 0
l!l
10482
MC/DC100%1/1
$rh_options->{"abbreviate"} // 0
$rh_options->{"abbreviate"}
Truth table100%2/2
A
A= $rh_options->{"abbreviate"}
Aresult
11
00
369  
370 # 2c. was only_address set at the formatting level
371 492 my $oa = $only_address;
372 492 ▶ if (defined($rh_options->{only_address})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_options->{"only_address"}) 2 490 492
373 2 $oa = $rh_options->{only_address};
374 }
375  
376 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
377 0 say STDERR "component_aliases";
378 0 say STDERR Dumper $self->{component_aliases};
379 }
380 # done with the options
381  
382 # 3. deal wtih terrible inputs
383 492 $self->_sanity_cleaning($rh_components);
384 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
385 0 say STDERR "after sanity_cleaning applied";
386 0 say STDERR Dumper $rh_components;
387 }
388  
389 # 4. set the aliases, unless this would overwrite something
390 # need to do this in the right order (as defined in the components file)
391 # For example:
392 # both 'city_district' and 'suburb' are aliases of 'neighbourhood'
393 # so which one should we use if both are present?
394 # We should use the one defined first in the list
395  
396 492 my $rhh_p2a;
397 492 foreach my $c (keys %$rh_components){
398  
399 # might not need an alias as it is a primary type
400 3833 ▶ next if (defined($self->{component_aliases}{$c}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"component_aliases"}{$c} 3058 775 3833
401  
402 # it is not a primary type
403 # is there an alias?
404 775 ▶ if (defined($self->{component2type}{$c})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"component2type"}{$c}) 536 239 775
405 536 my $ptype = $self->{component2type}{$c};
406 # but is it already set?
407 536 ▶ if (! defined($rh_components->{$ptype}) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rh_components->{$ptype}) 419 117 536
408 # no, we will set it later
409 419 $rhh_p2a->{$ptype}{$c} = 1;
410  
411 }
412 }
413 }
414  
415 # now we know which primary types have aliases
416 492 foreach my $ptype (keys %$rhh_p2a){
417 # is there more than one?
418 378 my @aliases = keys %{$rhh_p2a->{$ptype}};
419 378 ▶ if (scalar @aliases == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @aliases == 1) 338 40 378
420 338 $rh_components->{$ptype} = $rh_components->{$aliases[0]};
421 338 next; # we are done with this ptype
422 }
423  
424 # if there is more than one we need to go through the list
425 # so we do them in the right order
426 40 foreach my $c (@{$self->{component_aliases}->{$ptype}}){
427 45 ▶ if (defined($rh_components->{$c})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{$c}) 40 5 45
428 40 $rh_components->{$ptype} = $rh_components->{$c};
429 40 last; # we are done with this ptype
430 }
431 }
432 }
433  
434 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
435 0 say STDERR "after component_aliases applied";
436 0 say STDERR Dumper $rh_components;
437 }
438  
439 # 5. determine the template
440 492 my $template_text;
441 492 ▶ my $rh_config = $self->{templates}{uc($cc)} || $self->{templates}{default};
Condition66%2/3
$self->{'templates'}{uc $cc} || $self->{'templates'}{'default'}
l!l&&r!l&&!r
48660
MC/DC0%0/2
$self->{'templates'}{uc $cc} || $self->{'templates'}{'default'}
$self->{'templates'}{uc $cc} $self->{'templates'}{'default'}
Truth table66%2/3
A || B
A= $self->{'templates'}{uc $cc}B= $self->{'templates'}{'default'}
ABresult
1X1
011
000
442
443 492 ▶ if (defined($rh_options->{address_template})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_options->{'address_template'}) { } 2 490 492
444 2 $template_text = $rh_options->{address_template};
445 }
446 else {
447  
448 490 ▶ if (defined($rh_config->{address_template})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rh_config->{'address_template'}) { } 488 2 490
elsif (defined $self->{'templates'}{'default'}{'address_template'}) { } 2 0 2
449 488 $template_text = $rh_config->{address_template};
450 } elsif (defined($self->{templates}{default}{address_template})) {
451 2 $template_text = $self->{templates}{default}{address_template};
452 }
453
454 # do we have the minimal components for an address?
455 # or should we instead use the fallback template?
456 490 ▶ if (!$self->_minimal_components($rh_components)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_minimal_components($rh_components)) 57 433 490
457 57 ▶ say STDERR "using fallback" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 57 57
458 57 ▶ if (defined($rh_config->{fallback_template})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rh_config->{'fallback_template'}) { } 42 15 57
elsif (defined $self->{'templates'}{'default'}{'fallback_template'}) { } 14 1 15
459 42 $template_text = $rh_config->{fallback_template};
460 } elsif (defined($self->{templates}{default}{fallback_template})) {
461 14 $template_text = $self->{templates}{default}{fallback_template};
462 }
463 # no fallback
464 }
465  
466 }
467  
468 492 ▶ say STDERR 'template text: ' . $template_text if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 492 492
469  
470 # 6. clean up the components, possibly add codes
471 492 $self->_fix_country($rh_components);
472 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
473 0 say STDERR "after fix_country";
474 0 say STDERR Dumper $rh_components;
475 }
476  
477 # apply replacements if we have any
478 492 ▶ if (defined($rh_config->{_compiled_replace})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_config->{"_compiled_replace"}) 160 332 492
479 160 $self->_apply_replacements($rh_components, $rh_config->{_compiled_replace});
480 160 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 160 160
481 0 say STDERR "after applying_replacements applied";
482 0 say STDERR Dumper $rh_components;
483 }
484 }
485  
486 492 $self->_add_state_code($rh_components);
487 492 $self->_add_county_code($rh_components);
488 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
489 0 say STDERR "after adding codes";
490 0 say STDERR Dumper $rh_components;
491 }
492  
493 # 7. add the attention, if needed
494 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
495 0 say STDERR "object level only_address: $only_address";
496 0 say STDERR "formatting level only_address: $oa";
497 }
498  
499 492 ▶ if ($oa){
Branch: 1 branch
BranchTrueFalseTotal
if ($oa) { } 3 489 492
500 3 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 3 3
501 0 say STDERR "not looking for unknown_components";
502 0 say STDERR "only_address was specified";
503 }
504 }
505 else {
506 489 my $ra_unknown = $self->_find_unknown_components($rh_components);
507 489 ▶ if (scalar(@$ra_unknown)){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$ra_unknown) 230 259 489
508 230 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 230 230
509 0 say STDERR "unknown_components:";
510 0 say STDERR Dumper $ra_unknown;
511 }
512 # need to sort for consistency
513 # FIXME - add better sorting based on meaning of the values
514 $rh_components->{attention} =
515 230 join(', ', map { $rh_components->{$_} } sort @$ra_unknown);
516 230 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 230 230
517 0 say STDERR "putting unknown_components in 'attention'";
518 }
519 }
520 }
521  
522 # 8. abbreviate, if needed
523 492 ▶ if ($abbrv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abbrv) 10 482 492
524 10 $rh_components = $self->_abbreviate($rh_components);
525 }
526  
527 # 9. prepare the template (and cache it)
528 492 ▶ $template_text = $self->{_lambda_cache}{$template_text}
Condition66%2/3
$self->{"_lambda_cache"}{$template_text} //= $self->_replace_template_lambdas($template_text)
l!l&&r!l&&!r
3671250
MC/DC0%0/2
$self->{"_lambda_cache"}{$template_text} //= $self->_replace_template_lambdas($template_text)
$self->{"_lambda_cache"}{$template_text} $self->_replace_template_lambdas($template_text)
Truth table66%2/3
A //= B
A= $self->{"_lambda_cache"}{$template_text}B= $self->_replace_template_lambdas($template_text)
ABresult
1X1
011
000
529 //= $self->_replace_template_lambdas($template_text);
530  
531 # 10. compiled the template
532 492 ▶ my $compiled_template = $self->{compiled_template_cache}{$template_text}
Condition66%2/3
$self->{"compiled_template_cache"}{$template_text} //= $THC->compile($template_text, {"numeric_string_as_string", 1})
l!l&&r!l&&!r
3671250
MC/DC0%0/2
$self->{"compiled_template_cache"}{$template_text} //= $THC->compile($template_text, {"numeric_string_as_string", 1})
$self->{"compiled_template_cache"}{$template_text} $THC->compile($template_text, {"numeric_string_as_string", 1})
Truth table66%2/3
A //= B
A= $self->{"compiled_template_cache"}{$template_text}B= $THC->compile($template_text, {"numeric_string_as_string", 1})
ABresult
1X1
011
000
533 //= $THC->compile($template_text, {'numeric_string_as_string' => 1});
534  
535 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
536 0 say STDERR "before _render_template";
537 0 say STDERR Dumper $rh_components;
538 0 say STDERR "template: ";
539 0 say STDERR Dumper $compiled_template;
540 }
541  
542  
543 # 11. render the template
544 492 my $text = $self->_render_template($compiled_template, $rh_components);
545 492 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 492 492
546 0 say STDERR "text after _render_template $text";
547 }
548  
549 # 12. postformatting
550 492 $text = $self->_postformat($text, $rh_config->{_compiled_postformat});
551  
552 # 13. clean again
553 492 $text = $self->_clean($text);
554  
555 # 14. set final components (so we can get them later)
556 492 $self->{final_components} = $rh_components;
557  
558 # all done
559 492 return $text;
560 }
561  
562 # remove duplicates ("Berlin, Berlin"), do replacements and similar
563 494 ▶sub _postformat {
Subroutine _postformat: called CC=8 SCAR=20.8
564 494 my $self = shift;
565 494 my $text = shift;
566 494 my $raa_rules = shift;
567  
568 494 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 494 494
569 0 say STDERR "entering _postformat: $text";
570 0 say STDERR Dumper $raa_rules;
571 }
572  
573 # remove duplicates
574 494 my @before_pieces = split(/, /, $text);
575 494 my %seen;
576 my @after_pieces;
577 494 foreach my $piece (@before_pieces) {
578 612 $piece =~ s/^\s+//g;
579 612 $seen{$piece}++;
580 612 ▶ if (lc($piece) ne 'new york') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $piece ne "new york") 608 4 612
581 608 ▶ next if ($seen{$piece} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$piece} > 1 1 607 608
582 }
583 611 push(@after_pieces, $piece);
584 }
585 494 $text = join(', ', @after_pieces);
586  
587 # do any country specific rules
588 494 foreach my $rule (@$raa_rules) {
589 554 my $regexp = $rule->{re};
590 554 my $replacement = $rule->{replacement};
591  
592 # ultra hack to do substitution
593 # limited to $1 and $2, should really be a while loop
594 # doing every substitution
595  
596 554 ▶ if ($replacement =~ m/\$\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replacement =~ /\$\d/) 50 504 554
597 50 ▶ if ($text =~ m/$regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$regexp/) 21 29 50
598 21 my $tmp1 = $1;
599 21 my $tmp2 = $2;
600 21 my $tmp3 = $3;
601 21 $replacement =~ s/\$1/$tmp1/;
602 21 $replacement =~ s/\$2/$tmp2/;
603 21 $replacement =~ s/\$3/$tmp3/;
604 }
605 }
606 554 $text =~ s/$regexp/$replacement/;
607 }
608 494 return $text;
609 }
610  
611 493 ▶sub _sanity_cleaning {
Subroutine _sanity_cleaning: called CC=10 SCAR=23.1
612 493 my $self = shift;
613 493 ▶ my $rh_components = shift || return;
Condition50%1/2
shift() || (return)
l!l
4930
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
614  
615 # catch insane postcodes
616 493 ▶ if (defined($rh_components->{'postcode'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{"postcode"}) 334 159 493
617 334 ▶ if (length($rh_components->{'postcode'}) > 20) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $rh_components->{'postcode'} > 20) { } 1 333 334
elsif ($rh_components->{'postcode'} =~ /\d+;\d+/) { } 1 332 333
elsif ($rh_components->{'postcode'} =~ /^(\d{5}),\d{5}/) { } 1 331 332
618 1 delete $rh_components->{'postcode'};
619 } elsif ($rh_components->{'postcode'} =~ m/\d+;\d+/) {
620 # sometimes OSM has postcode ranges
621 1 delete $rh_components->{'postcode'};
622 } elsif ($rh_components->{'postcode'} =~ m/^(\d{5}),\d{5}/) {
623 1 $rh_components->{'postcode'} = $1;
624 }
625 }
626  
627 # remove things that might be empty
628 493 foreach my $c (keys %$rh_components) {
629 # catch empty values
630 3836 ▶ if (!defined($rh_components->{$c})) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $rh_components->{$c}) { } 0 3836 3836
elsif (not $rh_components->{$c} =~ /\w/) { } 1 3835 3836
elsif ($rh_components->{$c} =~ m[https?://]) { } 1 3834 3835
631 0 delete $rh_components->{$c};
632 }
633 # no chars
634 elsif ($rh_components->{$c} !~ m/\w/) {
635 1 delete $rh_components->{$c};
636 }
637 # catch values containing URLs
638 elsif ($rh_components->{$c} =~ m|https?://|) {
639 1 delete $rh_components->{$c};
640 }
641 }
642 493 return;
643 }
644  
645 490 ▶sub _minimal_components {
Subroutine _minimal_components: called CC=5 SCAR=16.1
646 490 my $self = shift;
647 490 ▶ my $rh_components = shift || return;
Condition50%1/2
shift() || (return)
l!l
4900
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
648 490 my @required_components = qw(road postcode); #FIXME - should be in conf
649 490 my $missing = 0; # num required components that are missing
650  
651 490 my $minimal_threshold = 2;
652 490 foreach my $c (@required_components) {
653 980 ▶ $missing++ if (!defined($rh_components->{$c}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rh_components->{$c} 237 743 980
654 980 ▶ return 0 if ($missing == $minimal_threshold);
Branch: 1 branch
BranchTrueFalseTotal
if $missing == $minimal_threshold 57 923 980
655 }
656 433 return 1;
657 }
658  
659 7 my %valid_replacement_components = ('state' => 1,);
660  
661 # determines which country code to use
662 # may also override other configuration if we are dealing with
663 # a dependent territory
664 493 ▶sub _determine_country_code {
Subroutine _determine_country_code: called CC=18 SCAR=29.6
665 493 my $self = shift;
666 493 ▶ my $rh_components = shift || return;
Condition50%1/2
shift() || (return)
l!l
4930
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
667  
668 # FIXME - validate it is a valid country
669 493 ▶ return if (!defined($rh_components->{country_code}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rh_components->{"country_code"} 4 489 493
670  
671 489 ▶ if (my $cc = lc($rh_components->{country_code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cc = lc $rh_components->{"country_code"}) 489 0 489
672  
673 # is it two letters long?
674 489 ▶ return if (length($cc) != 2);
Branch: 1 branch
BranchTrueFalseTotal
if length $cc != 2 0 489 489
675 489 ▶ return 'GB' if ($cc eq 'uk');
Branch: 1 branch
BranchTrueFalseTotal
if $cc eq "uk" 0 489 489
676  
677 489 $cc = uc($cc);
678  
679 # check if the configuration tells us to use
680 # the configuration of another country
681 # used in cases of dependent territories like
682 # American Samoa (AS) and Puerto Rico (PR)
683 489 ▶ if ( defined($self->{templates}{$cc})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"templates"}{$cc} and defined $self->{"templates"}{$cc}{"use_country"}) 46 443 489
Condition100%3/3
defined $self->{"templates"}{$cc} and defined $self->{"templates"}{$cc}{"use_country"}
!ll&&!rl&&r
244146
MC/DC100%2/2
defined $self->{"templates"}{$cc} and defined $self->{"templates"}{$cc}{"use_country"}
defined $self->{"templates"}{$cc} defined $self->{"templates"}{$cc}{"use_country"}
Truth table100%3/3
A and B
A= defined $self->{"templates"}{$cc}B= defined $self->{"templates"}{$cc}{"use_country"}
ABresult
0X0
100
111
684 && defined($self->{templates}{$cc}{use_country}))
685 {
686 46 my $old_cc = $cc;
687 46 $cc = $self->{templates}{$cc}{use_country};
688 46 ▶ if (defined($self->{templates}{$old_cc}{change_country})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"templates"}{$old_cc}{"change_country"}) 35 11 46
689  
690 35 my $new_country = $self->{templates}{$old_cc}{change_country};
691 35 ▶ if ($new_country =~ m/\$(\w*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_country =~ /\$(\w*)/) 2 33 35
692 2 my $component = $1;
693 2 ▶ if (defined($rh_components->{$component})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{$component}) { } 2 0 2
694 2 $new_country =~ s/\$$component/$rh_components->{$component}/;
695 } else {
696 0 $new_country =~ s/\$$component//;
697 }
698 }
699 35 $rh_components->{country} = $new_country;
700 }
701 46 ▶ if (defined($self->{templates}{$old_cc}{add_component})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"templates"}{$old_cc}{"add_component"}) 10 36 46
702 10 my $tmp = $self->{templates}{$old_cc}{add_component};
703 10 my ($k, $v) = split(/=/, $tmp);
704 # check whitelist of valid replacement components
705 10 ▶ if (defined($valid_replacement_components{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid_replacement_components{$k}) 9 1 10
706 9 $rh_components->{$k} = $v;
707 }
708 }
709 }
710  
711 489 ▶ if ($cc eq 'NL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq "NL") 5 484 489
712 5 ▶ if (defined($rh_components->{state})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{"state"}) 5 0 5
713 5 ▶ if ($rh_components->{state} eq 'Curaçao') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rh_components->{'state'} eq "Cura\347ao") { } 1 4 5
elsif ($rh_components->{'state'} =~ /^sint maarten/i) { } 0 4 4
elsif ($rh_components->{'state'} =~ /^Aruba/i) { } 1 3 4
714 1 $cc = 'CW';
715 1 $rh_components->{country} = 'Curaçao';
716 } elsif ($rh_components->{state} =~ m/^sint maarten/i) {
717 0 $cc = 'SX';
718 0 $rh_components->{country} = 'Sint Maarten';
719 } elsif ($rh_components->{state} =~ m/^Aruba/i) {
720 1 $cc = 'AW';
721 1 $rh_components->{country} = 'Aruba';
722 }
723 }
724 }
725 489 return $cc;
726 }
727 0 return;
728 }
729  
730 # hacks for bad country data
731 492 ▶sub _fix_country {
Subroutine _fix_country: called CC=5 SCAR=16.1
732 492 my $self = shift;
733 492 ▶ my $rh_c = shift || return;
Condition50%1/2
shift() || (return)
l!l
4920
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
734  
735 # is the country a number?
736 # if so, and there is a state, use state as country
737 492 ▶ if (defined($rh_c->{country}) && defined($rh_c->{state})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_c->{"country"} and defined $rh_c->{"state"}) 389 103 492
Condition100%3/3
defined $rh_c->{"country"} and defined $rh_c->{"state"}
!ll&&!rl&&r
994389
MC/DC100%2/2
defined $rh_c->{"country"} and defined $rh_c->{"state"}
defined $rh_c->{"country"} defined $rh_c->{"state"}
Truth table100%3/3
A and B
A= defined $rh_c->{"country"}B= defined $rh_c->{"state"}
ABresult
0X0
100
111
738 389 ▶ if (looks_like_number($rh_c->{country})){
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($rh_c->{"country"})) 1 388 389
739 1 $rh_c->{country} = delete $rh_c->{state};
740 }
741 }
742 492 return;
743 }
744  
745 # sets and returns a state code
746 # note may also set other values in some odd edge cases
747 497 ▶sub _add_state_code {
Subroutine _add_state_code: called CC=3 SCAR=11.0
748 497 my $self = shift;
749 497 ▶ my $rh_components = shift // return;
Condition50%1/2
shift() // (return)
l!l
4970
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
750 497 ▶ if (defined($rh_components->{state})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{"state"}) 395 102 497
751 395 return $self->_add_code('state', $rh_components);
752 }
753 102 return;
754 }
755  
756 496 ▶sub _add_county_code {
Subroutine _add_county_code: called CC=3 SCAR=11.0
757 496 my $self = shift;
758 496 ▶ my $rh_components = shift // return;
Condition50%1/2
shift() // (return)
l!l
4960
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
759 496 ▶ if (defined($rh_components->{county})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{"county"}) 216 280 496
760 216 return $self->_add_code('county', $rh_components);
761 }
762 280 return;
763 }
764  
765 611 ▶sub _add_code {
Subroutine _add_code: called CC=16 SCAR=28.8
766 611 my $self = shift;
767 611 ▶ my $keyname = shift // return;
Condition50%1/2
shift() // (return)
l!l
6110
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
768 611 my $rh_components = shift;
769 611 ▶ return if !$rh_components->{country_code}; # do we know country?
Branch: 1 branch
BranchTrueFalseTotal
unless $rh_components->{"country_code"} 4 607 611
770 607 ▶ return if !$rh_components->{$keyname}; # do we know state/county?
Branch: 1 branch
BranchTrueFalseTotal
unless $rh_components->{$keyname} 0 607 607
771  
772 607 my $code = $keyname . '_code';
773  
774 607 ▶ if (defined($rh_components->{$code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{$code}) 32 575 607
775 # do we already have code?
776 # we could have situation where code and long name are same
777 # so we want to corrent
778 32 ▶ return if ($rh_components->{$code} ne $rh_components->{$keyname});
Branch: 1 branch
BranchTrueFalseTotal
if $rh_components->{$code} ne $rh_components->{$keyname} 31 1 32
779 }
780  
781 # ensure country_code is uppercase as we use it as conf key
782 576 my $cc = uc($rh_components->{country_code});
783  
784 576 my $reverse_key = $code . 's_reverse';
785 576 my $name_key = $code . 's_name';
786  
787 576 ▶ if (my $rev = $self->{$reverse_key}{$cc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rev = $self->{$reverse_key}{$cc}) 306 270 576
788 306 my $name = $rh_components->{$keyname};
789 306 my $uc_name = uc($name);
790  
791 306 ▶ if (my $found_code = $rev->{$uc_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $found_code = $rev->{$uc_name}) 204 102 306
792 # matched a name -> code
793 204 $rh_components->{$code} = $found_code;
794 # if input was a code (e.g. state => 'NC'), set keyname to full name
795 204 ▶ if ($uc_name eq $found_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uc_name eq $found_code) 6 198 204
796 6 my $full_name = $self->{$name_key}{$cc}{$found_code};
797 6 ▶ $rh_components->{$keyname} = $full_name if defined $full_name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $full_name 6 0 6
798 }
799 }
800  
801 # US-specific edge cases
802 306 ▶ if ($cc eq 'US' && $keyname eq 'state' && !defined($rh_components->{state_code})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq "US" and $keyname eq "state" and not defined $rh_components->{"state_code"}) 2 304 306
Condition83%5/6
$cc eq "US" and $keyname eq "state" and not defined $rh_components->{"state_code"}
!ll&&!rl&&r
280242
$cc eq "US" and $keyname eq "state"
!ll&&!rl&&r
280026
MC/DC66%2/3
$cc eq "US" and $keyname eq "state" and not defined $rh_components->{"state_code"}
$cc eq "US" $keyname eq "state" not defined $rh_components->{"state_code"}
Truth table75%3/4
A and B and not(C)
A= $cc eq "US"B= $keyname eq "state"C= not defined $rh_components->{"state_code"}
ABCresult
0XX0
10X0
1100
1111
803 2 ▶ if ($rh_components->{state} =~ m/^united states/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_components->{"state"} =~ /^united states/i) 0 2 2
804 0 my $state = $rh_components->{state};
805 0 $state =~ s/^United States/US/i;
806 0 ▶ if (my $fc = $rev->{uc($state)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fc = $$rev{uc $state}) 0 0 0
807 0 $rh_components->{state_code} = $fc;
808 }
809 }
810 2 ▶ if ($rh_components->{state} =~ m/^washington,? d\.?c\.?/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_components->{"state"} =~ /^washington,? d\.?c\.?/i) 2 0 2
811 2 $rh_components->{state_code} = 'DC';
812 2 $rh_components->{state} = 'District of Columbia';
813 2 $rh_components->{city} = 'Washington';
814 }
815 }
816 }
817 576 return $rh_components->{$code};
818 }
819  
820 163 ▶sub _apply_replacements {
Subroutine _apply_replacements: called CC=20 SCAR=32.3
821 163 my $self = shift;
822 163 my $rh_components = shift;
823 163 ▶ my $ra_rules = shift // return; # bail out if no rules
Condition50%1/2
shift() // (return)
l!l
1630
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
824  
825 163 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 163 163
826 0 say STDERR "in _apply_replacements";
827 0 say STDERR Dumper $ra_rules;
828 }
829  
830 # support legacy raw array-of-arrays format (used by tests)
831 163 ▶ if (ref($ra_rules) eq 'ARRAY' && @$ra_rules && ref($ra_rules->[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ra_rules eq "ARRAY" and @$ra_rules and ref $ra_rules->[0] eq "ARRAY") 2 161 163
Condition83%5/6
ref $ra_rules eq "ARRAY" and @$ra_rules and ref $ra_rules->[0] eq "ARRAY"
!ll&&!rl&&r
11602
ref $ra_rules eq "ARRAY" and @$ra_rules
!ll&&!rl&&r
01162
MC/DC66%2/3
ref $ra_rules eq "ARRAY" and @$ra_rules and ref $ra_rules->[0] eq "ARRAY"
ref $ra_rules eq "ARRAY" @$ra_rules ref $ra_rules->[0] eq "ARRAY"
Truth table75%3/4
A and B and C
A= ref $ra_rules eq "ARRAY"B= @$ra_rulesC= ref $ra_rules->[0] eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
832 2 foreach my $component (keys %$rh_components) {
833 2 ▶ next if ($component eq 'country_code');
Branch: 1 branch
BranchTrueFalseTotal
if $component eq "country_code" 0 2 2
834 2 ▶ next if ($component eq 'house_number');
Branch: 1 branch
BranchTrueFalseTotal
if $component eq "house_number" 0 2 2
835 2 foreach my $ra_fromto (@$ra_rules) {
836 3 my $regexp;
837 3 ▶ if ($ra_fromto->[0] =~ m/^$component=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ra_fromto->[0] =~ /^$component=/) { } 0 3 3
838 0 my $from = $ra_fromto->[0];
839 0 $from =~ s/^$component=//;
840 0 ▶ if ($rh_components->{$component} eq $from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_components->{$component} eq $from) { } 0 0 0
841 0 $rh_components->{$component} = $ra_fromto->[1];
842 } else {
843 0 $regexp = $from;
844 }
845 } else {
846 3 $regexp = $ra_fromto->[0];
847 }
848 3 ▶ if (defined($regexp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regexp) 3 0 3
849 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
850 3 my $re = qr/$regexp/i;
851 2 $rh_components->{$component} =~ s/$re/$ra_fromto->[1]/;
852 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
853 1 warn "invalid replacement: " . join(', ', @$ra_fromto);
854 3 };
855 }
856 }
857 }
858 2 return $rh_components;
859 }
860  
861 161 foreach my $component (keys %$rh_components) {
862 # some components dont need replacements
863 1362 ▶ next if ($component eq 'country_code');
Branch: 1 branch
BranchTrueFalseTotal
if $component eq "country_code" 160 1202 1362
864 1202 ▶ next if ($component eq 'house_number');
Branch: 1 branch
BranchTrueFalseTotal
if $component eq "house_number" 61 1141 1202
865 1141 foreach my $rule (@$ra_rules) {
866 7937 ▶ if (defined($rule->{component})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rule->{'component'}) { } 1990 5947 7937
867 # component-specific rule: only applies to matching component
868 1990 ▶ next if ($rule->{component} ne $component);
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{"component"} ne $component 1793 197 1990
869 197 ▶ if ($rh_components->{$component} eq $rule->{exact_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_components->{$component} eq $rule->{'exact_match'}) { } 4 193 197
870 4 $rh_components->{$component} = $rule->{replacement};
871 } else {
872 193 $rh_components->{$component} =~ s/$rule->{re}/$rule->{replacement}/;
873 }
874 } else {
875 5947 $rh_components->{$component} =~ s/$rule->{re}/$rule->{replacement}/;
876 }
877 }
878 }
879 161 return $rh_components;
880 }
881  
882 12 ▶sub _abbreviate {
Subroutine _abbreviate: called CC=11 SCAR=24.0
883 12 my $self = shift;
884 12 ▶ my $rh_comp = shift // return;
Condition50%1/2
shift() // (return)
l!l
120
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
885  
886 # do we know the country?
887 12 ▶ if (!defined($rh_comp->{country_code})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rh_comp->{"country_code"}) 2 10 12
888 2 ▶ if ($show_warnings){
Branch: 1 branch
BranchTrueFalseTotal
if ($show_warnings) 1 1 2
889 1 my $error_msg = 'no country_code, unable to abbreviate';
890 1 ▶ if (defined($rh_comp->{country})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_comp->{"country"}) 1 0 1
891 1 $error_msg .= ' - country: ' . $rh_comp->{country};
892 }
893 1 warn $error_msg
894 }
895 2 return;
896 }
897  
898 # do we have abbreviations for this country?
899 10 my $cc = uc($rh_comp->{country_code});
900  
901 # 1. which languages?
902 10 ▶ if (defined($self->{country2lang}{$cc})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"country2lang"}{$cc}) 10 0 10
903  
904 10 my @langs = split(/,/, $self->{country2lang}{$cc});
905  
906 10 foreach my $lang (@langs) {
907 # do we have compiled abbrv for this lang?
908 14 ▶ if (defined($self->{compiled_abbreviations}{$lang})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'compiled_abbreviations'}{$lang}) { } 11 3 14
909 11 my $rh_compiled = $self->{compiled_abbreviations}{$lang};
910  
911 11 foreach my $comp_name (keys %$rh_compiled) {
912 16 ▶ next if (!defined($rh_comp->{$comp_name}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rh_comp->{$comp_name} 1 15 16
913 15 foreach my $rule (@{$rh_compiled->{$comp_name}}) {
914 111 $rh_comp->{$comp_name} =~ s/$rule->{re}/$1$rule->{short}/;
915 }
916 }
917 } else {
918 #warn "no abbreviations defined for lang $lang";
919 }
920 }
921 }
922 10 return $rh_comp;
923 }
924  
925 # " abc,,def , ghi " => 'abc, def, ghi'
926 990 ▶sub _clean {
Subroutine _clean: called CC=9 SCAR=22.0
927 990 my $self = shift;
928 990 ▶ my $out = shift // return;
Condition100%2/2
shift() // (return)
l!l
9891
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
929 989 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 989 989
930 0 say STDERR "entering _clean \n$out";
931 }
932  
933 989 $out =~ s/\&#39\;/'/g;
934  
935 989 $out =~ s/[\},\s]+$//;
936 989 $out =~ s/^[,\s]+//;
937  
938 989 $out =~ s/^- //; # line starting with dash due to a parameter missing
939  
940 989 $out =~ s/,\s*,/, /g; # multiple commas to one
941 989 $out =~ s/\h+,\h+/, /g; # one horiz whitespace behind comma
942 989 $out =~ s/\h\h+/ /g; # multiple horiz whitespace to one
943 989 $out =~ s/\h\n/\n/g; # horiz whitespace, newline to newline
944 989 $out =~ s/\n,/\n/g; # newline comma to just newline
945 989 $out =~ s/,,+/,/g; # multiple commas to one
946 989 $out =~ s/,\n/\n/g; # comma newline to just newline
947 989 $out =~ s/\n\h+/\n/g; # newline plus space to newline
948 989 $out =~ s/\n\n+/\n/g; # multiple newline to one
949  
950 # final dedupe across and within lines
951 989 my @before_pieces = split(/\n/, $out);
952 989 my %seen_lines;
953 my @after_pieces;
954 989 foreach my $line (@before_pieces) {
955 3759 $line =~ s/^\h+//g;
956 3759 $line =~ s/\h+$//g;
957 3759 $seen_lines{$line}++;
958 3759 ▶ next if ($seen_lines{$line} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seen_lines{$line} > 1 20 3739 3759
959 # now dedupe within the line
960 3739 my @before_words = split(/,/, $line);
961 3739 my %seen_words;
962 my @after_words;
963 3739 foreach my $w (@before_words) {
964 3962 $w =~ s/^\h+//g;
965 3962 $w =~ s/\h+$//g;
966 3962 ▶ if (lc($w) ne 'new york') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $w ne "new york") 3952 10 3962
967 3952 $seen_words{$w}++;
968 }
969 3962 ▶ next if ((defined($seen_words{$w})) && ($seen_words{$w} > 1));
Branch: 1 branch
BranchTrueFalseTotal
if defined $seen_words{$w} and $seen_words{$w} > 1 2 3960 3962
Condition100%3/3
defined $seen_words{$w} and $seen_words{$w} > 1
!ll&&!rl&&r
1039502
MC/DC100%2/2
defined $seen_words{$w} and $seen_words{$w} > 1
defined $seen_words{$w} $seen_words{$w} > 1
Truth table100%3/3
A and B
A= defined $seen_words{$w}B= $seen_words{$w} > 1
ABresult
0X0
100
111
970 3960 push(@after_words, $w);
971 }
972 3739 $line = join(', ', @after_words);
973 3739 push(@after_pieces, $line);
974 }
975 989 $out = join("\n", @after_pieces);
976  
977 989 $out =~ s/^\s+//; # remove leading whitespace
978 989 $out =~ s/\s+$//; # remove end whitespace
979  
980 989 $out .= "\n"; # add final newline
981 989 return $out; # we are done
982 }
983  
984 493 ▶sub _render_template {
Subroutine _render_template: called CC=6 SCAR=21.0
985 493 my $self = shift;
986 493 my $thtemplate = shift;
987 493 my $components = shift;
988  
989 # Mustache calls it context
990 493 my $context = $components;
991 493 ▶ say STDERR 'context: ' . Dumper $context if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 493 493
992 493 my $output = $thtemplate->render($context);
993  
994 493 $output = $self->_evaluate_template_lamdas($output);
995  
996 493 ▶ say STDERR "in _render before _clean: $output" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 493 493
997 493 $output = $self->_clean($output);
998  
999 # is it empty?
1000 493 ▶ if (length($output) == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (length $output == 0) 0 493 493
1001 # if yes and there is only one component then just use that one
1002 0 my @comps = keys %$components;
1003 0 ▶ if (scalar(@comps) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @comps == 1) 0 0 0
1004 0 foreach my $k (@comps) {
1005 0 $output = $components->{$k};
1006 }
1007 } # FIXME what if more than one?
1008 }
1009 493 return $output;
1010 }
1011  
1012 # Text::Hogan apparently caches lambdas when rendering templates. In the past
1013 # we needed our lambda 'first', example
1014 # {{#first}} {{{city}}} || {{{town}}} {{/first}}
1015 # to evaluate the components. Whenever the lambda was called with different
1016 # component values it consumed memory. Now replace with a simpler implementation
1017 #
1018 126 ▶sub _replace_template_lambdas {
Subroutine _replace_template_lambdas: called CC=1 SCAR=0.0
1019 126 my $self = shift;
1020 126 my $template_text = shift;
1021 126 $template_text =~ s!\Q{{#first}}\E(.+?)\Q{{/first}}\E!FIRSTSTART${1}FIRSTEND!g;
1022 126 return $template_text;
1023 }
1024  
1025 # We only use a lambda named 'first'
1026 493 ▶sub _evaluate_template_lamdas {
Subroutine _evaluate_template_lamdas: called CC=2 SCAR=6.9
1027 493 my $self = shift;
1028 493 my $text = shift;
1029 493 ▶ return $text if (index($text, 'FIRSTSTART') < 0); # only do regex if needed
Branch: 1 branch
BranchTrueFalseTotal
if (index($text, "FIRSTSTART") == -1) 5 488 493
1030 488 $text =~ s!FIRSTSTART\s*(.+?)\s*FIRSTEND!_select_first($1)!seg;
1031 488 return $text;
1032 }
1033  
1034 # '|| val1 || || val3' => 'val1'
1035 926 ▶sub _select_first {
Subroutine _select_first: called CC=2 SCAR=6.9
1036 926 my $text = shift;
1037 926 my @a_parts = grep { length($_) } split(/\s*\|\|\s*/, $text);
1038 926 ▶ return scalar(@a_parts) ? $a_parts[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
scalar @a_parts ? : 800 126 926
1039 }
1040  
1041 7 my %small_district = (
1042 'BR' => 1,
1043 'CR' => 1,
1044 'ES' => 1,
1045 'NI' => 1,
1046 'PY' => 1,
1047 'RO' => 1,
1048 'TG' => 1,
1049 'TM' => 1,
1050 'XK' => 1,
1051 );
1052  
1053 # correct the alias for "district"
1054 # in OSM some countries use district to mean "city_district"
1055 # others to mean "state_district"
1056 488 ▶sub _set_district_alias {
Subroutine _set_district_alias: called CC=4 SCAR=13.9
1057 488 my $self = shift;
1058 488 ▶ my $cc = shift // return;
Condition50%1/2
shift() // (return)
l!l
4880
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1059  
1060 488 my $ucc = uc($cc);
1061  
1062 # this may get called repeatedly
1063 # no need to do the work again
1064 488 ▶ return if (defined($self->{set_district_alias}{$ucc}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"set_district_alias"}{$ucc} 254 234 488
1065  
1066 # note that we are here so don't do this work again
1067 234 $self->{set_district_alias}{$ucc} = 1;
1068  
1069 234 my $oldalias;
1070 234 ▶ if (defined($small_district{$ucc})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $small_district{$ucc}) { } 11 223 234
1071 11 $self->{component2type}{district} = 'neighbourhood';
1072 11 $oldalias = 'state_district';
1073  
1074 # add to the neighbourhood alias list
1075 # though of course we are just sticking it at the end
1076 11 push(@{$self->{component_aliases}{'neighbourhood'}}, 'district');
1077  
1078 } else {
1079 # set 'district' to be type 'state_district'
1080 223 $self->{component2type}{district} = 'state_district';
1081 223 $oldalias = 'neighbourhood';
1082  
1083 # add to the state_district alias list
1084 223 push(@{$self->{component_aliases}{'state_district'}}, 'district');
1085 }
1086  
1087 # remove from the old alias list
1088 3075 my @temp = grep { $_ ne 'district' }
1089 234 @{$self->{component_aliases}{$oldalias}};
1090
1091 234 $self->{component_aliases}{$oldalias} = \@temp;
1092 234 return;
1093 }
1094  
1095  
1096 # returns []
1097 490 ▶sub _find_unknown_components {
Subroutine _find_unknown_components: called CC=1 SCAR=0.0
1098 490 my $self = shift;
1099 490 my $rh_comp = shift;
1100  
1101 490 my @a_unknown = grep { !exists($self->{h_known}->{$_}) } keys %$rh_comp;
1102 490 return \@a_unknown;
1103 }
1104  
1105  
1106 7 1;
1107  
1108 __END__
↑ Summary