Skip to content

Commit

Permalink
Added simple levenshtein distance algorithm to stations route
Browse files Browse the repository at this point in the history
  • Loading branch information
gillesdemey committed Dec 21, 2014
1 parent 90b1550 commit b526d96
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
27 changes: 26 additions & 1 deletion db/stations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

var redis = require('redis'),
client = redis.createClient(),
_ = require('lodash-node');
_ = require('lodash-node'),
leven = require('leven');

exports.byId = function(id, callback) {
client.hgetall('station:' + id, function(err, station) {
Expand All @@ -27,3 +28,27 @@ exports.deserialize = function(object) {

return object;
};

/**
* Lookup the ID of a train station based on the Levenshtein distance algo
*/
exports.levenLookup = function(needle, callback) {

client.HGETALL(['station:names'], function(err, stations) {

if (err)
return callback(err);

var results = [];

// itterate over stations to find lowest levenshtein distance string
_.forOwn(stations, function(id, station) {
var score = leven(needle, station);
if (score < 5 ) // if the score is larger than 5, it's probably nonsense
results.push({ score: score, id: id, station: station });
});

return callback(null, _.min(results, 'score'));
});

};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"debug": "~0.7.4",
"express": "~4.2.0",
"hiredis": "^0.1.17",
"leven": "^1.0.1",
"locale": "0.0.17",
"lodash-node": "^2.4.1",
"moment": "^2.8.1",
Expand Down
52 changes: 37 additions & 15 deletions routes/stations.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,17 @@
'use strict';

var express = require('express'),
ex = require('../utils/error'),
request = require('request'),
router = express.Router(),
_ = require('lodash-node'),
maps = require('../maps'),
utils = require('../utils'),
datetime = require('../utils/datetime'),
db = require('../db/stations'),
OAuth = require('../utils/oauth');

/* GET status */
router.get('/:id([0-9]+)/:subset(arrivals|departures)?', function(req, res, next) {

var dateTimeFrom = datetime.toCET().format('YYYY-MM-DD HH:mm:ss');

var dateTimeTo = datetime.toCET()
.add(1, 'hours')
.format('YYYY-MM-DD HH:mm:ss');

var params = {
'dateTimeFrom' : dateTimeFrom,
'dateTimeTo' : dateTimeTo,
'searchType' : 0,
'stationID' : req.params.id
};
function makeRequest(req, res, next, params) {

request.get(new OAuth('RetrieveStationSchedule', params), function(err, resp, body) {

Expand Down Expand Up @@ -65,6 +53,40 @@ router.get('/:id([0-9]+)/:subset(arrivals|departures)?', function(req, res, next
});
});

}

/* GET status */
router.get('/:station/:subset(arrivals|departures)?', function(req, res, next) {

var dateTimeFrom = datetime.toCET().format('YYYY-MM-DD HH:mm:ss');

var dateTimeTo = datetime.toCET()
.add(1, 'hours')
.format('YYYY-MM-DD HH:mm:ss');

var params = {
'dateTimeFrom' : dateTimeFrom,
'dateTimeTo' : dateTimeTo,
'searchType' : 0,
};

if (isFinite(req.params.station)) {
params.stationID = req.params.station;
makeRequest(req, res, next, params);
}

else if (isNaN(req.params.station))
db.levenLookup(req.params.station, function(err, result) {

params.stationID = result.id;

// no results from lookup returns Infinity
if (isFinite(result.score))
makeRequest(req, res, next, params);
else
return res.json(404, new ex.StationNotFoundException());
});

});

/* GET index */
Expand Down
4 changes: 4 additions & 0 deletions utils/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ exports.UnknownException = function(msg, code) {
return new exports.Exception(msg, undefined, code);
};

exports.StationNotFoundException = function() {
return new exports.Exception('Could not find station', 'StationNotFoundException', 404);
};

exports.Exception = function(msg, type, code) {
return {
'error' : {
Expand Down

0 comments on commit b526d96

Please sign in to comment.