Skip to content

Instantly share code, notes, and snippets.

@adrianseeley
Created November 18, 2012 15:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianseeley/4105774 to your computer and use it in GitHub Desktop.
Save adrianseeley/4105774 to your computer and use it in GitHub Desktop.
Crowd Complete :: a NodeJS Snippit for Ranked Crowd Sourced Auto Complete (Auto Complete Artificial Neural Network)
/* Crowd Complete :: a NodeJS Snippit for Ranked Crowd Sourced Auto Complete (Auto Complete Artificial Neural Network)
Usage:
Requires a running mongo database (db_url)
No solutions exist until you build them over time by 'feeding' the database with /fb.
/ac?e=getAutocompleteSolutionsFromAcDatabase
/fb?e=thisTextIsACompleteUserEntryThatIsBeingFedBackToTheAcDatabase
What is this?
TL;DR - Google's magic autocomplete bar.
Autocomplete by example: is when you type 'Crow', and you are provided with a list of suggestions to complete
your entry based on what you have entered in the past, say ['Crowd Complete', 'Crow Storm', 'Crows', etc.]. I hope in your mind you are
imagining the little bar dropping down with suggestions.
Autocomplete can easily be improved by a process called 'ranking'.
Lets imagine I enter 'Crowd Complete' twenty times, and I also enter 'Crow Storm' ten times.
Because I have historically entered Crowd Complete (20), more than Crow Storm (10), I would like them to appear in that order.
This is known as ranking.
Autocomplete is improved further by crowd sourcing.
Crowd sourcing means we take the entries of everyone and combine them all together.
This can yield incredible results over very little time.
*/
var db_url = 'ac';
var db = require('mongojs').connect(db_url, ['ac_ann']);
var http = require('http');
var url = require('url');
function onConnection(request, response)
{
var parsed = url.parse(request.url, true).query;
if (request.url.slice(0, 4) == '/ac?') ac(parsed, response); else if (request.url.slice(0, 4) == '/fb?') fb(parsed, response); else qq(response);
}
function ac(parsed, response)
{
var e = parsed.e;
var ac = [];
db.ac_ann.find({e: e}).sort({h:-1},
function(err, docs)
{
if(err) dberr(response);
else
{
for(var d = 0; d < docs.length; d++) ac.push(docs[d].ac);
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify({status : "groovy", ac: ac}));
}
}
);
}
function fb(parsed, response)
{
response.writeHead(200, {'Content-Type': 'application/json'});
response.end(JSON.stringify({status : "groovy"}));
var e = parsed.e;
fbr(e, e);
}
function fbr(p, ac)
{
db.ac_ann.findOne({e: p, ac: ac},
function(err, doc)
{
if(err) console.log("DB ERROR");
if(doc)
{
doc.h++;
db.ac_ann.save(doc);
if(p.length > 1) fbr(p.slice(0, p.length - 1), ac);
}
else
{
db.ac_ann.save({e: p, ac: ac, h: 1});
if(p.length > 1) fbr(p.slice(0, p.length - 1), ac);
}
}
);
}
function dberr(response)
{
console.log("DB ERROR");
response.writeHead(500, {'Content-Type': 'application/json'});
response.end(JSON.stringify({status : "jive turkey"}));
}
function qq(response)
{
response.writeHead(400, {'Content-Type': 'application/json'});
response.end(JSON.stringify({status : "jive turkey"}));
}
var port = 80;
http.createServer(onConnection).listen(port);
console.log("Server running at http://localhost:" + port + "/");
console.log("DB: " + db_url);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment