-
Notifications
You must be signed in to change notification settings - Fork 8
/
US.pm
1146 lines (952 loc) · 31.6 KB
/
US.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Geo::StreetAddress::US;
use 5.008_001;
use strict;
use warnings;
our $VERSION = '1.04';
use base 'Class::Data::Inheritable';
=head1 NAME
Geo::StreetAddress::US - Perl extension for parsing US street addresses
=head1 SYNOPSIS
use Geo::StreetAddress::US;
$hashref = Geo::StreetAddress::US->parse_location(
"1005 Gravenstein Hwy N, Sebastopol CA 95472" );
$hashref = Geo::StreetAddress::US->parse_location(
"Hollywood & Vine, Los Angeles, CA" );
$hashref = Geo::StreetAddress::US->parse_address(
"1600 Pennsylvania Ave, Washington, DC" );
$hashref = Geo::StreetAddress::US->parse_address(
"1600 Pennsylvania Ave, Washington, DC" );
$hashref = Geo::StreetAddress::US->parse_informal_address(
"Lot 3 Pennsylvania Ave" );
$hashref = Geo::StreetAddress::US->parse_intersection(
"Mission Street at Valencia Street, San Francisco, CA" );
$hashref = Geo::StreetAddress::US->normalize_address( \%spec );
# the parse_* methods call this automatically...
=head1 DESCRIPTION
Geo::StreetAddress::US is a regex-based street address and street intersection
parser for the United States. Its basic goal is to be as forgiving as possible
when parsing user-provided address strings. Geo::StreetAddress::US knows about
directional prefixes and suffixes, fractional building numbers, building units,
grid-based addresses (such as those used in parts of Utah), 5 and 9 digit ZIP
codes, and all of the official USPS abbreviations for street types, state
names and secondary unit designators.
=head1 RETURN VALUES
Most Geo::StreetAddress::US methods return a reference to a hash containing
address or intersection information. This
"address specifier" hash may contain any of the following fields for a
given address. If a given field is not present in the address, the
corresponding key will be set to C<undef> in the hash.
Future versions of this module may add extra fields.
=head1 ADDRESS SPECIFIER
=head2 number
House or street number.
=head2 prefix
Directional prefix for the street, such as N, NE, E, etc. A given prefix
should be one to two characters long.
=head2 street
Name of the street, without directional or type qualifiers.
=head2 type
Abbreviated street type, e.g. Rd, St, Ave, etc. See the USPS official
type abbreviations at L<http://pe.usps.com/text/pub28/pub28apc.html>
for a list of abbreviations used.
=head2 suffix
Directional suffix for the street, as above.
=head2 city
Name of the city, town, or other locale that the address is situated in.
=head2 state
The state which the address is situated in, given as its two-letter
postal abbreviation. for a list of abbreviations used.
=head2 zip
Five digit ZIP postal code for the address, including leading zero, if needed.
=head2 sec_unit_type
If the address includes a Secondary Unit Designator, such as a room, suite or
appartment, the C<sec_unit_type> field will indicate the type of unit.
=head2 sec_unit_num
If the address includes a Secondary Unit Designator, such as a room, suite or appartment,
the C<sec_unit_num> field will indicate the number of the unit (which may not be numeric).
=head1 INTERSECTION SPECIFIER
=head2 prefix1, prefix2
Directional prefixes for the streets in question.
=head2 street1, street2
Names of the streets in question.
=head2 type1, type2
Street types for the streets in question.
=head2 suffix1, suffix2
Directional suffixes for the streets in question.
=head2 city
City or locale containing the intersection, as above.
=head2 state
State abbreviation, as above.
=head2 zip
Five digit ZIP code, as above.
=cut
=head1 GLOBAL VARIABLES
Geo::StreetAddress::US contains a number of global variables which it
uses to recognize different bits of US street addresses. Although you
will probably not need them, they are documented here for completeness's
sake.
=cut
=head2 %Directional
Maps directional names (north, northeast, etc.) to abbreviations (N, NE, etc.).
=head2 %Direction_Code
Maps directional abbreviations to directional names.
=cut
our %Directional = (
north => "N",
northeast => "NE",
east => "E",
southeast => "SE",
south => "S",
southwest => "SW",
west => "W",
northwest => "NW",
);
our %Direction_Code; # setup in init();
=head2 %Street_Type
Maps lowercased USPS standard street types to their canonical postal
abbreviations as found in TIGER/Line. See eg/get_street_abbrev.pl in
the distrbution for how this map was generated.
=cut
our %Street_Type = (
allee => "aly",
alley => "aly",
ally => "aly",
anex => "anx",
annex => "anx",
annx => "anx",
arcade => "arc",
av => "ave",
aven => "ave",
avenu => "ave",
avenue => "ave",
avn => "ave",
avnue => "ave",
bayoo => "byu",
bayou => "byu",
beach => "bch",
bend => "bnd",
bluf => "blf",
bluff => "blf",
bluffs => "blfs",
bot => "btm",
bottm => "btm",
bottom => "btm",
boul => "blvd",
boulevard => "blvd",
boulv => "blvd",
branch => "br",
brdge => "brg",
bridge => "brg",
brnch => "br",
brook => "brk",
brooks => "brks",
burg => "bg",
burgs => "bgs",
bypa => "byp",
bypas => "byp",
bypass => "byp",
byps => "byp",
camp => "cp",
canyn => "cyn",
canyon => "cyn",
cape => "cpe",
causeway => "cswy",
causway => "cswy",
cen => "ctr",
cent => "ctr",
center => "ctr",
centers => "ctrs",
centr => "ctr",
centre => "ctr",
circ => "cir",
circl => "cir",
circle => "cir",
circles => "cirs",
ck => "crk",
cliff => "clf",
cliffs => "clfs",
club => "clb",
cmp => "cp",
cnter => "ctr",
cntr => "ctr",
cnyn => "cyn",
common => "cmn",
corner => "cor",
corners => "cors",
course => "crse",
court => "ct",
courts => "cts",
cove => "cv",
coves => "cvs",
cr => "crk",
crcl => "cir",
crcle => "cir",
crecent => "cres",
creek => "crk",
crescent => "cres",
cresent => "cres",
crest => "crst",
crossing => "xing",
crossroad => "xrd",
crscnt => "cres",
crsent => "cres",
crsnt => "cres",
crssing => "xing",
crssng => "xing",
crt => "ct",
curve => "curv",
dale => "dl",
dam => "dm",
div => "dv",
divide => "dv",
driv => "dr",
drive => "dr",
drives => "drs",
drv => "dr",
dvd => "dv",
estate => "est",
estates => "ests",
exp => "expy",
expr => "expy",
express => "expy",
expressway => "expy",
expw => "expy",
extension => "ext",
extensions => "exts",
extn => "ext",
extnsn => "ext",
falls => "fls",
ferry => "fry",
field => "fld",
fields => "flds",
flat => "flt",
flats => "flts",
ford => "frd",
fords => "frds",
forest => "frst",
forests => "frst",
forg => "frg",
forge => "frg",
forges => "frgs",
fork => "frk",
forks => "frks",
fort => "ft",
freeway => "fwy",
freewy => "fwy",
frry => "fry",
frt => "ft",
frway => "fwy",
frwy => "fwy",
garden => "gdn",
gardens => "gdns",
gardn => "gdn",
gateway => "gtwy",
gatewy => "gtwy",
gatway => "gtwy",
glen => "gln",
glens => "glns",
grden => "gdn",
grdn => "gdn",
grdns => "gdns",
green => "grn",
greens => "grns",
grov => "grv",
grove => "grv",
groves => "grvs",
gtway => "gtwy",
harb => "hbr",
harbor => "hbr",
harbors => "hbrs",
harbr => "hbr",
haven => "hvn",
havn => "hvn",
height => "hts",
heights => "hts",
hgts => "hts",
highway => "hwy",
highwy => "hwy",
hill => "hl",
hills => "hls",
hiway => "hwy",
hiwy => "hwy",
hllw => "holw",
hollow => "holw",
hollows => "holw",
holws => "holw",
hrbor => "hbr",
ht => "hts",
hway => "hwy",
inlet => "inlt",
island => "is",
islands => "iss",
isles => "isle",
islnd => "is",
islnds => "iss",
jction => "jct",
jctn => "jct",
jctns => "jcts",
junction => "jct",
junctions => "jcts",
junctn => "jct",
juncton => "jct",
key => "ky",
keys => "kys",
knol => "knl",
knoll => "knl",
knolls => "knls",
la => "ln",
lake => "lk",
lakes => "lks",
landing => "lndg",
lane => "ln",
lanes => "ln",
ldge => "ldg",
light => "lgt",
lights => "lgts",
lndng => "lndg",
loaf => "lf",
lock => "lck",
locks => "lcks",
lodg => "ldg",
lodge => "ldg",
loops => "loop",
manor => "mnr",
manors => "mnrs",
meadow => "mdw",
meadows => "mdws",
medows => "mdws",
mill => "ml",
mills => "mls",
mission => "msn",
missn => "msn",
mnt => "mt",
mntain => "mtn",
mntn => "mtn",
mntns => "mtns",
motorway => "mtwy",
mount => "mt",
mountain => "mtn",
mountains => "mtns",
mountin => "mtn",
mssn => "msn",
mtin => "mtn",
neck => "nck",
orchard => "orch",
orchrd => "orch",
overpass => "opas",
ovl => "oval",
parks => "park",
parkway => "pkwy",
parkways => "pkwy",
parkwy => "pkwy",
passage => "psge",
paths => "path",
pikes => "pike",
pine => "pne",
pines => "pnes",
pk => "park",
pkway => "pkwy",
pkwys => "pkwy",
pky => "pkwy",
place => "pl",
plain => "pln",
plaines => "plns",
plains => "plns",
plaza => "plz",
plza => "plz",
point => "pt",
points => "pts",
port => "prt",
ports => "prts",
prairie => "pr",
prarie => "pr",
prk => "park",
prr => "pr",
rad => "radl",
radial => "radl",
radiel => "radl",
ranch => "rnch",
ranches => "rnch",
rapid => "rpd",
rapids => "rpds",
rdge => "rdg",
rest => "rst",
ridge => "rdg",
ridges => "rdgs",
river => "riv",
rivr => "riv",
rnchs => "rnch",
road => "rd",
roads => "rds",
route => "rte",
rvr => "riv",
shoal => "shl",
shoals => "shls",
shoar => "shr",
shoars => "shrs",
shore => "shr",
shores => "shrs",
skyway => "skwy",
spng => "spg",
spngs => "spgs",
spring => "spg",
springs => "spgs",
sprng => "spg",
sprngs => "spgs",
spurs => "spur",
sqr => "sq",
sqre => "sq",
sqrs => "sqs",
squ => "sq",
square => "sq",
squares => "sqs",
station => "sta",
statn => "sta",
stn => "sta",
str => "st",
strav => "stra",
strave => "stra",
straven => "stra",
stravenue => "stra",
stravn => "stra",
stream => "strm",
street => "st",
streets => "sts",
streme => "strm",
strt => "st",
strvn => "stra",
strvnue => "stra",
sumit => "smt",
sumitt => "smt",
summit => "smt",
terr => "ter",
terrace => "ter",
throughway => "trwy",
tpk => "tpke",
tr => "trl",
trace => "trce",
traces => "trce",
track => "trak",
tracks => "trak",
trafficway => "trfy",
trail => "trl",
trails => "trl",
trk => "trak",
trks => "trak",
trls => "trl",
trnpk => "tpke",
trpk => "tpke",
tunel => "tunl",
tunls => "tunl",
tunnel => "tunl",
tunnels => "tunl",
tunnl => "tunl",
turnpike => "tpke",
turnpk => "tpke",
underpass => "upas",
union => "un",
unions => "uns",
valley => "vly",
valleys => "vlys",
vally => "vly",
vdct => "via",
viadct => "via",
viaduct => "via",
view => "vw",
views => "vws",
vill => "vlg",
villag => "vlg",
village => "vlg",
villages => "vlgs",
ville => "vl",
villg => "vlg",
villiage => "vlg",
vist => "vis",
vista => "vis",
vlly => "vly",
vst => "vis",
vsta => "vis",
walks => "walk",
well => "wl",
wells => "wls",
wy => "way",
);
our %_Street_Type_List; # set up in init() later;
our %_Street_Type_Match; # set up in init() later;
=head2 %State_Code
Maps lowercased US state and territory names to their canonical two-letter
postal abbreviations. See eg/get_state_abbrev.pl in the distrbution
for how this map was generated.
=cut
our %State_Code = (
"alabama" => "AL",
"alaska" => "AK",
"american samoa" => "AS",
"arizona" => "AZ",
"arkansas" => "AR",
"california" => "CA",
"colorado" => "CO",
"connecticut" => "CT",
"delaware" => "DE",
"district of columbia" => "DC",
"federated states of micronesia" => "FM",
"florida" => "FL",
"georgia" => "GA",
"guam" => "GU",
"hawaii" => "HI",
"idaho" => "ID",
"illinois" => "IL",
"indiana" => "IN",
"iowa" => "IA",
"kansas" => "KS",
"kentucky" => "KY",
"louisiana" => "LA",
"maine" => "ME",
"marshall islands" => "MH",
"maryland" => "MD",
"massachusetts" => "MA",
"michigan" => "MI",
"minnesota" => "MN",
"mississippi" => "MS",
"missouri" => "MO",
"montana" => "MT",
"nebraska" => "NE",
"nevada" => "NV",
"new hampshire" => "NH",
"new jersey" => "NJ",
"new mexico" => "NM",
"new york" => "NY",
"north carolina" => "NC",
"north dakota" => "ND",
"northern mariana islands" => "MP",
"ohio" => "OH",
"oklahoma" => "OK",
"oregon" => "OR",
"palau" => "PW",
"pennsylvania" => "PA",
"puerto rico" => "PR",
"rhode island" => "RI",
"south carolina" => "SC",
"south dakota" => "SD",
"tennessee" => "TN",
"texas" => "TX",
"utah" => "UT",
"vermont" => "VT",
"virgin islands" => "VI",
"virginia" => "VA",
"washington" => "WA",
"west virginia" => "WV",
"wisconsin" => "WI",
"wyoming" => "WY",
);
=head2 %State_FIPS
Maps two-digit FIPS-55 US state and territory codes (including the
leading zero!) as found in TIGER/Line to the state's canonical two-letter
postal abbreviation. See eg/get_state_fips.pl in the distrbution for
how this map was generated. Yes, I know the FIPS data also has the state
names. Oops.
=cut
our %State_FIPS = (
"01" => "AL",
"02" => "AK",
"04" => "AZ",
"05" => "AR",
"06" => "CA",
"08" => "CO",
"09" => "CT",
"10" => "DE",
"11" => "DC",
"12" => "FL",
"13" => "GA",
"15" => "HI",
"16" => "ID",
"17" => "IL",
"18" => "IN",
"19" => "IA",
"20" => "KS",
"21" => "KY",
"22" => "LA",
"23" => "ME",
"24" => "MD",
"25" => "MA",
"26" => "MI",
"27" => "MN",
"28" => "MS",
"29" => "MO",
"30" => "MT",
"31" => "NE",
"32" => "NV",
"33" => "NH",
"34" => "NJ",
"35" => "NM",
"36" => "NY",
"37" => "NC",
"38" => "ND",
"39" => "OH",
"40" => "OK",
"41" => "OR",
"42" => "PA",
"44" => "RI",
"45" => "SC",
"46" => "SD",
"47" => "TN",
"48" => "TX",
"49" => "UT",
"50" => "VT",
"51" => "VA",
"53" => "WA",
"54" => "WV",
"55" => "WI",
"56" => "WY",
"72" => "PR",
"78" => "VI",
);
our %FIPS_State; # setup in init() later;
=head2 %Addr_Match
A hash of compiled regular expressions corresponding to different
types of address or address portions. Defined regexen include
type, number, fraction, state, direct(ion), dircode, zip, corner,
street, place, address, and intersection.
Direct use of these patterns is not recommended because they may change in
subtle ways between releases.
=cut
our %Addr_Match; # setup in init()
init();
our %Normalize_Map = (
prefix => \%Directional,
prefix1 => \%Directional,
prefix2 => \%Directional,
suffix => \%Directional,
suffix1 => \%Directional,
suffix2 => \%Directional,
type => \%Street_Type,
type1 => \%Street_Type,
type2 => \%Street_Type,
state => \%State_Code,
);
=head1 CLASS ACCESSORS
=head2 avoid_redundant_street_type
If true then L</normalize_address> will set the C<type> field to undef
if the C<street> field contains a word that corresponds to the C<type> in L<\%Street_Type>.
For example, given "4321 Country Road 7", C<street> will be "Country Road 7"
and C<type> will be "Rd". With avoid_redundant_street_type set true, C<type>
will be undef because C<street> matches /\b (rd|road) \b/ix;
Also applies to C<type1> for C<street1> and C<type2> for C<street2>
fields for intersections.
The default is false, for backwards compatibility.
=cut
BEGIN { __PACKAGE__->mk_classdata('avoid_redundant_street_type' => 0) }
=head1 CLASS METHODS
=head2 init
# Add another street type mapping:
$Geo::StreetAddress::US::Street_Type{'cur'}='curv';
# Re-initialize to pick up the change
Geo::StreetAddress::US::init();
Runs the setup on globals. This is run automatically when the module is loaded,
but if you subsequently change the globals, you should run it again.
=cut
sub init {
%Direction_Code = reverse %Directional;
%FIPS_State = reverse %State_FIPS;
%_Street_Type_List = map { $_ => 1 } %Street_Type;
# build hash { 'rd' => qr/\b (?: rd|road ) \b/xi, ... }
%_Street_Type_Match = map { $_ => $_ } values %Street_Type;
while ( my ($type_alt, $type_abbrv) = each %Street_Type ) {
$_Street_Type_Match{$type_abbrv} .= "|\Q$type_alt";
}
%_Street_Type_Match = map {
my $alts = $_Street_Type_Match{$_};
$_ => qr/\b (?: $alts ) \b/xi;
} keys %_Street_Type_Match;
use re 'eval';
%Addr_Match = (
type => join("|", keys %_Street_Type_List),
fraction => qr{\d+\/\d+},
state => '\b(?:'.join("|",
# escape spaces in state names (e.g., "new york" --> "new\\ york")
# so they still match in the x environment below
map { ( quotemeta $_) } keys %State_Code, values %State_Code
).')\b',
direct => join("|",
# map direction names to direction codes
keys %Directional,
# also map the dotted version of the code to the code itself
map {
my $c = $_; $c =~ s/(\w)/$1./g; ( quotemeta $c, $_ )
} sort { length $b <=> length $a } values %Directional
),
dircode => join("|", keys %Direction_Code),
zip => qr/\d{5}(?:-?\d{4})?/, # XXX add \b?
corner => qr/(?:\band\b|\bat\b|&|\@)/i,
);
# we don't include letters in the number regex because we want to
# treat "42S" as "42 S" (42 South). For example,
# Utah and Wisconsin have a more elaborate system of block numbering
# http://en.wikipedia.org/wiki/House_number#Block_numbers
$Addr_Match{number} = qr/(\d+-?\d*)(?=\D) (?{ $_{number} = $^N })/ix,
# note that expressions like [^,]+ may scan more than you expect
$Addr_Match{street} = qr/
(?:
# special case for addresses like 100 South Street
(?:($Addr_Match{direct})\W+ (?{ $_{street} = $^N })
($Addr_Match{type})\b (?{ $_{type} = $^N }))
#(?{ $_{_street}.=1 })
|
(?:($Addr_Match{direct})\W+ (?{ $_{prefix} = $^N }))?
(?:
([^,]*\d) (?{ $_{street} = $^N })
(?:[^\w,]*($Addr_Match{direct})\b (?{ $_{suffix} = $^N; $_{type}||='' }))
#(?{ $_{_street}.=3 })
|
([^,]+) (?{ $_{street} = $^N })
(?:[^\w,]+($Addr_Match{type})\b (?{ $_{type} = $^N }))
(?:[^\w,]+($Addr_Match{direct})\b (?{ $_{suffix} = $^N }))?
#(?{ $_{_street}.=2 })
|
([^,]+?) (?{ $_{street} = $^N; $_{type}||='' })
(?:[^\w,]+($Addr_Match{type})\b (?{ $_{type} = $^N }))?
(?:[^\w,]+($Addr_Match{direct})\b (?{ $_{suffix} = $^N }))?
#(?{ $_{_street}.=4 })
)
)
/ix;
# http://pe.usps.com/text/pub28/pub28c2_003.htm
# TODO add support for those that don't require a number
# TODO map to standard names/abbreviations
$Addr_Match{sec_unit_type_numbered} = qr/
(su?i?te
|p\W*[om]\W*b(?:ox)?
|(?:ap|dep)(?:ar)?t(?:me?nt)?
|ro*m
|flo*r?
|uni?t
|bu?i?ldi?n?g
|ha?nga?r
|lo?t
|pier
|slip
|spa?ce?
|stop
|tra?i?le?r
|box)(?![a-z]) (?{ $_{sec_unit_type} = $^N })
/ix;
$Addr_Match{sec_unit_type_unnumbered} = qr/
(ba?se?me?n?t
|fro?nt
|lo?bby
|lowe?r
|off?i?ce?
|pe?n?t?ho?u?s?e?
|rear
|side
|uppe?r
)\b (?{ $_{sec_unit_type} = $^N })
/ix;
$Addr_Match{sec_unit} = qr/
(:?
(?: (?:$Addr_Match{sec_unit_type_numbered} \W*)
| (\#)\W* (?{ $_{sec_unit_type} = $^N })
)
( [\w-]+) (?{ $_{sec_unit_num} = $^N })
)
|
$Addr_Match{sec_unit_type_unnumbered}
/ix;
$Addr_Match{city_and_state} = qr/
(?:
([^\d,]+?)\W+ (?{ $_{city} = $^N })
($Addr_Match{state}) (?{ $_{state} = $^N })
)
/ix;
$Addr_Match{place} = qr/
(?:$Addr_Match{city_and_state}\W*)?
(?:($Addr_Match{zip}) (?{ $_{zip} = $^N }))?
/ix;
# the \x23 below is an alias for '#' to avoid a bug in perl 5.18.1
# https://rt.cpan.org/Ticket/Display.html?id=91420
$Addr_Match{address} = qr/
^
[^\w\x23]* # skip non-word chars except # (eg unit)
( $Addr_Match{number} )\W*
(?:$Addr_Match{fraction}\W*)?
$Addr_Match{street}\W+
(?:$Addr_Match{sec_unit}\W+)?
$Addr_Match{place}
\W* # require on non-word chars at end
$ # right up to end of string
/ix;
my $sep = qr/(?:\W+|\Z)/;
$Addr_Match{informal_address} = qr/
^
\s* # skip leading whitespace
(?:$Addr_Match{sec_unit} $sep)?
(?:$Addr_Match{number})?\W*
(?:$Addr_Match{fraction}\W*)?
$Addr_Match{street} $sep
(?:$Addr_Match{sec_unit} $sep)?
(?:$Addr_Match{place})?
# don't require match to reach end of string
/ix;
$Addr_Match{intersection} = qr/^\W*
$Addr_Match{street}\W*?
\s+$Addr_Match{corner}\s+
(?{ exists $_{$_} and $_{$_.1} = delete $_{$_} for (qw{prefix street type suffix})})
$Addr_Match{street}\W+
(?{ exists $_{$_} and $_{$_.2} = delete $_{$_} for (qw{prefix street type suffix})})
$Addr_Match{place}
\W*$/ix;
}
=head2 parse_location
$spec = Geo::StreetAddress::US->parse_location( $string )
Parses any address or intersection string and returns the appropriate
specifier. If $string matches the $Addr_Match{corner} pattern then
parse_intersection() is used. Else parse_address() is called and if that
returns false then parse_informal_address() is called.
=cut
sub parse_location {
my ($class, $addr) = @_;
if ($addr =~ /$Addr_Match{corner}/ios) {
return $class->parse_intersection($addr);
}
return $class->parse_address($addr)
|| $class->parse_informal_address($addr);
}
=head2 parse_address
$spec = Geo::StreetAddress::US->parse_address( $address_string )
Parses a street address into an address specifier using the $Addr_Match{address}
pattern. Returning undef if the address cannot be parsed as a complete formal
address.
You may want to use parse_location() instead.
=cut
sub parse_address {
my ($class, $addr) = @_;
local %_;
$addr =~ /$Addr_Match{address}/ios
or return undef;
return $class->normalize_address({ %_ });
}
=head2 parse_informal_address
$spec = Geo::StreetAddress::US->parse_informal_address( $address_string )
Acts like parse_address() except that it handles a wider range of address
formats because it uses the L</informal_address> pattern. That means a
unit can come first, a street number is optional, and the city and state aren't
needed. Which means that informal addresses like "#42 123 Main St" can be parsed.
Returns undef if the address cannot be parsed.
You may want to use parse_location() instead.
=cut
sub parse_informal_address {
my ($class, $addr) = @_;
local %_;
$addr =~ /$Addr_Match{informal_address}/ios
or return undef;
return $class->normalize_address({ %_ });
}
=head2 parse_intersection
$spec = Geo::StreetAddress::US->parse_intersection( $intersection_string )
Parses an intersection string into an intersection specifier, returning
undef if the address cannot be parsed. You probably want to use
parse_location() instead.