I'm trying to extract address details from very ugly free text:
import regex
pat_addr_verbose = """(?ix) # case insensitive and verbose flag
(?:(?:BND|BY|CNR|OF)\W+)* # non-capturing (list)
(?:(?!RD|HWY|TRAIL|St) # negative lookahead (list of street types)
(?: # either
(?P<n_start>\d+)-(?P<n_end>\d+) # number sequence
|(?<!-)(?P<n>\d+) # single number
)\W+)? # No number, maybe non word character follows
(?P<name>
(?:
(?!RD|HWY|TRAIL|St)\w+\W*)+)\W+ # capturing words not preceded by (list of street types)
(?P<type>RD|HWY|TRAIL|St)* # non-capturing (list of street types)
"""
pat_addr = regex.compile(pat_addr_verbose, regex.IGNORECASE & regex.VERBOSE)
text = """BND BY THOMAS RAIL TRAIL, 7 SNOW WHITE HWY & MICKEY RD,
337-343 BOGEYMAN RD, 4, 8, 9-13, 16-18 Fictional Rd & 17 Elm St"""
regex.findall(pat_addr, text)
I'm getting the right results for simple addresses, but I'm failing to get the many different street numbers in Fictional Road
[m.groupdict() for m in pat_addr.finditer(text)]
[{'n': None,
'n_end': None,
'n_start': None,
'name': 'THOMAS RAIL',
'type': 'TRAIL'},
{'n': '7',
'n_end': None,
'n_start': None,
'name': 'SNOW WHITE',
'type': 'HWY'},
{'n': None, 'n_end': None, 'n_start': None, 'name': 'MICKEY', 'type': 'RD'},
{'n': None,
'n_end': '343',
'n_start': '337',
'name': 'BOGEYMAN',
'type': 'RD'},
{'n': '4',
'n_end': None,
'n_start': None,
'name': '8, 9-13, 16-18 Fictional',
'type': 'Rd'},
{'n': '17', 'n_end': None, 'n_start': None, 'name': 'Elm', 'type': 'St'}]
I wonder if it is possible to either get a list
of numbers (doesn't matter if they're not named) or a dict
for them in regex?
EDIT: This is what I expect to get:
Option 1:
{'numbers':
[
{
'n': '4',
'n_end': None,
'n_start': None,
},
{
'n': '8',
'n_end': None,
'n_start': None,
},
{
'n': None,
'n_end': '13',
'n_start': '9',
},
{
'n': None,
'n_end': '18',
'n_start': '16',
}
],
'name': 'Fictional',
'type': 'Rd'},
Option 2:
{'numbers':
[
'4',
'8',
'9-13',
'16-18'
],
'name': '8, 9-13, 16-18 Fictional',
'type': 'Rd'},