-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #169 from pelias/ngram-address-parser
Address Parser
- Loading branch information
Showing
18 changed files
with
788 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* These values specify how much a document that matches a certain _type | ||
* should be boosted in elasticsearch results. | ||
*/ | ||
|
||
module.exports = { | ||
'admin0': 4, | ||
'admin1': 3, | ||
'admin2': 2, | ||
'local_admin': 1, | ||
'locality':1, | ||
'neighborhood':1 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
|
||
var parser = require('addressit'); | ||
var extend = require('extend'); | ||
var get_layers = require('../helper/layers'); | ||
var delim = ','; | ||
|
||
module.exports = function(query) { | ||
|
||
var tokenized = query.split(/[ ,]+/); | ||
var hasNumber = /\d/.test(query); | ||
|
||
var getAdminPartsBySplittingOnDelim = function(query) { | ||
// naive approach - for admin matching during query time | ||
// split 'flatiron, new york, ny' into 'flatiron' and 'new york, ny' | ||
var delimIndex = query.indexOf(delim); | ||
var address = {}; | ||
if ( delimIndex !== -1 ) { | ||
address.name = query.substring(0, delimIndex); | ||
address.admin_parts = query.substring(delimIndex + 1).trim(); | ||
} | ||
|
||
return address; | ||
}; | ||
|
||
var getTargetLayersWhenAddressParsingIsNotNecessary = function(query) { | ||
var address = {}; | ||
// set target_layer if input length <= 3 characters | ||
if (query.length <= 3 ) { | ||
// no address parsing required | ||
address.target_layer = get_layers(['admin']); | ||
} else if (tokenized.length === 1 || (tokenized.length < 3 && !hasNumber)) { | ||
// no need to hit address layers if there's only one (or two) token(s) | ||
address.target_layer = get_layers(['admin', 'poi']); | ||
} | ||
|
||
return address.target_layer ? address : null; | ||
}; | ||
|
||
var getAddressParts = function(query) { | ||
// address parsing | ||
var address = parser( query ); | ||
// set target_layer if input suggests no address | ||
if (address.text === address.regions.join(' ') && !hasNumber) { | ||
address.target_layer = get_layers(['admin', 'poi']); | ||
} | ||
|
||
return address; | ||
}; | ||
|
||
var addressWithAdminParts = getAdminPartsBySplittingOnDelim(query); | ||
var addressWithTargetLayers= getTargetLayersWhenAddressParsingIsNotNecessary(query); | ||
var addressWithAddressParts= !addressWithTargetLayers ? getAddressParts(query) : {}; | ||
|
||
var parsedAddress = extend(addressWithAdminParts, | ||
addressWithTargetLayers, | ||
addressWithAddressParts); | ||
|
||
var address_parts = [ 'name', | ||
'number', | ||
'street', | ||
'city', | ||
'state', | ||
'country', | ||
'postalcode', | ||
'regions', | ||
'admin_parts', | ||
'target_layer' | ||
]; | ||
|
||
var parsed_input = {}; | ||
|
||
address_parts.forEach(function(part){ | ||
if (parsedAddress[part]) { | ||
parsed_input[part] = parsedAddress[part]; | ||
} | ||
}); | ||
|
||
return parsed_input; | ||
}; | ||
|
||
|
||
// parsed_input = { | ||
// name : parsedAddress.name, | ||
// number : parsedAddress.number, | ||
// street : parsedAddress.street, | ||
// city : parsedAddress.city, | ||
// state : parsedAddress.state, | ||
// country: parsedAddress.country, | ||
// postalcode : parsedAddress.postalcode, | ||
// regions: parsedAddress.regions, | ||
// admin_parts: parsedAddress.admin_parts, | ||
// target_layer: parsedAddress.target_layer | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.