One benefit of using Julia for playing with statistics is that it’s a modern, usable language for general purposes as well. Earlier I built a k-Nearest Neighbor classifier for predicting crime from the data on hbg-crime.org, so I thought it would be fun to build a web service in Julia that could return those results.
Morsel is a small Sinatra-like web framework for Julia that we’re going to be using.
Here’s a quick function to handle building a JSON response out of a hash; we just set the Content-Type header on the response in place, and then write out our response data as JSON.
This is how we’re going to parse the params for a request, which will
look like this:
lon we just call
parsefloat on the string we receive; for
many nearest neighbors to use) we use
parseint, and for
either parse a string we receive or use the current time.
Here’s our only route. We are pulling the params out of the
req.state hash, and either rendering an error if
aren’t specified, or returning the result of our prediction.
And then we start the app; once the kNN engine parses the reports data
and is ready to go, you’ll see a ready message from Morsel and you can
start making requests to