Ajax Bestiary: A Javascript Field Guide
 
Ajax Bestiary: A Javascript Field Guide
 
 

Entries Tagged as 'NodeJS'

TWSS: Naive Bayes Classifier and k-Nearest Neighbor library for NodeJS

Posted by Dave Mahon

TWSS is an acronym that stands for That’s What She Said, a joke frequently abused by the character Michael Scott on the US version of The Office. Why would anyone write a library that tells you whether or not "That’s what she said!" is an "appropriate" response to a given sentence? Frankly, I don’t know. However, during implementation, he created a generalized document NBC and k-NN tool that runs cleanly inside NodeJS.

In effect, with different training material, this is a powerful tool with an MIT license. Its also potentially very expandable.

So let’s see it in action.

Set the algorithm:

twss.algo = 'nbc';

Set the tolerance threshold:

twss.threshold = 0.5;
twss.is("You're hardly my first."); // false
 
twss.threshold = 0.3;
twss.is("You're hardly my first."); // true

Or, for your customized tool, return the probability:

twss.prob("The juice keeps coming out of the wrong hole!"); // 0.9961630818418142

How do we expand our algorithm selection? Look in /lib/twss.js:

var classify = {
  nbc: require('./classifier/nbc'),
  knn: require('./classifier/knn')
};

As you can see, adding algorithms is primarily a matter of a new file in the classifier subdirectory and adding a member element to the classify object. The new classifier must implement getTwssProbability() and isTwss().

However, you will have to lightly edit exports.probability() and exports.is() to recognize your custom algorithm.

UPDATE My apologies for the links to Wikipedia on the day of the SOPA blackout.

Access Amazon’s EC2 (and more) from within NodeJS

Posted by Dave Mahon

At first glance it might seem silly, but when you’re running your app from within NodeJS, why wouldn’t you want to store the data in the cloud and let your web server do just that – serve content? Unfortunately, Amazon does not provide a generalized solution for Amazon Web Services (AWS).

This inspired Mirko Keifer to develop just such a module. This might not be worthy of mention in and of itself, but he made it a generalized solution, so it can also handle other API’s, including Product Advertising, SimpleDB, Simple Email Service (SES) and Elastic Load Balancing Service (ELB).

Naturally, basic examples are provided:

var aws = require("aws-lib");
 
ec2 = aws.createEC2Client(yourAccessKeyId, yourSecretAccessKey);
 
ec2.call("DescribeInstances", {}, function(result) {
  console.log(JSON.stringify(result));
})

This returns a JSON response relevant to EC2.

You can also pass a third parameter of an object specifying options, like so:

ec2 = aws.createEC2Client(yourAccessKeyId, yourSecretAccessKey, {version: '2010-08-31'});

This instantiates the EC2 client, but using a previous version of the API.

The slick part is because each service is a wrapper for his core AWS class, each can be implemented in about 33 lines of code, which makes it simple to understand the relevant options. (Currently, permissible options are only documented in code).

Find it on GitHub.

Batman.js: Micro-framework with macro features

Posted by Dave Mahon

Nick Small has released batman.js, a framework for building rich web applications with CoffeeScript or JavaScript.

Supplying an MVC architecture, data-bound HTML views and support for NodeJS, the framework is designed for single-page apps. The APIs are drawn from Rails, so RoR developers in particular will get started quickly. (See the batman-classifieds project).
Keep reading →

Moment.js: The successor to Date.js

Posted by Dave Mahon

We’ve discussed Date.js before. It is an amazing library that makes date manipulation simple. On their home page, perhaps the most elegant line of code is (3).days().ago();

There is a downside though – it’s abandoned. Its last full release was November 2007 and code was last committed May 2008. Moment.js, on the other hand, is still under active development.

It also has integrated support for NodeJS, but what’s particularly cool about it is the emphasis on human-friendly times. The from() and fromNow() methods will return strings like “in 5 days” or “a year ago”, while the format() method can simply be passed “LL” to display a localized date, without the time, simplifying development.

It also adds a diff() method, quickly returning the difference between two moment objects, but in units of your choice.

That said, there are some things it doesn’t do that Date.js does. The comparison methods like compareTo() and equals() are nowhere to be seen, although you could certainly use diff() to emulate them.

Likewise, hasDaylightSavingTime() can quickly be rewritten as

return moment({month: 0, day: 1}).getTimezoneOffset() !=
moment({month: 6, day: 1}).getTimezoneOffset();


which is actually more succinct than the Date.js implementation.