lib/Geo/Address/Formatter.pm

filter: Statementstmt 88.1% 445 / 505 Branchbran 79.2% 176 / 222 Conditioncond 71.4% 35 / 49 MC/DCmcdc 48.0% 12 / 25 Subroutinesub 89.1% 41 / 46 Podpod 100% 3 / 3 totaltotal 83.7% 712 / 850 CC 158 SCAR 30.6
CC
158
Cov
85%
format_address
68.2
_add_code
20.2
_determine_country_code
19.3
CRAP 21.3
« blib/lib/Geo/Address/Formatter.pm ↑ Summary
1 package Geo::Address::Formatter;
2 6 $Geo::Address::Formatter::VERSION = '1.994';
3 # ABSTRACT: take structured address data and format it according to the various global/country rules
4  
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use feature qw(say);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 $Data::Dumper::Sortkeys = 1;
11 6 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Ref::Util qw(is_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Text::Hogan::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Try::Catch;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use YAML::XS qw(LoadFile);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 6 my $THC = Text::Hogan::Compiler->new;
21  
22 # optional params
23 6 my $show_warnings = 1;
24 6 my $debug = 0;
25 6 my $only_address = 0;
26  
27  
28 sub new {
29 17 ▶ my ($class, %params) = @_;
Subroutine new: called CC=10 SCAR=23.9
Pod documented
30  
31 17 my $self = {};
32 17 ▶ my $conf_path = $params{conf_path} || die "no conf_path set";
Condition50%1/2
$params{'conf_path'} || die('no conf_path set')
l!l
170
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
33  
34 # optional params
35 17 ▶ if ( defined($params{no_warnings}) && ($params{no_warnings})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"no_warnings"} and $params{"no_warnings"}) 1 16 17
Condition66%2/3
defined $params{"no_warnings"} and $params{"no_warnings"}
!ll&&!rl&&r
1601
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
36 1 $show_warnings = 0;
37 }
38 17 ▶ $only_address = (defined($params{only_address}) && $params{only_address}) // 0;
Condition60%3/5
defined $params{"only_address"} && $params{"only_address"} // 0
l!l
170
defined $params{"only_address"} and $params{"only_address"}
!ll&&!rl&&r
1502
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
39 17 ▶ $debug = (defined($params{debug}) && $params{debug}) // 0;
Condition60%3/5
defined $params{"debug"} && $params{"debug"} // 0
l!l
170
defined $params{"debug"} and $params{"debug"}
!ll&&!rl&&r
1205
MC/DC50%1/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
40  
41 17 $self->{final_components} = undef;
42 17 bless($self, $class);
43  
44 17 ▶ say STDERR "************* in Geo::Address::Formatter::new ***" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 17 17
45
46 17 ▶ if ($self->_read_configuration($conf_path)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_read_configuration($conf_path)) 16 1 17
47 16 return $self;
48 }
49 1 die 'unable to read configuration';
50 }
51  
52 sub _read_configuration {
53 17 ▶ my $self = shift;
Subroutine _read_configuration: called CC=7 SCAR=20.1
54 17 my $path = shift;
55  
56 17 ▶ return if (! -e $path);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $path 0 17 17
57  
58 17 my @a_filenames = File::Find::Rule->file()->name('*.yaml')->in($path . '/countries');
59  
60 17 $self->{templates} = {};
61 17 $self->{component_aliases} = {};
62 17 $self->{component2type} = {};
63 17 $self->{ordered_components} = [];
64  
65 # read the config file(s)
66 17 my $loaded = 0;
67 17 foreach my $filename (sort @a_filenames) {
68 try {
69 16 ▶ my $rh_templates = LoadFile($filename);
Subroutine __ANON__: called CC=2 SCAR=6.9
70  
71 # if file 00-default.yaml defines 'DE' (Germany) and
72 # file 01-germany.yaml does as well, then the second
73 # occurance of the key overwrites the first.
74 16 foreach (keys %$rh_templates) {
75 2930 $self->{templates}{$_} = $rh_templates->{$_};
76 }
77 16 $loaded = 1;
78 } catch {
79 0 ▶ warn "error parsing country configuration in $filename: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 16 };
81 }
82 17 ▶ return if ($loaded == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $loaded == 0 1 16 17
83  
84 # see if we can load the components
85 try {
86 16 ▶ say STDERR "loading components" if ($debug);
Subroutine __ANON__: called CC=10 SCAR=25.2
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 16 16
87 16 my @c = LoadFile($path . '/components.yaml');
88  
89 16 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 16 16
90 0 say STDERR Dumper \@c;
91 }
92  
93 16 foreach my $rh_c (@c) {
94 234 ▶ if (defined($rh_c->{name})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_c->{"name"}) 234 0 234
95 234 ▶ if (defined($rh_c->{aliases})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_c->{'aliases'}) { } 128 106 234
96 128 $self->{component_aliases}{$rh_c->{name}} = $rh_c->{aliases};
97 } else {
98 106 $self->{component_aliases}{$rh_c->{name}} = [];
99 }
100 }
101 }
102  
103 16 foreach my $rh_c (@c) {
104 234 push(@{$self->{ordered_components}}, $rh_c->{name});
105 234 $self->{component2type}->{$rh_c->{name}} = $rh_c->{name};
106  
107 234 ▶ if (defined($rh_c->{aliases})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_c->{"aliases"}) 128 106 234
108 128 foreach my $alias (@{$rh_c->{aliases}}) {
109 480 push(@{$self->{ordered_components}}, $alias);
110 480 $self->{component2type}->{$alias} = $rh_c->{name};
111 }
112 }
113 }
114 16 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 16 16
115 0 say STDERR 'component_aliases';
116 0 say STDERR Dumper $self->{component_aliases};
117 0 say STDERR 'ordered_components';
118 0 say STDERR Dumper $self->{ordered_components};
119 0 say STDERR 'component2type';
120 0 say STDERR Dumper $self->{component2type};
121 }
122 } catch {
123 0 ▶ warn "error parsing component configuration: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 16 };
125  
126 # get the county and state codes and country2lang conf
127 16 my @conf_files = qw(county_codes state_codes country2lang);
128 16 foreach my $cfile (@conf_files) {
129 48 $self->{$cfile} = {};
130 48 my $yfile = $path . '/' . $cfile . '.yaml';
131 48 ▶ if (-e $yfile) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $yfile) 42 6 48
132 try {
133 42 ▶ $self->{$cfile} = LoadFile($yfile);
Subroutine __ANON__: called CC=1 SCAR=0.0
134 } catch {
135 0 ▶ warn "error parsing $cfile configuration: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 42 };
137 }
138 }
139  
140 # get the abbreviations
141 16 my @abbrv_filenames = File::Find::Rule->file()->name('*.yaml')->in($path . '/abbreviations');
142  
143 # read the config files
144 16 foreach my $abbrv_file (@abbrv_filenames) {
145 try {
146 236 ▶ if ($abbrv_file =~ m/\/(\w\w)\.yaml$/) {
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
if ($abbrv_file =~ m[/(\w\w)\.yaml$]) 236 0 236
147 236 my $lang = $1; # two letter lang code like 'en'
148 236 my $rh_c = LoadFile($abbrv_file);
149 236 $self->{abbreviations}->{$lang} = $rh_c;
150 }
151 } catch {
152 0 ▶ warn "error parsing abbrv configuration in $abbrv_file: $_";
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 236 };
154 }
155 #say Dumper $self->{abbreviations};
156 #say Dumper $self->{country2lang};
157 16 return 1;
158 }
159  
160  
161 sub final_components {
162 6 ▶ my $self = shift;
Subroutine final_components: called CC=3 SCAR=11.0
Pod documented
163 6 ▶ if (defined($self->{final_components})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"final_components"}) 5 1 6
164 5 return $self->{final_components};
165 }
166 1 ▶ warn 'final_components not yet set' if ($show_warnings);
Branch: 1 branch
BranchTrueFalseTotal
if $show_warnings 1 0 1
167 1 return;
168 }
169  
170  
171 sub format_address {
172 447 ▶ my $self = shift;
Subroutine format_address: called CC=41 SCAR=42.2
Pod documented
173 447 ▶ my $rh_components = clone(shift) || return;
Condition50%1/2
&clone(shift()) || (return)
l!l
4470
MC/DC0%0/1
&clone(shift()) || (return)
&clone(shift())
Truth table50%1/2
A
A= &clone(shift())
Aresult
11
00
174 447 ▶ my $rh_options = shift || {};
Condition100%2/2
shift() || {}
l!l
9438
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
175  
176 # 1. make sure empty at the beginning
177 447 $self->{final_components} = undef;
178  
179 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
180 0 say STDERR "*** in format_address ***";
181 0 say STDERR Dumper $rh_options;
182 0 say STDERR Dumper $rh_components;
183 }
184  
185 # 2. deal with the options
186  
187 # 2a. which country format will we use?
188 # might have been specified in options
189 # otherwise look at components
190 my $cc = $rh_options->{country}
191 447 ▶ || $self->_determine_country_code($rh_components)
Condition100%5/5
$rh_options->{'country'} || $self->_determine_country_code($rh_components) || ''
l!l
4434
$rh_options->{"country"} or $self->_determine_country_code($rh_components)
l!l&&r!l&&!r
14424
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
192 || '';
193  
194 447 ▶ if ($cc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc) 443 4 447
195 443 $rh_components->{country_code} = $cc;
196 443 $self->_set_district_alias($cc);
197 }
198  
199 # 2b. should we abbreviate?
200 447 ▶ my $abbrv = $rh_options->{abbreviate} // 0;
Condition100%2/2
$rh_options->{"abbreviate"} // 0
l!l
5442
MC/DC100%1/1
$rh_options->{"abbreviate"} // 0
$rh_options->{"abbreviate"}
Truth table100%2/2
A
A= $rh_options->{"abbreviate"}
Aresult
11
00
201  
202 # 2c. was only_address set at the formatting level
203 447 my $oa = $only_address;
204 447 ▶ if (defined($rh_options->{only_address})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_options->{"only_address"}) 2 445 447
205 2 $oa = $rh_options->{only_address};
206 }
207  
208 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
209 0 say STDERR "component_aliases";
210 0 say STDERR Dumper $self->{component_aliases};
211 }
212  
213 # done with the options
214  
215 # 3. set the aliases, unless this would overwrite something
216 # need to do this in the right order (as defined in the components file)
217 # For example:
218 # both 'city_district' and 'suburb' are aliases of 'neighbourhood'
219 # so which one should we use if both are present?
220 # We should use the one defined first in the list
221  
222 447 my $rhh_p2a;
223 447 foreach my $c (keys %$rh_components){
224  
225 # might not need an alias as it is a primary type
226 3518 ▶ next if (defined($self->{component_aliases}{$c}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"component_aliases"}{$c} 2793 725 3518
227  
228 # it is not a primary type
229 # is there an alias?
230 725 ▶ if (defined($self->{component2type}{$c})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"component2type"}{$c}) 492 233 725
231 492 my $ptype = $self->{component2type}{$c};
232 # but is it already set?
233 492 ▶ if (! defined($rh_components->{$ptype}) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rh_components->{$ptype}) 394 98 492
234 # no, we will set it later
235 394 $rhh_p2a->{$ptype}{$c} = 1;
236  
237 }
238 }
239 }
240  
241 # now we know which primary types have aliases
242 447 foreach my $ptype (keys %$rhh_p2a){
243 # is there more than one?
244 355 my @aliases = keys %{$rhh_p2a->{$ptype}};
245 355 ▶ if (scalar @aliases == 1){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @aliases == 1) 317 38 355
246 317 $rh_components->{$ptype} = $rh_components->{$aliases[0]};
247 317 next; # we are done with this ptype
248 }
249  
250 # if there is more than one we need to go through the list
251 # so we do them in the right order
252 38 foreach my $c (@{$self->{component_aliases}->{$ptype}}){
253 44 ▶ if (defined($rh_components->{$c})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{$c}) 38 6 44
254 38 $rh_components->{$ptype} = $rh_components->{$c};
255 38 last; # we are done with this ptype
256 }
257 }
258 }
259  
260 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
261 0 say STDERR "after component_aliases applied";
262 0 say STDERR Dumper $rh_components;
263 }
264  
265 # 4. deal wtih terrible inputs
266 447 $self->_sanity_cleaning($rh_components);
267 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
268 0 say STDERR "after sanity_cleaning applied";
269 0 say STDERR Dumper $rh_components;
270 }
271  
272 # 5. determine the template
273 447 my $template_text;
274 447 ▶ 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
44160
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
275
276 447 ▶ if (defined($rh_options->{address_template})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_options->{'address_template'}) { } 2 445 447
277 2 $template_text = $rh_options->{address_template};
278 }
279 else {
280  
281 445 ▶ if (defined($rh_config->{address_template})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rh_config->{'address_template'}) { } 443 2 445
elsif (defined $self->{'templates'}{'default'}{'address_template'}) { } 2 0 2
282 443 $template_text = $rh_config->{address_template};
283 } elsif (defined($self->{templates}{default}{address_template})) {
284 2 $template_text = $self->{templates}{default}{address_template};
285 }
286
287 # do we have the minimal components for an address?
288 # or should we instead use the fallback template?
289 445 ▶ if (!$self->_minimal_components($rh_components)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_minimal_components($rh_components)) 47 398 445
290 47 ▶ say STDERR "using fallback" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 47 47
291 47 ▶ if (defined($rh_config->{fallback_template})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $rh_config->{'fallback_template'}) { } 32 15 47
elsif (defined $self->{'templates'}{'default'}{'fallback_template'}) { } 14 1 15
292 32 $template_text = $rh_config->{fallback_template};
293 } elsif (defined($self->{templates}{default}{fallback_template})) {
294 14 $template_text = $self->{templates}{default}{fallback_template};
295 }
296 # no fallback
297 }
298  
299 }
300  
301 447 ▶ say STDERR 'template text: ' . $template_text if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 447 447
302  
303 # 6. clean up the components, possibly add codes
304 447 $self->_fix_country($rh_components);
305 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
306 0 say STDERR "after fix_country";
307 0 say STDERR Dumper $rh_components;
308 }
309  
310 447 $self->_apply_replacements($rh_components, $rh_config->{replace});
311 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
312 0 say STDERR "after applying_replacements applied";
313 0 say STDERR Dumper $rh_components;
314 }
315 447 $self->_add_state_code($rh_components);
316 447 $self->_add_county_code($rh_components);
317 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
318 0 say STDERR "after adding codes";
319 0 say STDERR Dumper $rh_components;
320 }
321  
322 # 7. add the attention, if needed
323 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
324 0 say STDERR "object level only_address: $only_address";
325 0 say STDERR "formatting level only_address: $oa";
326 }
327  
328 447 ▶ if ($oa){
Branch: 1 branch
BranchTrueFalseTotal
if ($oa) { } 3 444 447
329 3 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 3 3
330 0 say STDERR "not looking for unknown_components";
331 0 say STDERR "only_address was specified";
332 }
333 }
334 else {
335 444 my $ra_unknown = $self->_find_unknown_components($rh_components);
336 444 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 444 444
337 0 say STDERR "unknown_components:";
338 0 say STDERR Dumper $ra_unknown;
339 }
340 444 ▶ if (scalar(@$ra_unknown)){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$ra_unknown) 223 221 444
341 $rh_components->{attention} =
342 223 join(', ', map { $rh_components->{$_} } @$ra_unknown);
343 223 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 223 223
344 0 say STDERR "putting unknown_components in 'attention'";
345 }
346 }
347 }
348  
349 # 8. abbreviate, if needed
350 447 ▶ if ($abbrv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abbrv) 5 442 447
351 5 $rh_components = $self->_abbreviate($rh_components);
352 }
353  
354 # 9. prepare the template
355 447 $template_text = $self->_replace_template_lambdas($template_text);
356  
357 # 10. compiled the template
358 447 my $compiled_template =
359 $THC->compile($template_text, {'numeric_string_as_string' => 1});
360  
361 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
362 0 say STDERR "before _render_template";
363 0 say STDERR Dumper $rh_components;
364 0 say STDERR "template: ";
365 0 say STDERR Dumper $compiled_template;
366 }
367  
368 # 11. render the template
369 447 my $text = $self->_render_template($compiled_template, $rh_components);
370 447 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 447 447
371 0 say STDERR "text after _render_template $text";
372 }
373  
374 # 11. postformatting
375 447 $text = $self->_postformat($text, $rh_config->{postformat_replace});
376  
377 # 12. clean again
378 447 $text = $self->_clean($text);
379  
380 # 13. set final components
381 447 $self->{final_components} = $rh_components;
382  
383 # all done
384 447 return $text;
385 }
386  
387 # remove duplicates ("Berlin, Berlin"), do replacements and similar
388 sub _postformat {
389 449 ▶ my $self = shift;
Subroutine _postformat: called CC=6 SCAR=17.9
390 449 my $text = shift;
391 449 my $raa_rules = shift;
392  
393 449 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 449 449
394 0 say STDERR "entering _postformat: $text"
395 }
396  
397 # remove duplicates
398 449 my @before_pieces = split(/, /, $text);
399 449 my %seen;
400 my @after_pieces;
401 449 foreach my $piece (@before_pieces) {
402 555 $piece =~ s/^\s+//g;
403 555 $seen{$piece}++;
404 555 ▶ if (lc($piece) ne 'new york') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $piece ne "new york") 551 4 555
405 551 ▶ next if ($seen{$piece} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$piece} > 1 1 550 551
406 }
407 554 push(@after_pieces, $piece);
408 }
409 449 $text = join(', ', @after_pieces);
410  
411 # do any country specific rules
412 449 foreach my $ra_fromto (@$raa_rules) {
413 try {
414 491 ▶ my $regexp = qr/$ra_fromto->[0]/;
Subroutine __ANON__: called CC=3 SCAR=11.0
415 491 my $replacement = $ra_fromto->[1];
416  
417 # ultra hack to do substitution
418 # limited to $1 and $2, should really be a while loop
419 # doing every substitution
420  
421 491 ▶ if ($replacement =~ m/\$\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replacement =~ /\$\d/) 45 446 491
422 45 ▶ if ($text =~ m/$regexp/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$regexp/) 20 25 45
423 20 my $tmp1 = $1;
424 20 my $tmp2 = $2;
425 20 my $tmp3 = $3;
426 20 $replacement =~ s/\$1/$tmp1/;
427 20 $replacement =~ s/\$2/$tmp2/;
428 20 $replacement =~ s/\$3/$tmp3/;
429 }
430 }
431 491 $text =~ s/$regexp/$replacement/;
432 } catch {
433 0 ▶ warn "invalid replacement: " . join(', ', @$ra_fromto);
Subroutine __ANON__: not called CC=1 SCAR=6.9
434 491 };
435 }
436 449 return $text;
437 }
438  
439 sub _sanity_cleaning {
440 448 ▶ my $self = shift;
Subroutine _sanity_cleaning: called CC=10 SCAR=23.1
441 448 ▶ my $rh_components = shift || return;
Condition50%1/2
shift() || (return)
l!l
4480
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
442  
443 # catch insane postcodes
444 448 ▶ if (defined($rh_components->{'postcode'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{"postcode"}) 308 140 448
445 308 ▶ if (length($rh_components->{'postcode'}) > 20) {
Branch: 3 branches
BranchTrueFalseTotal
if (length $rh_components->{'postcode'} > 20) { } 1 307 308
elsif ($rh_components->{'postcode'} =~ /\d+;\d+/) { } 1 306 307
elsif ($rh_components->{'postcode'} =~ /^(\d{5}),\d{5}/) { } 1 305 306
446 1 delete $rh_components->{'postcode'};
447 } elsif ($rh_components->{'postcode'} =~ m/\d+;\d+/) {
448 # sometimes OSM has postcode ranges
449 1 delete $rh_components->{'postcode'};
450 } elsif ($rh_components->{'postcode'} =~ m/^(\d{5}),\d{5}/) {
451 1 $rh_components->{'postcode'} = $1;
452 }
453 }
454  
455 # remove things that might be empty
456 448 foreach my $c (keys %$rh_components) {
457 # catch empty values
458 3873 ▶ if (!defined($rh_components->{$c})) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $rh_components->{$c}) { } 0 3873 3873
elsif (not $rh_components->{$c} =~ /\w/) { } 1 3872 3873
elsif ($rh_components->{$c} =~ m[https?://]) { } 1 3871 3872
459 0 delete $rh_components->{$c};
460 }
461 # no chars
462 elsif ($rh_components->{$c} !~ m/\w/) {
463 1 delete $rh_components->{$c};
464 }
465 # catch values containing URLs
466 elsif ($rh_components->{$c} =~ m|https?://|) {
467 1 delete $rh_components->{$c};
468 }
469 }
470 448 return;
471 }
472  
473 sub _minimal_components {
474 445 ▶ my $self = shift;
Subroutine _minimal_components: called CC=5 SCAR=16.1
475 445 ▶ my $rh_components = shift || return;
Condition50%1/2
shift() || (return)
l!l
4450
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
476 445 my @required_components = qw(road postcode); #FIXME - should be in conf
477 445 my $missing = 0; # number of required components missing
478  
479 445 my $minimal_threshold = 2;
480 445 foreach my $c (@required_components) {
481 890 ▶ $missing++ if (!defined($rh_components->{$c}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rh_components->{$c} 200 690 890
482 890 ▶ return 0 if ($missing == $minimal_threshold);
Branch: 1 branch
BranchTrueFalseTotal
if $missing == $minimal_threshold 47 843 890
483 }
484 398 return 1;
485 }
486  
487 6 my %valid_replacement_components = ('state' => 1,);
488  
489 # determines which country code to use
490 # may also override other configuration if we are dealing with
491 # a dependent territory
492 sub _determine_country_code {
493 448 ▶ my $self = shift;
Subroutine _determine_country_code: called CC=18 SCAR=29.6
494 448 ▶ my $rh_components = shift || return;
Condition50%1/2
shift() || (return)
l!l
4480
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
495  
496 # FIXME - validate it is a valid country
497 448 ▶ return if (!defined($rh_components->{country_code}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rh_components->{"country_code"} 4 444 448
498  
499 444 ▶ if (my $cc = lc($rh_components->{country_code})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cc = lc $rh_components->{"country_code"}) 444 0 444
500  
501 # is it two letters long?
502 444 ▶ return if ($cc !~ m/^[a-z][a-z]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $cc =~ /^[a-z][a-z]$/ 0 444 444
503 444 ▶ return 'GB' if ($cc eq 'uk');
Branch: 1 branch
BranchTrueFalseTotal
if $cc eq "uk" 0 444 444
504  
505 444 $cc = uc($cc);
506  
507 # check if the configuration tells us to use
508 # the configuration of another country
509 # used in cases of dependent territories like
510 # American Samoa (AS) and Puerto Rico (PR)
511 444 ▶ if ( defined($self->{templates}{$cc})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"templates"}{$cc} and defined $self->{"templates"}{$cc}{"use_country"}) 49 395 444
Condition100%3/3
defined $self->{"templates"}{$cc} and defined $self->{"templates"}{$cc}{"use_country"}
!ll&&!rl&&r
239349
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
512 && defined($self->{templates}{$cc}{use_country}))
513 {
514 49 my $old_cc = $cc;
515 49 $cc = $self->{templates}{$cc}{use_country};
516 49 ▶ if (defined($self->{templates}{$old_cc}{change_country})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"templates"}{$old_cc}{"change_country"}) 36 13 49
517  
518 36 my $new_country = $self->{templates}{$old_cc}{change_country};
519 36 ▶ if ($new_country =~ m/\$(\w*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_country =~ /\$(\w*)/) 2 34 36
520 2 my $component = $1;
521 2 ▶ if (defined($rh_components->{$component})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{$component}) { } 2 0 2
522 2 $new_country =~ s/\$$component/$rh_components->{$component}/;
523 } else {
524 0 $new_country =~ s/\$$component//;
525 }
526 }
527 36 $rh_components->{country} = $new_country;
528 }
529 49 ▶ if (defined($self->{templates}{$old_cc}{add_component})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"templates"}{$old_cc}{"add_component"}) 12 37 49
530 12 my $tmp = $self->{templates}{$old_cc}{add_component};
531 12 my ($k, $v) = split(/=/, $tmp);
532 # check whitelist of valid replacement components
533 12 ▶ if (defined($valid_replacement_components{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid_replacement_components{$k}) 11 1 12
534 11 $rh_components->{$k} = $v;
535 }
536 }
537 }
538  
539 444 ▶ if ($cc eq 'NL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq "NL") 5 439 444
540 5 ▶ if (defined($rh_components->{state})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{"state"}) 5 0 5
541 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
542 1 $cc = 'CW';
543 1 $rh_components->{country} = 'Curaçao';
544 } elsif ($rh_components->{state} =~ m/^sint maarten/i) {
545 0 $cc = 'SX';
546 0 $rh_components->{country} = 'Sint Maarten';
547 } elsif ($rh_components->{state} =~ m/^Aruba/i) {
548 1 $cc = 'AW';
549 1 $rh_components->{country} = 'Aruba';
550 }
551 }
552 }
553 444 return $cc;
554 }
555 0 return;
556 }
557  
558 # hacks for bad country data
559 sub _fix_country {
560 447 ▶ my $self = shift;
Subroutine _fix_country: called CC=5 SCAR=16.2
561 447 ▶ my $rh_components = shift || return;
Condition50%1/2
shift() || (return)
l!l
4470
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
562  
563 # is the country a number?
564 # if so, and there is a state, use state as country
565 447 ▶ if (defined($rh_components->{country})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{"country"}) 444 3 447
566 444 ▶ if (looks_like_number($rh_components->{country})) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($rh_components->{"country"})) 1 443 444
567 1 ▶ if (defined($rh_components->{state})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{"state"}) 1 0 1
568 1 $rh_components->{country} = $rh_components->{state};
569 1 delete $rh_components->{state};
570 }
571 }
572 }
573 447 return;
574 }
575  
576 # sets and returns a state code
577 # note may also set other values in some odd edge cases
578 sub _add_state_code {
579 452 ▶ my $self = shift;
Subroutine _add_state_code: called CC=1 SCAR=0.0
580 452 my $rh_components = shift;
581 452 return $self->_add_code('state', $rh_components);
582 }
583  
584 sub _add_county_code {
585 448 ▶ my $self = shift;
Subroutine _add_county_code: called CC=1 SCAR=0.0
586 448 my $rh_components = shift;
587 448 return $self->_add_code('county', $rh_components);
588 }
589  
590 sub _add_code {
591 900 ▶ my $self = shift;
Subroutine _add_code: called CC=19 SCAR=30.1
592 900 ▶ my $keyname = shift // return;
Condition50%1/2
shift() // (return)
l!l
9000
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
593 900 my $rh_components = shift;
594 900 ▶ return if !$rh_components->{country_code}; # do we know country?
Branch: 1 branch
BranchTrueFalseTotal
unless $rh_components->{"country_code"} 9 891 900
595 891 ▶ return if !$rh_components->{$keyname}; # do we know state/county?
Branch: 1 branch
BranchTrueFalseTotal
unless $rh_components->{$keyname} 335 556 891
596  
597 556 my $code = $keyname . '_code';
598  
599 556 ▶ if (defined($rh_components->{$code})) { # do we already have code?
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_components->{$code}) 22 534 556
600 # but could have situation
601 # where code and long name are
602 # the same which we want to correct
603 22 ▶ if ($rh_components->{$code} ne $rh_components->{$keyname}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_components->{$code} ne $rh_components->{$keyname}) 21 1 22
604 21 return;
605 }
606 }
607  
608 # ensure country_code is uppercase as we use it as conf key
609 535 $rh_components->{country_code} = uc($rh_components->{country_code});
610 535 my $cc = $rh_components->{country_code};
611  
612 535 ▶ if (my $mapping = $self->{$code . 's'}{$cc}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mapping = $$self{$code . 's'}{$cc}) 280 255 535
613  
614 280 my $name = $rh_components->{$keyname};
615 280 my $uc_name = uc($name);
616  
617 280 LOCCODE: foreach my $abbrv (keys %$mapping) {
618  
619 6652 my @confnames; # can have multiple names for the place
620 # for example in different languages
621  
622 6652 ▶ if (is_hashref($mapping->{$abbrv})) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_hashref($mapping->{$abbrv})) { } 883 5769 6652
623 883 push(@confnames, values %{$mapping->{$abbrv}});
624 } else {
625 5769 push(@confnames, $mapping->{$abbrv});
626 }
627  
628 6652 foreach my $confname (@confnames) {
629 7607 ▶ if ($uc_name eq uc($confname)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uc_name eq uc $confname) 178 7429 7607
630 178 $rh_components->{$code} = $abbrv;
631 178 last LOCCODE;
632 }
633 # perhaps instead of passing in a name, we passed in a code
634 # example: state => 'NC'
635 # we want to turn that into
636 # state => 'North Carolina'
637 # state_code => 'NC'
638 #
639 7429 ▶ if ($uc_name eq $abbrv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uc_name eq $abbrv) 6 7423 7429
640 6 $rh_components->{$keyname} = $confname;
641 6 $rh_components->{$code} = $abbrv;
642 6 last LOCCODE;
643 }
644 }
645 }
646 # didn't find a valid code or name
647  
648 # try again for odd variants like "United States Virgin Islands"
649 280 ▶ if ($keyname eq 'state') {
Branch: 1 branch
BranchTrueFalseTotal
if ($keyname eq "state") 252 28 280
650 252 ▶ if (!defined($rh_components->{state_code})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rh_components->{"state_code"}) 80 172 252
651 80 ▶ if ($cc eq 'US') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cc eq "US") 2 78 80
652 2 ▶ if ($rh_components->{state} =~ m/^united states/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_components->{"state"} =~ /^united states/i) 0 2 2
653 0 my $state = $rh_components->{state};
654 0 $state =~ s/^United States/US/i;
655 0 foreach my $k (keys %$mapping) {
656 0 ▶ if (uc($state) eq uc($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $state eq uc $k) 0 0 0
657 0 $rh_components->{state_code} = $mapping->{$k};
658 0 last;
659 }
660 }
661 }
662 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
663 2 $rh_components->{state_code} = 'DC';
664 2 $rh_components->{state} = 'District of Columbia';
665 2 $rh_components->{city} = 'Washington';
666 }
667 }
668 }
669 }
670 }
671 535 return $rh_components->{$code};
672 }
673  
674 sub _apply_replacements {
675 450 ▶ my $self = shift;
Subroutine _apply_replacements: called CC=4 SCAR=13.9
676 450 my $rh_components = shift;
677 450 my $raa_rules = shift;
678  
679 450 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 450 450
680 0 say STDERR "in _apply_replacements";
681 0 say STDERR Dumper $raa_rules;
682 }
683  
684 450 foreach my $component (keys %$rh_components) {
685 3872 foreach my $ra_fromto (@$raa_rules) {
686 try {
687 # do key specific replacement
688 7827 ▶ if ($ra_fromto->[0] =~ m/^$component=/) {
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if ($ra_fromto->[0] =~ /^$component=/) { } 79 7748 7827
689 79 my $from = $ra_fromto->[0];
690 79 $from =~ s/^$component=//;
691 79 ▶ if ($rh_components->{$component} eq $from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rh_components->{$component} eq $from) 5 74 79
692 5 $rh_components->{$component} = $ra_fromto->[1];
693 }
694 } else {
695 7748 my $regexp = qr/$ra_fromto->[0]/;
696 7747 $rh_components->{$component} =~ s/$regexp/$ra_fromto->[1]/;
697 }
698 } catch {
699 1 ▶ warn "invalid replacement: " . join(', ', @$ra_fromto);
Subroutine __ANON__: called CC=1 SCAR=0.0
700 7827 };
701 }
702 }
703 450 return $rh_components;
704 }
705  
706 sub _abbreviate {
707 7 ▶ my $self = shift;
Subroutine _abbreviate: called CC=11 SCAR=24.1
708 7 ▶ my $rh_comp = shift // return;
Condition50%1/2
shift() // (return)
l!l
70
MC/DC0%0/1
shift() // (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
709  
710 # do we know the country?
711 7 ▶ if (!defined($rh_comp->{country_code})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rh_comp->{"country_code"}) 2 5 7
712 2 ▶ if ($show_warnings){
Branch: 1 branch
BranchTrueFalseTotal
if ($show_warnings) 1 1 2
713 1 my $error_msg = 'no country_code, unable to abbreviate';
714 1 ▶ if (defined($rh_comp->{country})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rh_comp->{"country"}) 1 0 1
715 1 $error_msg .= ' - country: ' . $rh_comp->{country};
716 }
717 1 warn $error_msg
718 }
719 2 return;
720 }
721  
722 # do we have abbreviations for this country?
723 5 my $cc = uc($rh_comp->{country_code});
724  
725 # 1. which languages?
726 5 ▶ if (defined($self->{country2lang}{$cc})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"country2lang"}{$cc}) 5 0 5
727  
728 5 my @langs = split(/,/, $self->{country2lang}{$cc});
729  
730 5 foreach my $lang (@langs) {
731 # do we have abbrv for this lang?
732 9 ▶ if (defined($self->{abbreviations}->{$lang})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'abbreviations'}{$lang}) { } 6 3 9
733  
734 6 my $rh_abbr = $self->{abbreviations}->{$lang};
735 6 foreach my $comp_name (keys %$rh_abbr) {
736 10 ▶ next if (!defined($rh_comp->{$comp_name}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rh_comp->{$comp_name} 0 10 10
737 10 foreach my $long (keys %{$rh_abbr->{$comp_name}}) {
738 62 my $short = $rh_abbr->{$comp_name}->{$long};
739 62 $rh_comp->{$comp_name} =~ s/\b$long\b/$short/;
740 }
741 }
742 } else {
743 #warn "no abbreviations defined for lang $lang";
744 }
745 }
746 }
747  
748 5 return $rh_comp;
749 }
750  
751 # " abc,,def , ghi " => 'abc, def, ghi'
752 sub _clean {
753 900 ▶ my $self = shift;
Subroutine _clean: called CC=9 SCAR=22.0
754 900 ▶ my $out = shift // return;
Condition100%2/2
shift() // (return)
l!l
8991
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
755 899 ▶ if ($debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 0 899 899
756 0 say STDERR "entering _clean \n$out";
757 }
758  
759 899 $out =~ s/\&#39\;/'/g;
760  
761 899 $out =~ s/[\},\s]+$//;
762 899 $out =~ s/^[,\s]+//;
763  
764 899 $out =~ s/^- //; # line starting with dash due to a parameter missing
765  
766 899 $out =~ s/,\s*,/, /g; # multiple commas to one
767 899 $out =~ s/\h+,\h+/, /g; # one horiz whitespace behind comma
768 899 $out =~ s/\h\h+/ /g; # multiple horiz whitespace to one
769 899 $out =~ s/\h\n/\n/g; # horiz whitespace, newline to newline
770 899 $out =~ s/\n,/\n/g; # newline comma to just newline
771 899 $out =~ s/,,+/,/g; # multiple commas to one
772 899 $out =~ s/,\n/\n/g; # comma newline to just newline
773 899 $out =~ s/\n\h+/\n/g; # newline plus space to newline
774 899 $out =~ s/\n\n+/\n/g; # multiple newline to one
775  
776 # final dedupe across and within lines
777 899 my @before_pieces = split(/\n/, $out);
778 899 my %seen_lines;
779 my @after_pieces;
780 899 foreach my $line (@before_pieces) {
781 3477 $line =~ s/^\h+//g;
782 3477 $line =~ s/\h+$//g;
783 3477 $seen_lines{$line}++;
784 3477 ▶ next if ($seen_lines{$line} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seen_lines{$line} > 1 18 3459 3477
785 # now dedupe within the line
786 3459 my @before_words = split(/,/, $line);
787 3459 my %seen_words;
788 my @after_words;
789 3459 foreach my $w (@before_words) {
790 3659 $w =~ s/^\h+//g;
791 3659 $w =~ s/\h+$//g;
792 3659 ▶ if (lc($w) ne 'new york') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $w ne "new york") 3649 10 3659
793 3649 $seen_words{$w}++;
794 }
795 3659 ▶ next if ((defined($seen_words{$w})) && ($seen_words{$w} > 1));
Branch: 1 branch
BranchTrueFalseTotal
if defined $seen_words{$w} and $seen_words{$w} > 1 1 3658 3659
Condition100%3/3
defined $seen_words{$w} and $seen_words{$w} > 1
!ll&&!rl&&r
1036481
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
796 3658 push(@after_words, $w);
797 }
798 3459 $line = join(', ', @after_words);
799 3459 push(@after_pieces, $line);
800 }
801 899 $out = join("\n", @after_pieces);
802  
803 899 $out =~ s/^\s+//; # remove leading whitespace
804 899 $out =~ s/\s+$//; # remove end whitespace
805  
806 899 $out .= "\n"; # add final newline
807 899 return $out; # we are done
808 }
809  
810 sub _render_template {
811 448 ▶ my $self = shift;
Subroutine _render_template: called CC=6 SCAR=18.5
812 448 my $thtemplate = shift;
813 448 my $components = shift;
814  
815 # Mustache calls it context
816 448 my $context = clone($components);
817 448 ▶ say STDERR 'context: ' . Dumper $context if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 448 448
818 448 my $output = $thtemplate->render($context);
819  
820 448 $output = $self->_evaluate_template_lamdas($output);
821  
822 448 ▶ say STDERR "in _render before _clean: $output" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 448 448
823 448 $output = $self->_clean($output);
824  
825 # is it empty?
826 # if yes and there is only one component then just use that one
827 448 ▶ if ($output !~ m/\w/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output =~ /\w/) 2 446 448
828 2 my @comps = sort keys %$components;
829 2 ▶ if (scalar(@comps) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @comps == 1) 0 2 2
830 0 foreach my $k (@comps) {
831 0 $output = $components->{$k};
832 }
833 } # FIXME what if more than one?
834 }
835 448 return $output;
836 }
837  
838 # Text::Hogan apparently caches lambdas when rendering templates. In the past
839 # we needed our lambda 'first', example
840 # {{#first}} {{{city}}} || {{{town}}} {{/first}}
841 # to evaluate the componentes. Whenever the lambda was called with different
842 # component values it consumed memory. Now replace with a simpler implementation
843 #
844 sub _replace_template_lambdas {
845 448 ▶ my $self = shift;
Subroutine _replace_template_lambdas: called CC=1 SCAR=0.0
846 448 my $template_text = shift;
847 448 $template_text =~ s!\Q{{#first}}\E(.+?)\Q{{/first}}\E!FIRSTSTART${1}FIRSTEND!g;
848 448 return $template_text;
849 }
850  
851 # We only use a lambda named 'first'
852 sub _evaluate_template_lamdas {
853 448 ▶ my $self = shift;
Subroutine _evaluate_template_lamdas: called CC=1 SCAR=0.0
854 448 my $text = shift;
855 448 $text =~ s!FIRSTSTART\s*(.+?)\s*FIRSTEND!_select_first($1)!seg;
856 448 return $text;
857 }
858  
859 # '|| val1 || || val3' => 'val1'
860 sub _select_first {
861 750 ▶ my $text = shift;
Subroutine _select_first: called CC=2 SCAR=6.9
862 750 my @a_parts = grep { length($_) } split(/\s*\|\|\s*/, $text);
863 750 ▶ return scalar(@a_parts) ? $a_parts[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
scalar @a_parts ? : 663 87 750
864 }
865  
866 6 my %small_district = (
867 'br' => 1,
868 'cr' => 1,
869 'es' => 1,
870 'ni' => 1,
871 'py' => 1,
872 'ro' => 1,
873 'tg' => 1,
874 'tm' => 1,
875 'xk' => 1,
876 );
877  
878 # correct the alias for "district"
879 # in OSM some countries use district to mean "city_district"
880 # others to mean "state_district"
881 sub _set_district_alias {
882 443 ▶ my $self = shift;
Subroutine _set_district_alias: called CC=2 SCAR=7.3
883 443 my $cc = shift;
884  
885 443 my $oldalias;
886 443 ▶ if (defined($small_district{$cc})){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $small_district{$cc}) { } 0 443 443
887 0 $self->{component2type}{district} = 'neighbourhood';
888 0 $oldalias = 'state_district';
889  
890 # add to the neighbourhood alias list
891 # though of course we are just sticking it at the end
892 0 push(@{$self->{component_aliases}{'neighbourhood'}}, 'district');
893  
894 } else {
895 # set 'district' to be type 'state_district'
896 443 $self->{component2type}{district} = 'state_district';
897 443 $oldalias = 'neighbourhood';
898  
899 # add to the state_district alias list
900 443 push(@{$self->{component_aliases}{'state_district'}}, 'district');
901 }
902  
903 # remove from the old alias list
904 443 my @temp = grep { $_ ne 'district' } @{$self->{component_aliases}{$oldalias}};
905 443 $self->{component_aliases}{$oldalias} = \@temp;
906  
907 443 return;
908 }
909  
910  
911 # returns []
912 sub _find_unknown_components {
913 445 ▶ my $self = shift;
Subroutine _find_unknown_components: called CC=1 SCAR=0.0
914 445 my $rh_components = shift;
915  
916 445 my %h_known = map { $_ => 1 } @{$self->{ordered_components}};
917 445 my @a_unknown = grep { !exists($h_known{$_}) } sort keys %$rh_components;
918  
919 445 return \@a_unknown;
920 }
921  
922 6 1;
923  
924 __END__
925  
926 =pod
927  
928 =encoding UTF-8
929  
930 =head1 NAME
931  
932 Geo::Address::Formatter - take structured address data and format it according to the various global/country rules
933  
934 =head1 VERSION
935  
936 version 1.994
937  
938 =head1 SYNOPSIS
939  
940 #
941 # get the templates (or use your own)
942 # git clone git@github.com:OpenCageData/address-formatting.git
943 #
944 my $GAF = Geo::Address::Formatter->new( conf_path => '/path/to/templates' );
945  
946 my $components = { ... }
947 my $text = $GAF->format_address($components, { country => 'FR' } );
948 my $rh_final_components = $GAF->final_components();
949 #
950 # or if we want shorter output
951 #
952 my $short_text = $GAF->format_address($components, { country => 'FR', abbreviate => 1, });
953  
954 =head2 new
955  
956 my $GAF = Geo::Address::Formatter->new( conf_path => '/path/to/templates' );
957  
958 Returns one instance. The I<conf_path> is required.
959  
960 Optional parameters are:
961  
962 I<debug>: prints tons of debugging info for use in development.
963  
964 I<no_warnings>: turns off a few warnings if configuration is not optimal.
965  
966 I<only_address>: formatted will only contain known components (will not include POI names like). Note, can be overridden with optional param to format_address method.
967  
968 =head2 final_components
969  
970 my $rh_components = $GAF->final_components();
971  
972 returns a reference to a hash of the final components that are set at the
973 completion of B<format_address>. Warns if called before they have been set
974 (unless I<no_warnings> was set at object creation).
975  
976 =head2 format_address
977  
978 my $text = $GAF->format_address(\%components, \%options );
979  
980 Given a structures address (hashref) and options (hashref) returns a
981 formatted address.
982  
983 Possible options are:
984  
985 'abbreviate', if supplied common abbreviations are applied
986 to the resulting output.
987  
988 'address_template', a mustache format template to be used instead of the template
989 defined in the configuration
990  
991 'country', which should be an uppercase ISO 3166-1:alpha-2 code
992 e.g. 'GB' for Great Britain, 'DE' for Germany, etc.
993 If ommited we try to find the country in the address components.
994  
995 'only_address', same as only_address global option but set at formatting level
996  
997 =head1 DESCRIPTION
998  
999 You have a structured postal address (hash) and need to convert it into a
1000 readable address based on the format of the address country.
1001  
1002 For example, you have:
1003  
1004 {
1005 house_number => 12,
1006 street => 'Avenue Road',
1007 postcode => 45678,
1008 city => 'Deville'
1009 }
1010  
1011 you need:
1012  
1013 Great Britain: 12 Avenue Road, Deville 45678
1014 France: 12 Avenue Road, 45678 Deville
1015 Germany: Avenue Road 12, 45678 Deville
1016 Latvia: Avenue Road 12, Deville, 45678
1017  
1018 It gets more complicated with 200+ countries and territories and dozens more
1019 address components to consider.
1020  
1021 This module comes with a minimal configuration to run tests. Instead of
1022 developing your own configuration please use (and contribute to)
1023 those in https://github.com/OpenCageData/address-formatting
1024 which includes test cases.
1025  
1026 Together we can address the world!
1027  
1028 =head1 AUTHOR
1029  
1030 Ed Freyfogle
1031  
1032 =head1 COPYRIGHT AND LICENSE
1033  
1034 This software is copyright (c) 2023 by Opencage GmbH.
1035  
1036 This is free software; you can redistribute it and/or modify it under
1037 the same terms as the Perl 5 programming language system itself.
1038  
1039 =cut
« blib/lib/Geo/Address/Formatter.pm ↑ Summary