Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for libpostal over http service, adapter pattern et al. #249

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"node": true,
"curly": true,
"eqeqeq": true,
"esversion": 6,
"esversion": 8,
"freeze": true,
"immed": true,
"indent": 2,
Expand Down
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
missinglink marked this conversation as resolved.
Show resolved Hide resolved

# dependencies
RUN apt-get update && \
Expand Down
7 changes: 4 additions & 3 deletions api/extract.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Database = require('better-sqlite3');
const query = { extract: require('../query/extract') };
const analyze = require('../lib/analyze');
const asyncForEach = require('../lib/asyncForEach');

// export setup method
function setup( addressDbPath, streetDbPath ){
Expand All @@ -12,16 +13,16 @@ function setup( addressDbPath, streetDbPath ){
db.exec(`ATTACH DATABASE '${streetDbPath}' as 'street'`);

// query method
var q = function( coord, names, cb ){
var q = async function( coord, names, cb ){

var point = {
lat: parseFloat( coord.lat ),
lon: parseFloat( coord.lon )
};

var normalized = [];
names.forEach( function( name ){
normalized = normalized.concat( analyze.street( name ) );
await asyncForEach(names, async (name) => {
normalized = normalized.concat( await analyze.street( name ) );
});

// error checking
Expand Down
4 changes: 2 additions & 2 deletions api/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function setup( addressDbPath, streetDbPath ){
db.exec('PRAGMA street.mmap_size=268435456;');

// query method
var q = function( coord, number, street, cb ){
var q = async function( coord, number, street, cb ){

var point = {
lat: parseFloat( coord.lat ),
Expand All @@ -31,7 +31,7 @@ function setup( addressDbPath, streetDbPath ){

var normalized = {
number: analyze.housenumber( number ),
street: analyze.street( street )
street: await analyze.street( street )
};

// error checking
Expand Down
4 changes: 2 additions & 2 deletions cmd/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ app.use('/demo', express.static('demo'));
// app.use('/builds', express.static('/data/builds'));
// app.use('/builds', directory('/data/builds', { hidden: false, icons: false, view: 'details' }));

app.listen( PORT, HOST, function() {
app.listen( PORT, HOST, async function() {

// force loading of libpostal
analyze.street( 'test street' );
await analyze.street( 'test street' );

console.log(util.format( 'server listening on %s:%s', HOST || '0.0.0.0', PORT ));
});
34 changes: 7 additions & 27 deletions lib/analyze.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
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.
**/
function street( streetName ){
const postal = get_libpostal();
async function street( streetName ){

// use libpostal to expand the address
var expansions = postal.expand.expand_address( streetName );
let expansions = await postal.expand.expand_address( streetName );

// remove ordinals
expansions = expansions.map(function( item ){
Expand Down
8 changes: 8 additions & 0 deletions lib/asyncForEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// async friendly version of Array.forEach
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}

module.exports = asyncForEach;
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');
}
missinglink marked this conversation as resolved.
Show resolved Hide resolved
Loading