Skip to content

Commit e0cb372

Browse files
authored
Merge pull request #75 from argyle-engineering/us-fixes
Add line1 and line2 element, split of full street
2 parents bfb720a + 522f39d commit e0cb372

5 files changed

Lines changed: 100 additions & 54 deletions

File tree

pyap/address.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class Address:
2323
country_id: str
2424
full_street: str
2525
full_address: str
26+
line1: str
27+
line2: Union[str, None] = None
2628
city: Union[str, None] = None
2729
floor: Union[str, None] = None
2830
region1: Union[str, None] = None
@@ -44,9 +46,9 @@ def __post_init__(self):
4446
for s_field in self.__dataclass_fields__:
4547
field = getattr(self, s_field)
4648
if isinstance(field, str):
47-
setattr(self, s_field, field.strip(" ,;:"))
49+
setattr(self, s_field, field.strip(" ,;:\n"))
4850
elif isinstance(field, list) and field and isinstance(field[0], str):
49-
setattr(self, s_field, field[0].strip(" ,;:"))
51+
setattr(self, s_field, field[0].strip(" ,;:\n"))
5052

5153
def __repr__(self) -> str:
5254
# Address object is represented as textual address

pyap/source_CA/data.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -431,31 +431,38 @@
431431
# Format commonly used in French
432432
(?P<full_street_b>
433433
434-
{street_number_b}{div}
435-
{street_type_b}{div}
436-
({street_name_b} {po_box_positive_lookahead})?\,?\ ?
437-
{post_direction_b}?\,?\ ?
438-
{po_box_b}?\,?\ ?
434+
(?P<line1_b>
435+
{street_number_b}{div}
436+
{street_type_b}{div}
437+
({street_name_b} {po_box_positive_lookahead})?\,?\ ?
438+
{post_direction_b}?\,?\ ?
439+
{po_box_b}?\,?\ ?
440+
)
439441
)
440442
|
441443
# Format commonly used in English
442444
(?P<full_street>
443445
444-
{street_number}\,?\ ?
445-
{street_name}?\,?\ ?
446-
(?:(?<=[\ \,]){street_type})\,?\ ?
447-
{post_direction}?\,?\ ?
448-
{floor}?\,?\ ?
446+
(?P<line1>
447+
{street_number}\,?\ ?
448+
{street_name}?\,?\ ?
449+
(?:(?<=[\ \,]){street_type})\,?\ ?
450+
{post_direction}?
451+
)
452+
\,?\ ?
453+
(?P<line2>
454+
{floor}?\,?\ ?
449455
450-
(?P<building_id>
451-
{building}
452-
)?\,?\ ?
456+
(?P<building_id>
457+
{building}
458+
)?\,?\ ?
453459
454-
(?P<occupancy>
455-
{occupancy}
456-
)?\,?\ ?
460+
(?P<occupancy>
461+
{occupancy}
462+
)?\,?\ ?
457463
458-
{po_box}?
464+
{po_box}?
465+
)
459466
)
460467
)""".format(
461468
street_number=street_number,

pyap/source_GB/data.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -304,27 +304,31 @@
304304
(?:
305305
(?P<full_street>
306306
307-
(?:
308-
{po_box} {part_divider}? # TODO: maybe remove the '?' on the part_dividers is mismatch address parts
309-
)?
310-
(?:
311-
{floor} {part_divider}?
312-
)?
313-
(?:
314-
{occupancy} {part_divider}?
315-
)?
316-
(?:
317-
{building} {part_divider}?
318-
)?
307+
(?P<line2>
308+
(?:
309+
{po_box} {part_divider}? # TODO: maybe remove the '?' on the part_dividers is mismatch address parts
310+
)?
311+
(?:
312+
{floor} {part_divider}?
313+
)?
314+
(?:
315+
{occupancy} {part_divider}?
316+
)?
317+
(?:
318+
{building} {part_divider}?
319+
)?
320+
)
319321
320-
(?:
321-
(?: {street_number} {space} )
322-
|
323-
(?! \d{{}} )
324-
325-
)?
326-
(?:{street_name} )
327-
(?:{space} {street_type} {space}?)?
322+
(?P<line1>
323+
(?:
324+
(?: {street_number} {space} )
325+
|
326+
(?! \d{{}} )
327+
328+
)?
329+
(?:{street_name} )
330+
(?:{space} {street_type} {space}?)?
331+
)
328332
)
329333
) # end full_street
330334
""".format(

pyap/source_US/data.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -995,24 +995,28 @@ def street_type_list_to_regex(street_type_list: list[str]) -> str:
995995
(?:
996996
(?P<full_street>
997997
(?:
998-
(?:(?P<po_box_b>{po_box}){part_div})?
999-
{street_number}{space_div}?
1000-
(?:
1001-
(?:{numbered_or_typeless_street_name})
1002-
|
1003-
(?:{typed_street_name}(?![A-Za-z\d\.]))
1004-
|
998+
(?P<line1>
999+
(?:(?P<po_box_b>{po_box}){part_div})?
1000+
{street_number}{space_div}?
10051001
(?:
1006-
{post_direction_re}\
1007-
\d{{,3}}[A-Za-z\-]{{1,31}}
1002+
(?:{numbered_or_typeless_street_name})
1003+
|
1004+
(?:{typed_street_name}(?![A-Za-z\d\.]))
1005+
|
1006+
(?:
1007+
{post_direction_re}\
1008+
\d{{,3}}[A-Za-z\-]{{1,31}}
1009+
)
10081010
)
1011+
(?:{space_div}{post_direction})?
10091012
)
1010-
(?:{space_div}{post_direction})?
1011-
(?:{part_div}{floor})?
1012-
(?:{part_div}{building})?
1013-
(?:{part_div}?{occupancy})?
1014-
(?:{part_div}{mail_stop})?
1015-
(?:{part_div}(?P<po_box_a>{po_box}))?
1013+
(?P<line2>
1014+
(?:{part_div}{floor})?
1015+
(?:{part_div}{building})?
1016+
(?:{part_div}?{occupancy})?
1017+
(?:{part_div}{mail_stop})?
1018+
(?:{part_div}(?P<po_box_a>{po_box}))?
1019+
)?
10161020
)
10171021
|
10181022
(?P<po_box_c>{po_box})

tests/test_parser.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def test_address_class_init():
4343
city="CityVille, ",
4444
full_street="Street 1b",
4545
full_address="Street 1b CityVille USA",
46+
line1="Street 1b",
4647
)
4748
assert addr.region1 == "USA"
4849

@@ -94,6 +95,8 @@ def test_combine_results():
9495
"postal_code": "44483",
9596
"full_address": "2590 Elm Road NE - Warren, OH 44483, US",
9697
"country": "US",
98+
"line1": "2590 Elm Road NE",
99+
"line2": "",
97100
},
98101
),
99102
(
@@ -106,6 +109,8 @@ def test_combine_results():
106109
"region1": "FL",
107110
"postal_code": "32746",
108111
"full_address": "899 HEATHROW PARK LN 02-2135\nLAKE MARY, FL 32746",
112+
"line1": "899 HEATHROW PARK LN",
113+
"line2": "02-2135",
109114
},
110115
),
111116
(
@@ -119,6 +124,8 @@ def test_combine_results():
119124
"region1": "FL",
120125
"postal_code": "32547",
121126
"full_address": "696 BEAL PKWY NW\nFT WALTON BCH FL 32547",
127+
"line1": "696 BEAL PKWY NW",
128+
"line2": "",
122129
},
123130
),
124131
(
@@ -134,6 +141,8 @@ def test_combine_results():
134141
"full_address": (
135142
"225 E. John Carpenter Freeway, Suite 1500 Irving, Texas 75062"
136143
),
144+
"line1": "225 E. John Carpenter Freeway",
145+
"line2": "Suite 1500",
137146
},
138147
),
139148
(
@@ -146,6 +155,8 @@ def test_combine_results():
146155
"region1": None,
147156
"postal_code": "49441",
148157
"full_address": "1300 E MOUNT GARFIELD ROAD, NORTON SHORES 49441",
158+
"line1": "1300 E MOUNT GARFIELD ROAD",
159+
"line2": "",
149160
},
150161
),
151162
(
@@ -158,6 +169,8 @@ def test_combine_results():
158169
"city": "Richfield",
159170
"region1": "MN",
160171
"postal_code": "55423",
172+
"line1": "7601 Penn Avenue South",
173+
"line2": "",
161174
},
162175
),
163176
(
@@ -169,6 +182,8 @@ def test_combine_results():
169182
"city": "LOS ANGELES",
170183
"region1": "CA",
171184
"postal_code": None,
185+
"line1": "242 N AVENUE 25",
186+
"line2": "SUITE 300",
172187
},
173188
),
174189
(
@@ -181,6 +196,8 @@ def test_combine_results():
181196
"city": "San Ramon",
182197
"region1": "CA",
183198
"postal_code": "94583-2176",
199+
"line1": "2633 Camino Ramon",
200+
"line2": "Ste. 400",
184201
},
185202
),
186203
(
@@ -194,6 +211,8 @@ def test_combine_results():
194211
"region1": "KS",
195212
"po_box": "PO Drawer J",
196213
"postal_code": "67530",
214+
"line1": "2006 Broadway Ave",
215+
"line2": "Suite 2A, PO Drawer J",
197216
},
198217
),
199218
(
@@ -206,6 +225,8 @@ def test_combine_results():
206225
"city": "Houston",
207226
"region1": "TX",
208227
"postal_code": "77030-3411",
228+
"line1": "One Baylor Plaza",
229+
"line2": "MS: BCM204",
209230
},
210231
),
211232
(
@@ -218,6 +239,8 @@ def test_combine_results():
218239
"city": "Amazeville",
219240
"region1": "AL",
220241
"postal_code": "12345",
242+
"line1": "2817 PETERS ROAD",
243+
"line2": "BAY 52",
221244
},
222245
),
223246
(
@@ -230,6 +253,8 @@ def test_combine_results():
230253
"city": "SAN FRANCISCO",
231254
"region1": "CA",
232255
"postal_code": "94131",
256+
"line1": "6325F OPAL HEIGHTS BL",
257+
"line2": "",
233258
},
234259
),
235260
(
@@ -242,6 +267,8 @@ def test_combine_results():
242267
"city": "LEHI",
243268
"region1": "UT",
244269
"postal_code": "84043",
270+
"line1": "2744W GRANDIOSE WAY",
271+
"line2": "#100",
245272
},
246273
),
247274
(
@@ -254,6 +281,8 @@ def test_combine_results():
254281
"city": "ST. LOUIS",
255282
"region1": "MO",
256283
"postal_code": "63101",
284+
"line1": "532 N 9TH STREET",
285+
"line2": "",
257286
},
258287
),
259288
],

0 commit comments

Comments
 (0)