I'm getting address (physical address, not digital) input that's obfuscated, and looks like the following:
The plaintext version:
'39 Jerrabomberra Ave. Narrabundah Canberra 2604 Australia'
The obfuscated version:
['39 Jerrabomberra Ave., Narrabundah', 'Canberra', ' ', '2604', ', ', 'Australia', '39 Jerrabomberra Ave., Narrabundah', 'Canberra 2604, ', 'Australia']
Usually the obfuscation is simple duplication and rearranging, which my script catches, however there are a few edge cases that are missed, which I'm working on catching.
However, my solution feels like it could be simplified.
The logic follows this order:
- Join the array into one long string with a space as the 'glue' character.
- Use
re.sub
to find all commas and remove them. - Split by space
- Add each non-empty component to the components array if it is not already in there.
- Join the components together.
import re
...
address = fooGetAddress(foo[bar]) #returns an array
address_components = []
for component in re.sub(",", "", " ".join(address)).split(" "):
if component not in address_components and component is not "":
address_components.append(component)
address = " ".join(address_components)