Freebase Suggest is a little JavaScript (and jQuery) widget for adding Freebase's auto-complete and search API reconciliation features to any of your text boxes. While this might sound a bit strange or pointless initially, its actually incredibly useful in many applications Imagine you are running a site for your own music reviews. You have a nice web form for writing the reviews, which includes fields for artist name and album. It can be a pain sometimes to remember the exact album title or band name, and you probably find yourself having to double-check this a few times before posting your review. However, chances are high that your artist and album are already available in Freebase, and this is where the Freebase Suggest widget comes in. You simply attach the widget to your artist field, and your album field. Now by default, you can auto-complete to any topic in Freebase. This is pretty good, but wait there's more! You can trivially nail down the types returned by the auto-complete service to just be, say, /music/album. You could do the same for your musical artist box by limiting to /music/artist. To see what this Freebase Suggest widget looks like in this configuration, check out some of the examples. So now you know how to add Freebase Suggest to your web forms to reduce friction on input. You also know how to nail down the auto-complete results to a specific type. But what if you have a single input box which accepts, say a more generic 'media title'. This could be a film name, a TV show or a music album. Again, it would be really nice here to be able to use Freebase Suggest to leverage the already-entered and verified data of Freebase to ease input in your form - but how do you make it work with multiple types? This is something I wanted to do in one of my own applications today, and its very easy! In order to limit your Freebase Suggest results to a specific type, you already have some code setting up the ac_param dictionary passed to the widget. That code probably looks something like this:

var o = { type:'/music/album' };
$(id).freebaseSuggest({ac_param:o});
$(id).bind("fb-select", suggest_cb);
Now, to adapt this so that it also handles completion of films and TV shows, we simply add multiple type keys to our options dictionary, like so:
var o = { type:'/music/album', 
          type:'/film/film',
          type:'/tv/tv_program' };
$(id).freebaseSuggest({ac_param:o});
$(id).bind("fb-select", suggest_cb);
And there you have it! Freebase Suggest results restricted to multiple types.

Niall O'Higgins is an author and software developer. He wrote the O'Reilly book MongoDB and Python. He also develops Strider Open Source Continuous Deployment and offers full-stack consulting services at FrozenRidge.co.

blog comments powered by Disqus