Address line parser
An address provider returns addresses only with concatenated street names and numbers. Our own system on the other hand has separate fields for street name and street number.
Input: string of address
Output: string of street and string of street-number as JSON object
-
Write a simple program that does the task for the simplest cases, e.g.
"Winterallee 3"
->{"street": "Winterallee", "housenumber": "3"}
"Musterstrasse 45"
->{"street": "Musterstrasse", "housenumber": "45"}
"Blaufeldweg 123B"
->{"street": "Blaufeldweg", "housenumber": "123B"}
-
Consider more complicated cases
"Am Bächle 23"
->{"street": "Am Bächle", "housenumber": "23"}
"Auf der Vogelwiese 23 b"
->{"street": "Auf der Vogelwiese", "housenumber": "23 b"}
-
Consider other countries (complex cases)
"4, rue de la revolution"
->{"street": "rue de la revolution", "housenumber": "4"}
"200 Broadway Av"
->{"street": "Broadway Av", "housenumber": "200"}
"Calle Aduana, 29"
->{"street": "Calle Aduana", "housenumber": "29"}
"Calle 39 No 1540"
->{"street": "Calle 39", "housenumber": "No 1540"}
It requires a Python interpreter and has been tested with version 3.8.1 and macOS. But it should be platform agnostic and run on Linux and Windows.
The application extracts the street name and house number using regular expressions and simple logic.
There have been used 3 different approaches to try to extract the corresponding address components.
They are evaluated one at a time. When the parser matches with one of the cases for the input address line, it's not necessary
to continue with others.
When the parser doesn't meet any of the 3 given criteria, then the algorithm moves from the end of the address string to the beginning until it meets the number. Therefore everything to the right of the number, including the number itself is supposed to be a house number.
(?P<street>.+\d+)\s(?P<house_number>(?:No|Nr|no|nr)\.?\s?\d+\s?[a-zA-Z]?)
It will match addresses with the following format:
- Calle 39 No 1540
- Calle 39 nr. 1540
has_num(splitted_address[-1])
It will match addresses with the trailing house number:
- Winterallee 3
- Musterstrasse 45
- Blaufeldweg 123B
- Am Bächle 23
- Calle Aduana, 29
- 200th century Fox Av. 1568
has_num(splitted_address[0])
It will match addresses with the following format:
- 200 Broadway Av
- 4, rue de la revolution
- 123 W 34th St
for ix in reversed(range(len(splitted_address)))
It will match addresses with the following format:
- Auf der Vogelwiese 23 b
Clone the project executing the following command in a terminal:
git clone https://github.com/plahoi/address_parser.git
Move to the project directory using:
cd address_parser
Execute the following:
python3 main.py [OPTIONS] [ADDRESS_LINE]
-s Address line to be parsed
Parse the address "Rue des Francs, 70":
python3 main.py -s 'Rue des Francs, 70'
Output:
{'street': 'Rue des Francs', 'housenumber': '70'}
Parse the address "Calle 39 No 1540B":
python3 main.py -s 'Calle 39 No 1540B'
Output:
{'street': 'Calle 39', 'housenumber': 'No 1540B'}
To run tests, in the project directory run the following command:
python3 -m unittest test.test_parser