Skip to content

Commit

Permalink
feat(libpostal): add support for libpostal over http service, adapter…
Browse files Browse the repository at this point in the history
… pattern et al.
  • Loading branch information
missinglink authored and orangejulius committed May 17, 2021
1 parent 6bab7e1 commit fb8d77e
Show file tree
Hide file tree
Showing 11 changed files with 1,557 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# base image
FROM pelias/libpostal_baseimage
FROM pelias/baseimage

# dependencies
RUN apt-get update && \
Expand Down
30 changes: 5 additions & 25 deletions lib/analyze.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
const postal = require('../libpostal/client');

// constants for controlling how we parse ranges, eg: 'α-β'
// some ranges such as '1-7' are ambiguous; it could mean 'apt 7, no 1'; or
// it could mean 'apt 1, no 7'; or could even be a valid range 'one to seven'.
// note: these values provide a means of setting some sane defaults for which
// ranges we try to parse and which ones we leave.
var MIN_RANGE = 1; // the miniumum amount β is higher than α
var MAX_RANGE = 6; // the maximum amount β is higher than α
var MIN_RANGE_HOUSENUMBER = 10; // the minimum acceptible value for both α and β

/*
* Return the appropriate version of node-postal
*/

var _nodepostal_module;
function get_libpostal() {
// lazy load this dependency; since it's large (~2GB RAM) and may be
// accidentally required by a process which doesn't use it.
if (!_nodepostal_module) {
// load the mock library if MOCK_LIBPOSTAL env var is set
if (process.env.MOCK_LIBPOSTAL) {
_nodepostal_module = require('../test/lib/mock_libpostal');
// otherwise load the real thing
} else {
_nodepostal_module = require('node-postal');
}
}

return _nodepostal_module;
}
const MIN_RANGE = 1; // the miniumum amount β is higher than α
const MAX_RANGE = 6; // the maximum amount β is higher than α
const MIN_RANGE_HOUSENUMBER = 10; // the minimum acceptible value for both α and β

/**
analyze input streetname string and return a list of expansions.
**/
async function street( streetName ){
const postal = get_libpostal();

// use libpostal to expand the address
let expansions = await postal.expand.expand_address( streetName );
Expand Down
17 changes: 17 additions & 0 deletions libpostal/LibpostalServiceConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const ServiceConfiguration = require('pelias-microservice-wrapper').ServiceConfiguration;

class LibpostalServiceConfig extends ServiceConfiguration {
constructor(config) {
super('libpostal', config);
}
getUrl(params) {
return this.baseUrl + params.endpoint;
}
getParameters(params) {
return {
address: params.address
};
}
}

module.exports = LibpostalServiceConfig;
21 changes: 21 additions & 0 deletions libpostal/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Return the appropriate version of node-postal
*/

const config = require('pelias-config').generate();
const serviceIsConfigured = config.get('services.libpostal') || config.get('api.services.libpostal');

// load the mock library if MOCK_LIBPOSTAL env var is set
if (process.env.MOCK_LIBPOSTAL) {
module.exports = require('./mock');
}

// else use the HTTP webservice when configured
else if (serviceIsConfigured) {
module.exports = require('./service');
}

// otherwise use the npm module
else {
module.exports = require('./module');
}
Loading

0 comments on commit fb8d77e

Please sign in to comment.