blib/lib/Geo/Address/Formatter.pm untested

filter: Statementstmt 0.0% 0 / 621 Branchbran 0.0% 0 / 274 Conditioncond 0.0% 0 / 81 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 1038 CC 138 SCAR 57.7
CC
138
Cov
0%
format_address
1190.0
_determine_country_code
272.0
_read_configuration
240.0
CRAP 319.6
1 package Geo::Address::Formatter;
2 $Geo::Address::Formatter::VERSION = '1.9993';
3 # ABSTRACT: take structured address data and format it according to the various global/country rules
4  
5 use strict;
6 use warnings;
7 use feature qw(say);
8 use Clone qw(clone);
9 use Data::Dumper;
10 $Data::Dumper::Sortkeys = 1;
11 use File::Basename qw(dirname);
12 use Ref::Util qw(is_hashref);
13 use Scalar::Util qw(looks_like_number);
14 use Text::Hogan::Compiler;
15 use Try::Catch;
16 use YAML::XS qw(LoadFile);
17 use utf8;
18  
19 my $THC = Text::Hogan::Compiler->new;
20  
21 # optional params
22 my $show_warnings = 1;
23 my $debug = 0;
24 my $only_address = 0;
25  
26  
27  
28  
29 my $instance;
30  
31 sub instance {
32 my ($class, %params) = @_;
33
34 unless ($instance) {
35 $instance = $class->new(%params);
36 }
37  
38 say STDERR "************* in Geo::Address::Formatter::instance ***" if ($debug);
39
40 # clear these fields that change on each run
41 $instance->{final_components} = undef;
42 $instance->{set_district_alias} = {};
43  
44 return $instance;
45 }
46  
47 sub new {
48 my ($class, %params) = @_;
49  
50 my $self = {};
51 my $conf_path = $params{conf_path} || die "no conf_path set";
52  
53 # optional params
54 if ( defined($params{no_warnings}) && ($params{no_warnings})){
55 $show_warnings = 0;
56 }
57 $only_address = (defined($params{only_address}) && $params{only_address}) // 0;
58 $debug = (defined($params{debug}) && $params{debug}) // 0;
59  
60 $self->{final_components} = undef;
61 $self->{set_district_alias} = {};
62  
63 bless($self, $class);
64  
65 say STDERR "************* in Geo::Address::Formatter::new ***" if ($debug);
66  
67 # is slow because lots of conf, and pre-compiling
68 if ($self->_read_configuration($conf_path)){
69 return $self;
70 }
71 die 'unable to read configuration';
72 }
73  
74 sub _read_configuration {
75 my $self = shift;
76 my $path = shift;
77  
78 return if (! -e $path);
79 # components file
80 my $compyaml = $path . '/components.yaml';
81 return if (! -e $compyaml);
82  
83 $self->{templates} = {};
84 $self->{component_aliases} = {};
85 $self->{component2type} = {};
86 $self->{ordered_components} = [];
87  
88 # read the config file(s)
89 my $loaded = 0;
90  
91 my $wwfile = $path . '/countries/worldwide.yaml';
92 say STDERR "loading templates $wwfile" if ($debug);
93  
94 return if (! -e $wwfile);
95  
96 try {
97 my $rh_templates = LoadFile($wwfile);
98 foreach (keys %$rh_templates) {
99 $self->{templates}{$_} = $rh_templates->{$_};
100 $loaded = 1;
101 }
102 } catch {
103 warn "error parsing country configuration in $wwfile";
104 return;
105 };
106 return if ($loaded == 0); # no templates
107  
108 # see if we can load the components
109 try {
110 say STDERR "loading components" if ($debug);
111 my @c = LoadFile($compyaml);
112  
113 if ($debug){
114 say STDERR Dumper \@c;
115 }
116  
117 foreach my $rh_c (@c) {
118 if (defined($rh_c->{name})){
119 if (defined($rh_c->{aliases})){
120 $self->{component_aliases}{$rh_c->{name}} = $rh_c->{aliases};
121 } else {
122 $self->{component_aliases}{$rh_c->{name}} = [];
123 }
124 }
125 }
126  
127 foreach my $rh_c (@c) {
128 push(@{$self->{ordered_components}}, $rh_c->{name});
129 $self->{component2type}->{$rh_c->{name}} = $rh_c->{name};
130  
131 if (defined($rh_c->{aliases})) {
132 foreach my $alias (@{$rh_c->{aliases}}) {
133 push(@{$self->{ordered_components}}, $alias);
134 $self->{component2type}->{$alias} = $rh_c->{name};
135 }
136 }
137 }
138 my %h_known = map { $_ => 1 } @{$self->{ordered_components}};
139 $self->{h_known} = \%h_known;
140  
141 if ($debug){
142 say STDERR 'component_aliases';
143 say STDERR Dumper $self->{component_aliases};
144 say STDERR 'component2type';
145 say STDERR Dumper $self->{component2type};
146 }
147 } catch {
148 warn "error parsing component configuration: $_";
149 };
150  
151 # get the county and state codes and country2lang conf
152 my @conf_files = qw(county_codes state_codes country2lang);
153 foreach my $cfile (@conf_files) {
154 $self->{$cfile} = {};
155 my $yfile = $path . '/' . $cfile . '.yaml';
156 if (-e $yfile) {
157 try {
158 $self->{$cfile} = LoadFile($yfile);
159 } catch {
160 warn "error parsing $cfile configuration: $_";
161 };
162 }
163 }
164  
165 my $abbrvdir = $path . '/abbreviations';
166  
167 if (-d $abbrvdir){
168 opendir my $dh, $abbrvdir
169 or die "Could not open '$abbrvdir' for read: $!\n";
170  
171 while (my $file = readdir $dh) {
172 # say STDERR "file: $file";
173 if ($file =~ m/^(\w\w)\.yaml$/) {
174 my $lang = $1; # two letter lang code like 'en'
175 my $abbrvfile = $abbrvdir . '/' . $file;
176 try {
177 $self->{abbreviations}->{$lang} = LoadFile($abbrvfile);
178 } catch {
179 warn "error parsing abbrv conf in $abbrvfile: $_";
180 };
181 }
182 }
183 closedir $dh;
184 }
185  
186 #say Dumper $self->{abbreviations};
187 #say Dumper $self->{country2lang};
188  
189 $self->_precompile_rules();
190 $self->_build_code_lookups();
191 $self->_precompile_abbreviations();
192  
193 return 1;
194 }
195  
196 sub _precompile_rules {
197 my $self = shift;
198  
199 foreach my $cc (keys %{$self->{templates}}) {
200 my $tpl = $self->{templates}{$cc};
201 next unless is_hashref($tpl);
202  
203 # Pre-compile replace rules
204 if (defined($tpl->{replace})) {
205 my @compiled;
206 foreach my $ra_fromto (@{$tpl->{replace}}) {
207 my $pattern = $ra_fromto->[0];
208 my $replacement = $ra_fromto->[1];
209 my ($comp_name, $exact_match, $re);
210  
211 # detect component-specific rules like "state=Some Value"
212 if ($pattern =~ m/^(\w+)=(.+)$/) {
213 $comp_name = $1;
214 my $rest = $2;
215 # try to compile as regex; if it fails, it's an exact match
216 eval { $re = qr/$rest/i };
217 if ($@) {
218 warn "invalid replacement regex '$rest' for $cc, skipping";
219 next;
220 }
221 $exact_match = $rest;
222 } else {
223 eval { $re = qr/$pattern/i };
224 if ($@) {
225 warn "invalid replacement regex '$pattern' for $cc, skipping";
226 next;
227 }
228 }
229 push @compiled, {
230 component => $comp_name,
231 exact_match => $exact_match,
232 re => $re,
233 replacement => $replacement,
234 };
235 }
236 $tpl->{_compiled_replace} = \@compiled;
237 }
238  
239 # Pre-compile postformat_replace rules
240 if (defined($tpl->{postformat_replace})) {
241 my @compiled;
242 foreach my $ra_fromto (@{$tpl->{postformat_replace}}) {
243 my $pattern = $ra_fromto->[0];
244 my $replacement = $ra_fromto->[1];
245 my $re;
246 eval { $re = qr/$pattern/ };
247 if ($@) {
248 warn "invalid postformat regex '$pattern' for $cc, skipping";
249 next;
250 }
251 push @compiled, {
252 re => $re,
253 replacement => $replacement,
254 };
255 }
256 $tpl->{_compiled_postformat} = \@compiled;
257 }
258 }
259 return;
260 }
261  
262 sub _build_code_lookups {
263 my $self = shift;
264  
265 foreach my $type (qw(state_codes county_codes)) {
266 my $reverse_key = $type . '_reverse';
267 my $name_key = $type . '_name';
268 $self->{$reverse_key} = {};
269 $self->{$name_key} = {};
270  
271 my $data = $self->{$type} // next;
272 foreach my $cc (keys %$data) {
273 my $mapping = $data->{$cc};
274 my $rev = {};
275 my $name = {};
276  
277 foreach my $code (keys %$mapping) {
278 my $val = $mapping->{$code};
279 if (is_hashref($val)) {
280 # hash with default, alt, alt_XX keys
281 my $default_name = $val->{default};
282 $name->{$code} = $default_name if defined $default_name;
283 foreach my $v (values %$val) {
284 $rev->{uc($v)} = $code;
285 }
286 } else {
287 # simple string value
288 $name->{$code} = $val;
289 $rev->{uc($val)} = $code;
290 }
291 # code-to-code identity lookup
292 $rev->{$code} = $code;
293 }
294 $self->{$reverse_key}{$cc} = $rev;
295 $self->{$name_key}{$cc} = $name;
296 }
297 }
298 return;
299 }
300  
301 sub _precompile_abbreviations {
302 my $self = shift;
303 my $abbr = $self->{abbreviations} // return;
304  
305 foreach my $lang (keys %$abbr) {
306 foreach my $comp_name (keys %{$abbr->{$lang}}) {
307 my $rh_pairs = $abbr->{$lang}{$comp_name};
308 my @compiled;
309 foreach my $long (keys %$rh_pairs) {
310 push @compiled, {
311 re => qr/(^|\s)\Q$long\E\b/,
312 short => $rh_pairs->{$long},
313 };
314 }
315 $self->{compiled_abbreviations}{$lang}{$comp_name} = \@compiled;
316 }
317 }
318 return;
319 }
320  
321  
322 sub final_components {
323 my $self = shift;
324 if (defined($self->{final_components})) {
325 return $self->{final_components};
326 }
327 warn 'final_components not yet set' if ($show_warnings);
328 return;
329 }
330  
331  
332 sub format_address {
333 my $self = shift;
334 my $rh_components = clone(shift) || return;
335 my $rh_options = shift || {};
336  
337 # 1. make sure empty at the beginning
338 $self->{final_components} = undef;
339  
340 if ($debug){
341 say STDERR "*** in format_address ***";
342 say STDERR Dumper $rh_options;
343 say STDERR Dumper $rh_components;
344 }
345  
346 # 2. deal with the options
347  
348 foreach (grep { ! /^[a-z0-9_]+$/ } keys %$rh_components) {
349 if ($debug){
350 say STDERR "Skipping compontent $_";
351 }
352 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 || $self->_determine_country_code($rh_components)
360 || '';
361  
362 if ($cc) {
363 $rh_components->{country_code} = $cc;
364 $self->_set_district_alias($cc);
365 }
366  
367 # 2b. should we abbreviate?
368 my $abbrv = $rh_options->{abbreviate} // 0;
369  
370 # 2c. was only_address set at the formatting level
371 my $oa = $only_address;
372 if (defined($rh_options->{only_address})){
373 $oa = $rh_options->{only_address};
374 }
375  
376 if ($debug){
377 say STDERR "component_aliases";
378 say STDERR Dumper $self->{component_aliases};
379 }
380 # done with the options
381  
382 # 3. deal wtih terrible inputs
383 $self->_sanity_cleaning($rh_components);
384 if ($debug){
385 say STDERR "after sanity_cleaning applied";
386 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 my $rhh_p2a;
397 foreach my $c (keys %$rh_components){
398  
399 # might not need an alias as it is a primary type
400 next if (defined($self->{component_aliases}{$c}));
401  
402 # it is not a primary type
403 # is there an alias?
404 if (defined($self->{component2type}{$c})){
405 my $ptype = $self->{component2type}{$c};
406 # but is it already set?
407 if (! defined($rh_components->{$ptype}) ){
408 # no, we will set it later
409 $rhh_p2a->{$ptype}{$c} = 1;
410  
411 }
412 }
413 }
414  
415 # now we know which primary types have aliases
416 foreach my $ptype (keys %$rhh_p2a){
417 # is there more than one?
418 my @aliases = keys %{$rhh_p2a->{$ptype}};
419 if (scalar @aliases == 1){
420 $rh_components->{$ptype} = $rh_components->{$aliases[0]};
421 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 foreach my $c (@{$self->{component_aliases}->{$ptype}}){
427 if (defined($rh_components->{$c})){
428 $rh_components->{$ptype} = $rh_components->{$c};
429 last; # we are done with this ptype
430 }
431 }
432 }
433  
434 if ($debug){
435 say STDERR "after component_aliases applied";
436 say STDERR Dumper $rh_components;
437 }
438  
439 # 5. determine the template
440 my $template_text;
441 my $rh_config = $self->{templates}{uc($cc)} || $self->{templates}{default};
442
443 if (defined($rh_options->{address_template})) {
444 $template_text = $rh_options->{address_template};
445 }
446 else {
447  
448 if (defined($rh_config->{address_template})) {
449 $template_text = $rh_config->{address_template};
450 } elsif (defined($self->{templates}{default}{address_template})) {
451 $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 if (!$self->_minimal_components($rh_components)) {
457 say STDERR "using fallback" if ($debug);
458 if (defined($rh_config->{fallback_template})) {
459 $template_text = $rh_config->{fallback_template};
460 } elsif (defined($self->{templates}{default}{fallback_template})) {
461 $template_text = $self->{templates}{default}{fallback_template};
462 }
463 # no fallback
464 }
465  
466 }
467  
468 say STDERR 'template text: ' . $template_text if ($debug);
469  
470 # 6. clean up the components, possibly add codes
471 $self->_fix_country($rh_components);
472 if ($debug){
473 say STDERR "after fix_country";
474 say STDERR Dumper $rh_components;
475 }
476  
477 # apply replacements if we have any
478 if (defined($rh_config->{_compiled_replace})){
479 $self->_apply_replacements($rh_components, $rh_config->{_compiled_replace});
480 if ($debug){
481 say STDERR "after applying_replacements applied";
482 say STDERR Dumper $rh_components;
483 }
484 }
485  
486 $self->_add_state_code($rh_components);
487 $self->_add_county_code($rh_components);
488 if ($debug){
489 say STDERR "after adding codes";
490 say STDERR Dumper $rh_components;
491 }
492  
493 # 7. add the attention, if needed
494 if ($debug){
495 say STDERR "object level only_address: $only_address";
496 say STDERR "formatting level only_address: $oa";
497 }
498  
499 if ($oa){
500 if ($debug){
501 say STDERR "not looking for unknown_components";
502 say STDERR "only_address was specified";
503 }
504 }
505 else {
506 my $ra_unknown = $self->_find_unknown_components($rh_components);
507 if (scalar(@$ra_unknown)){
508 if ($debug){
509 say STDERR "unknown_components:";
510 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 join(', ', map { $rh_components->{$_} } sort @$ra_unknown);
516 if ($debug){
517 say STDERR "putting unknown_components in 'attention'";
518 }
519 }
520 }
521  
522 # 8. abbreviate, if needed
523 if ($abbrv) {
524 $rh_components = $self->_abbreviate($rh_components);
525 }
526  
527 # 9. prepare the template
528 $template_text = $self->_replace_template_lambdas($template_text);
529  
530 # 10. compiled the template
531 my $compiled_template = $self->{compiled_template_cache}{$template_text}
532 //= $THC->compile($template_text, {'numeric_string_as_string' => 1});
533  
534 if ($debug){
535 say STDERR "before _render_template";
536 say STDERR Dumper $rh_components;
537 say STDERR "template: ";
538 say STDERR Dumper $compiled_template;
539 }
540  
541  
542 # 11. render the template
543 my $text = $self->_render_template($compiled_template, $rh_components);
544 if ($debug){
545 say STDERR "text after _render_template $text";
546 }
547  
548 # 12. postformatting
549 $text = $self->_postformat($text, $rh_config->{_compiled_postformat});
550  
551 # 13. clean again
552 $text = $self->_clean($text);
553  
554 # 14. set final components (so we can get them later)
555 $self->{final_components} = $rh_components;
556  
557 # all done
558 return $text;
559 }
560  
561 # remove duplicates ("Berlin, Berlin"), do replacements and similar
562 sub _postformat {
563 my $self = shift;
564 my $text = shift;
565 my $raa_rules = shift;
566  
567 if ($debug){
568 say STDERR "entering _postformat: $text";
569 say STDERR Dumper $raa_rules;
570 }
571  
572 # remove duplicates
573 my @before_pieces = split(/, /, $text);
574 my %seen;
575 my @after_pieces;
576 foreach my $piece (@before_pieces) {
577 $piece =~ s/^\s+//g;
578 $seen{$piece}++;
579 if (lc($piece) ne 'new york') {
580 next if ($seen{$piece} > 1);
581 }
582 push(@after_pieces, $piece);
583 }
584 $text = join(', ', @after_pieces);
585  
586 # do any country specific rules
587 foreach my $rule (@$raa_rules) {
588 my $regexp = $rule->{re};
589 my $replacement = $rule->{replacement};
590  
591 # ultra hack to do substitution
592 # limited to $1 and $2, should really be a while loop
593 # doing every substitution
594  
595 if ($replacement =~ m/\$\d/) {
596 if ($text =~ m/$regexp/) {
597 my $tmp1 = $1;
598 my $tmp2 = $2;
599 my $tmp3 = $3;
600 $replacement =~ s/\$1/$tmp1/;
601 $replacement =~ s/\$2/$tmp2/;
602 $replacement =~ s/\$3/$tmp3/;
603 }
604 }
605 $text =~ s/$regexp/$replacement/;
606 }
607 return $text;
608 }
609  
610 sub _sanity_cleaning {
611 my $self = shift;
612 my $rh_components = shift || return;
613  
614 # catch insane postcodes
615 if (defined($rh_components->{'postcode'})) {
616 if (length($rh_components->{'postcode'}) > 20) {
617 delete $rh_components->{'postcode'};
618 } elsif ($rh_components->{'postcode'} =~ m/\d+;\d+/) {
619 # sometimes OSM has postcode ranges
620 delete $rh_components->{'postcode'};
621 } elsif ($rh_components->{'postcode'} =~ m/^(\d{5}),\d{5}/) {
622 $rh_components->{'postcode'} = $1;
623 }
624 }
625  
626 # remove things that might be empty
627 foreach my $c (keys %$rh_components) {
628 # catch empty values
629 if (!defined($rh_components->{$c})) {
630 delete $rh_components->{$c};
631 }
632 # no chars
633 elsif ($rh_components->{$c} !~ m/\w/) {
634 delete $rh_components->{$c};
635 }
636 # catch values containing URLs
637 elsif ($rh_components->{$c} =~ m|https?://|) {
638 delete $rh_components->{$c};
639 }
640 }
641 return;
642 }
643  
644 sub _minimal_components {
645 my $self = shift;
646 my $rh_components = shift || return;
647 my @required_components = qw(road postcode); #FIXME - should be in conf
648 my $missing = 0; # num required components that are missing
649  
650 my $minimal_threshold = 2;
651 foreach my $c (@required_components) {
652 $missing++ if (!defined($rh_components->{$c}));
653 return 0 if ($missing == $minimal_threshold);
654 }
655 return 1;
656 }
657  
658 my %valid_replacement_components = ('state' => 1,);
659  
660 # determines which country code to use
661 # may also override other configuration if we are dealing with
662 # a dependent territory
663 sub _determine_country_code {
664 my $self = shift;
665 my $rh_components = shift || return;
666  
667 # FIXME - validate it is a valid country
668 return if (!defined($rh_components->{country_code}));
669  
670 if (my $cc = lc($rh_components->{country_code})) {
671  
672 # is it two letters long?
673 return if (length($cc) != 2);
674 return 'GB' if ($cc eq 'uk');
675  
676 $cc = uc($cc);
677  
678 # check if the configuration tells us to use
679 # the configuration of another country
680 # used in cases of dependent territories like
681 # American Samoa (AS) and Puerto Rico (PR)
682 if ( defined($self->{templates}{$cc})
683 && defined($self->{templates}{$cc}{use_country}))
684 {
685 my $old_cc = $cc;
686 $cc = $self->{templates}{$cc}{use_country};
687 if (defined($self->{templates}{$old_cc}{change_country})) {
688  
689 my $new_country = $self->{templates}{$old_cc}{change_country};
690 if ($new_country =~ m/\$(\w*)/) {
691 my $component = $1;
692 if (defined($rh_components->{$component})) {
693 $new_country =~ s/\$$component/$rh_components->{$component}/;
694 } else {
695 $new_country =~ s/\$$component//;
696 }
697 }
698 $rh_components->{country} = $new_country;
699 }
700 if (defined($self->{templates}{$old_cc}{add_component})) {
701 my $tmp = $self->{templates}{$old_cc}{add_component};
702 my ($k, $v) = split(/=/, $tmp);
703 # check whitelist of valid replacement components
704 if (defined($valid_replacement_components{$k})) {
705 $rh_components->{$k} = $v;
706 }
707 }
708 }
709  
710 if ($cc eq 'NL') {
711 if (defined($rh_components->{state})) {
712 if ($rh_components->{state} eq 'Curaçao') {
713 $cc = 'CW';
714 $rh_components->{country} = 'Curaçao';
715 } elsif ($rh_components->{state} =~ m/^sint maarten/i) {
716 $cc = 'SX';
717 $rh_components->{country} = 'Sint Maarten';
718 } elsif ($rh_components->{state} =~ m/^Aruba/i) {
719 $cc = 'AW';
720 $rh_components->{country} = 'Aruba';
721 }
722 }
723 }
724 return $cc;
725 }
726 return;
727 }
728  
729 # hacks for bad country data
730 sub _fix_country {
731 my $self = shift;
732 my $rh_c = shift || return;
733  
734 # is the country a number?
735 # if so, and there is a state, use state as country
736 if (defined($rh_c->{country}) && defined($rh_c->{state})){
737 if (looks_like_number($rh_c->{country})){
738 $rh_c->{country} = delete $rh_c->{state};
739 }
740 }
741 return;
742 }
743  
744 # sets and returns a state code
745 # note may also set other values in some odd edge cases
746 sub _add_state_code {
747 my $self = shift;
748 my $rh_components = shift // return;
749 if (defined($rh_components->{state})){
750 return $self->_add_code('state', $rh_components);
751 }
752 return;
753 }
754  
755 sub _add_county_code {
756 my $self = shift;
757 my $rh_components = shift // return;
758 if (defined($rh_components->{county})){
759 return $self->_add_code('county', $rh_components);
760 }
761 return;
762 }
763  
764 sub _add_code {
765 my $self = shift;
766 my $keyname = shift // return;
767 my $rh_components = shift;
768 return if !$rh_components->{country_code}; # do we know country?
769 return if !$rh_components->{$keyname}; # do we know state/county?
770  
771 my $code = $keyname . '_code';
772  
773 if (defined($rh_components->{$code})) {
774 # do we already have code?
775 # we could have situation where code and long name are same
776 # so we want to corrent
777 return if ($rh_components->{$code} ne $rh_components->{$keyname});
778 }
779  
780 # ensure country_code is uppercase as we use it as conf key
781 my $cc = uc($rh_components->{country_code});
782  
783 my $reverse_key = $code . 's_reverse';
784 my $name_key = $code . 's_name';
785  
786 if (my $rev = $self->{$reverse_key}{$cc}) {
787 my $name = $rh_components->{$keyname};
788 my $uc_name = uc($name);
789  
790 if (my $found_code = $rev->{$uc_name}) {
791 # matched a name -> code
792 $rh_components->{$code} = $found_code;
793 # if input was a code (e.g. state => 'NC'), set keyname to full name
794 if ($uc_name eq $found_code) {
795 my $full_name = $self->{$name_key}{$cc}{$found_code};
796 $rh_components->{$keyname} = $full_name if defined $full_name;
797 }
798 }
799  
800 # US-specific edge cases
801 if ($cc eq 'US' && $keyname eq 'state' && !defined($rh_components->{state_code})) {
802 if ($rh_components->{state} =~ m/^united states/i) {
803 my $state = $rh_components->{state};
804 $state =~ s/^United States/US/i;
805 if (my $fc = $rev->{uc($state)}) {
806 $rh_components->{state_code} = $fc;
807 }
808 }
809 if ($rh_components->{state} =~ m/^washington,? d\.?c\.?/i) {
810 $rh_components->{state_code} = 'DC';
811 $rh_components->{state} = 'District of Columbia';
812 $rh_components->{city} = 'Washington';
813 }
814 }
815 }
816 return $rh_components->{$code};
817 }
818  
819 sub _apply_replacements {
820 my $self = shift;
821 my $rh_components = shift;
822 my $ra_rules = shift // return; # bail out if no rules
823  
824 if ($debug){
825 say STDERR "in _apply_replacements";
826 say STDERR Dumper $ra_rules;
827 }
828  
829 # support legacy raw array-of-arrays format (used by tests)
830 if (ref($ra_rules) eq 'ARRAY' && @$ra_rules && ref($ra_rules->[0]) eq 'ARRAY') {
831 foreach my $component (keys %$rh_components) {
832 next if ($component eq 'country_code');
833 next if ($component eq 'house_number');
834 foreach my $ra_fromto (@$ra_rules) {
835 my $regexp;
836 if ($ra_fromto->[0] =~ m/^$component=/) {
837 my $from = $ra_fromto->[0];
838 $from =~ s/^$component=//;
839 if ($rh_components->{$component} eq $from) {
840 $rh_components->{$component} = $ra_fromto->[1];
841 } else {
842 $regexp = $from;
843 }
844 } else {
845 $regexp = $ra_fromto->[0];
846 }
847 if (defined($regexp)) {
848 try {
849 my $re = qr/$regexp/i;
850 $rh_components->{$component} =~ s/$re/$ra_fromto->[1]/;
851 } catch {
852 warn "invalid replacement: " . join(', ', @$ra_fromto);
853 };
854 }
855 }
856 }
857 return $rh_components;
858 }
859  
860 foreach my $component (keys %$rh_components) {
861 # some components dont need replacements
862 next if ($component eq 'country_code');
863 next if ($component eq 'house_number');
864 foreach my $rule (@$ra_rules) {
865 if (defined($rule->{component})) {
866 # component-specific rule: only applies to matching component
867 next if ($rule->{component} ne $component);
868 if ($rh_components->{$component} eq $rule->{exact_match}) {
869 $rh_components->{$component} = $rule->{replacement};
870 } else {
871 $rh_components->{$component} =~ s/$rule->{re}/$rule->{replacement}/;
872 }
873 } else {
874 $rh_components->{$component} =~ s/$rule->{re}/$rule->{replacement}/;
875 }
876 }
877 }
878 return $rh_components;
879 }
880  
881 sub _abbreviate {
882 my $self = shift;
883 my $rh_comp = shift // return;
884  
885 # do we know the country?
886 if (!defined($rh_comp->{country_code})) {
887 if ($show_warnings){
888 my $error_msg = 'no country_code, unable to abbreviate';
889 if (defined($rh_comp->{country})) {
890 $error_msg .= ' - country: ' . $rh_comp->{country};
891 }
892 warn $error_msg
893 }
894 return;
895 }
896  
897 # do we have abbreviations for this country?
898 my $cc = uc($rh_comp->{country_code});
899  
900 # 1. which languages?
901 if (defined($self->{country2lang}{$cc})) {
902  
903 my @langs = split(/,/, $self->{country2lang}{$cc});
904  
905 foreach my $lang (@langs) {
906 # do we have compiled abbrv for this lang?
907 if (defined($self->{compiled_abbreviations}{$lang})) {
908 my $rh_compiled = $self->{compiled_abbreviations}{$lang};
909  
910 foreach my $comp_name (keys %$rh_compiled) {
911 next if (!defined($rh_comp->{$comp_name}));
912 foreach my $rule (@{$rh_compiled->{$comp_name}}) {
913 $rh_comp->{$comp_name} =~ s/$rule->{re}/$1$rule->{short}/;
914 }
915 }
916 } else {
917 #warn "no abbreviations defined for lang $lang";
918 }
919 }
920 }
921 return $rh_comp;
922 }
923  
924 # " abc,,def , ghi " => 'abc, def, ghi'
925 sub _clean {
926 my $self = shift;
927 my $out = shift // return;
928 if ($debug){
929 say STDERR "entering _clean \n$out";
930 }
931  
932 $out =~ s/\&#39\;/'/g;
933  
934 $out =~ s/[\},\s]+$//;
935 $out =~ s/^[,\s]+//;
936  
937 $out =~ s/^- //; # line starting with dash due to a parameter missing
938  
939 $out =~ s/,\s*,/, /g; # multiple commas to one
940 $out =~ s/\h+,\h+/, /g; # one horiz whitespace behind comma
941 $out =~ s/\h\h+/ /g; # multiple horiz whitespace to one
942 $out =~ s/\h\n/\n/g; # horiz whitespace, newline to newline
943 $out =~ s/\n,/\n/g; # newline comma to just newline
944 $out =~ s/,,+/,/g; # multiple commas to one
945 $out =~ s/,\n/\n/g; # comma newline to just newline
946 $out =~ s/\n\h+/\n/g; # newline plus space to newline
947 $out =~ s/\n\n+/\n/g; # multiple newline to one
948  
949 # final dedupe across and within lines
950 my @before_pieces = split(/\n/, $out);
951 my %seen_lines;
952 my @after_pieces;
953 foreach my $line (@before_pieces) {
954 $line =~ s/^\h+//g;
955 $line =~ s/\h+$//g;
956 $seen_lines{$line}++;
957 next if ($seen_lines{$line} > 1);
958 # now dedupe within the line
959 my @before_words = split(/,/, $line);
960 my %seen_words;
961 my @after_words;
962 foreach my $w (@before_words) {
963 $w =~ s/^\h+//g;
964 $w =~ s/\h+$//g;
965 if (lc($w) ne 'new york') {
966 $seen_words{$w}++;
967 }
968 next if ((defined($seen_words{$w})) && ($seen_words{$w} > 1));
969 push(@after_words, $w);
970 }
971 $line = join(', ', @after_words);
972 push(@after_pieces, $line);
973 }
974 $out = join("\n", @after_pieces);
975  
976 $out =~ s/^\s+//; # remove leading whitespace
977 $out =~ s/\s+$//; # remove end whitespace
978  
979 $out .= "\n"; # add final newline
980 return $out; # we are done
981 }
982  
983 sub _render_template {
984 my $self = shift;
985 my $thtemplate = shift;
986 my $components = shift;
987  
988 # Mustache calls it context
989 my $context = $components;
990 say STDERR 'context: ' . Dumper $context if ($debug);
991 my $output = $thtemplate->render($context);
992  
993 $output = $self->_evaluate_template_lamdas($output);
994  
995 say STDERR "in _render before _clean: $output" if ($debug);
996 $output = $self->_clean($output);
997  
998 # is it empty?
999 if (length($output) == 0){
1000 # if yes and there is only one component then just use that one
1001 my @comps = keys %$components;
1002 if (scalar(@comps) == 1) {
1003 foreach my $k (@comps) {
1004 $output = $components->{$k};
1005 }
1006 } # FIXME what if more than one?
1007 }
1008 return $output;
1009 }
1010  
1011 # Text::Hogan apparently caches lambdas when rendering templates. In the past
1012 # we needed our lambda 'first', example
1013 # {{#first}} {{{city}}} || {{{town}}} {{/first}}
1014 # to evaluate the components. Whenever the lambda was called with different
1015 # component values it consumed memory. Now replace with a simpler implementation
1016 #
1017 sub _replace_template_lambdas {
1018 my $self = shift;
1019 my $template_text = shift;
1020 $template_text =~ s!\Q{{#first}}\E(.+?)\Q{{/first}}\E!FIRSTSTART${1}FIRSTEND!g;
1021 return $template_text;
1022 }
1023  
1024 # We only use a lambda named 'first'
1025 sub _evaluate_template_lamdas {
1026 my $self = shift;
1027 my $text = shift;
1028 $text =~ s!FIRSTSTART\s*(.+?)\s*FIRSTEND!_select_first($1)!seg;
1029 return $text;
1030 }
1031  
1032 # '|| val1 || || val3' => 'val1'
1033 sub _select_first {
1034 my $text = shift;
1035 my @a_parts = grep { length($_) } split(/\s*\|\|\s*/, $text);
1036 return scalar(@a_parts) ? $a_parts[0] : '';
1037 }
1038  
1039 my %small_district = (
1040 'BR' => 1,
1041 'CR' => 1,
1042 'ES' => 1,
1043 'NI' => 1,
1044 'PY' => 1,
1045 'RO' => 1,
1046 'TG' => 1,
1047 'TM' => 1,
1048 'XK' => 1,
1049 );
1050  
1051 # correct the alias for "district"
1052 # in OSM some countries use district to mean "city_district"
1053 # others to mean "state_district"
1054 sub _set_district_alias {
1055 my $self = shift;
1056 my $cc = shift // return;
1057  
1058 my $ucc = uc($cc);
1059  
1060 # this may get called repeatedly
1061 # no need to do the work again
1062 return if (defined($self->{set_district_alias}{$ucc}));
1063  
1064 # note that we are here so don't do this work again
1065 $self->{set_district_alias}{$ucc} = 1;
1066  
1067 my $oldalias;
1068 if (defined($small_district{$ucc})){
1069 $self->{component2type}{district} = 'neighbourhood';
1070 $oldalias = 'state_district';
1071  
1072 # add to the neighbourhood alias list
1073 # though of course we are just sticking it at the end
1074 push(@{$self->{component_aliases}{'neighbourhood'}}, 'district');
1075  
1076 } else {
1077 # set 'district' to be type 'state_district'
1078 $self->{component2type}{district} = 'state_district';
1079 $oldalias = 'neighbourhood';
1080  
1081 # add to the state_district alias list
1082 push(@{$self->{component_aliases}{'state_district'}}, 'district');
1083 }
1084  
1085 # remove from the old alias list
1086 my @temp = grep { $_ ne 'district' }
1087 @{$self->{component_aliases}{$oldalias}};
1088
1089 $self->{component_aliases}{$oldalias} = \@temp;
1090 return;
1091 }
1092  
1093  
1094 # returns []
1095 sub _find_unknown_components {
1096 my $self = shift;
1097 my $rh_comp = shift;
1098  
1099 my @a_unknown = grep { !exists($self->{h_known}->{$_}) } keys %$rh_comp;
1100 return \@a_unknown;
1101 }
1102  
1103  
1104 1;
1105  
1106 __END__
1107  
1108 =pod
1109  
1110 =encoding UTF-8
1111  
1112 =head1 NAME
1113  
1114 Geo::Address::Formatter - take structured address data and format it according to the various global/country rules
1115  
1116 =head1 VERSION
1117  
1118 version 1.9993
1119  
1120 =head1 SYNOPSIS
1121  
1122 #
1123 # get the templates (or use your own)
1124 # git clone git@github.com:OpenCageData/address-formatting.git
1125 #
1126 my $GAF = Geo::Address::Formatter->new( conf_path => '/path/to/templates' );
1127  
1128 my $components = { ... }
1129 my $text = $GAF->format_address($components, { country => 'FR' } );
1130 my $rh_final_components = $GAF->final_components();
1131 #
1132 # or if we want shorter output
1133 #
1134 my $short_text = $GAF->format_address($components, { country => 'FR', abbreviate => 1, });
1135  
1136 =head2 new
1137  
1138 my $GAF = Geo::Address::Formatter->new( conf_path => '/path/to/templates' );
1139  
1140 Returns new object. The I<conf_path> is required.
1141  
1142 Optional parameters are:
1143  
1144 =over
1145  
1146 =item * I<debug>
1147  
1148 Prints tons of debugging info for use in development.
1149  
1150 =item * I<no_warnings>
1151  
1152 Turns off a few warnings if configuration is not optimal.
1153  
1154 =item * I<only_address>
1155  
1156 Formatted output will only contain known address components (will not
1157 include POI names). Can be overridden per-call via the I<only_address>
1158 option to L</format_address>.
1159  
1160 =back
1161  
1162 =head2 instance
1163  
1164 my $GAF = Geo::Address::Formatter->instance( conf_path => '/path/to/templates' );
1165  
1166 Returns new instance (potentially re-used) The I<conf_path> is required.
1167  
1168 Optional parameters are as in the B<new> method.
1169  
1170 =head2 final_components
1171  
1172 my $rh_components = $GAF->final_components();
1173  
1174 Returns a reference to a hash of the final components that were set during
1175 the most recent call to B<format_address>. Returns C<undef> if called
1176 before B<format_address> has been called. Warns in that case unless
1177 I<no_warnings> was set at object creation.
1178  
1179 =head2 format_address
1180  
1181 my $text = $GAF->format_address(\%components, \%options );
1182  
1183 Given structured address components (hashref) and options (hashref) returns a
1184 formatted address as a multiline string with a trailing newline.
1185  
1186 Possible options are:
1187  
1188 =over
1189  
1190 =item * I<abbreviate>
1191  
1192 If true, common abbreviations are applied to the resulting output.
1193  
1194 =item * I<address_template>
1195  
1196 A Mustache-format template to be used instead of the template defined in
1197 the configuration.
1198  
1199 =item * I<country>
1200  
1201 An uppercase ISO 3166-1 alpha-2 code, e.g. C<'GB'> for Great Britain,
1202 C<'DE'> for Germany, etc. If omitted the country is determined from
1203 the address components.
1204  
1205 =item * I<only_address>
1206  
1207 Same as the I<only_address> constructor option but applied per-call.
1208  
1209 =back
1210  
1211 =head1 DESCRIPTION
1212  
1213 You have a structured postal address (hash) and need to convert it into a
1214 readable address based on the format of the address country.
1215  
1216 For example, you have:
1217  
1218 {
1219 house_number => 12,
1220 street => 'Avenue Road',
1221 postcode => 45678,
1222 city => 'Deville'
1223 }
1224  
1225 you need:
1226  
1227 Great Britain: 12 Avenue Road, Deville 45678
1228 France: 12 Avenue Road, 45678 Deville
1229 Germany: Avenue Road 12, 45678 Deville
1230 Latvia: Avenue Road 12, Deville, 45678
1231  
1232 It gets more complicated with 200+ countries and territories and dozens more
1233 address components to consider.
1234  
1235 This module comes with a minimal configuration to run tests. Instead of
1236 developing your own configuration please use (and contribute to)
1237 those in https://github.com/OpenCageData/address-formatting
1238 which includes test cases.
1239  
1240 Together we can address the world!
1241  
1242 =head1 SEE ALSO
1243  
1244 L<https://github.com/OpenCageData/address-formatting> - the address
1245 formatting template configuration used by this module.
1246  
1247 L<https://opencagedata.com> - OpenCage geocoder.
1248  
1249 =head1 AUTHOR
1250  
1251 Ed Freyfogle
1252  
1253 =head1 COPYRIGHT AND LICENSE
1254  
1255 This software is copyright (c) 2026 by Opencage GmbH.
1256  
1257 This is free software; you can redistribute it and/or modify it under
1258 the same terms as the Perl 5 programming language system itself.
1259  
1260 =cut