I'm trying to reduce the time users need to spent on filling in an address form. This form requires the street address, postal code, city, district, and sub district.
To do so I query Open Street Map's Nominatim API like so:
var
request = require('request'),
address = 'Grand Parkview Asoke, Unit 255/109 15th Flr., Sukhumvit 21 Road',
baseUri = 'http://nominatim.openstreetmap.org/search?format=json&addressdetails=1',
query = '&accept-language=en&q=' + encodeURIComponent(address);
request(baseUri + query, function(err, res, body) {
console.log(JSON.parse(body));
});
I then parse things like the postcode from the returned body.
The problem is that this only works for "normal addresses" that do not contain irrelevant things like the floor number. In other words, this works:
var address = 'Sukhumvit 21 Road';
But this does not work:
var address = 'Grand Parkview Asoke, Unit 255/109 15th Flr., Sukhumvit 21 Road';
Now I am querying the API many times with a very crude set of possible trials, like so:
//create trials
var
trials = [],
addressParts = address.split(' ');
for (var i = 0, il = addressParts.length; i < il; i++) {
if (il - i >= 2) trials.push(addressParts.slice(i, il).join(' '));
}
Which means that it will attempt all of these strings:
Grand Parkview Asoke, Unit 255/109 15th Flr., Sukhumvit 21 Road
Parkview Asoke, Unit 255/109 15th Flr., Sukhumvit 21 Road
Asoke, Unit 255/109 15th Flr., Sukhumvit 21 Road
Unit 255/109 15th Flr., Sukhumvit 21 Road
255/109 15th Flr., Sukhumvit 21 Road
15th Flr., Sukhumvit 21 Road
Flr., Sukhumvit 21 Road
Sukhumvit 21 Road ==> it works!
Which requires many requests and is therefore very slow.
Is there a smarter way to filter out this "non-address" information? Note that I'm also looking for a way to do this in non-western scripts such as Thai.