Smorgasbord - Politics, Lisp, Rails, Fencing, etc.

My musings on assortment of things ranging from politics, computer technology and programming to sports.

Friday, August 25, 2006


On this day:

India vis-a-vis China

An old but nice summarized comparison drawing from articles by Yasheng Huang, a business professor at MIT Sloan and his book.

Maybe someday when I am in the mood to write I will post my own rant on some of the problems which small startups face in India.

Thursday, August 24, 2006


On this day:

acts_as_clusterable

This plugin makes it very easy to cluster active record objects. It uses the clusterer Ruby gem.

Download and install the gem, and its dependencies. Copy the plugin into vendor/plugins from the following svn repository


http://opensvn.csie.org/acts_as_clusterable/


Add the following lines to your model:


acts_as_clusterable :fields => ['title','text']


If no fields are given then it will use all text and string fields present in the model.
Now, doing clustering is as easy as:


Joke.hierarchical_clustering()
Joke.kmeans_clustering()


By default the number of clusters is Math.sqrt(no. of objects). To get custom no. of clusters give the method the no. of clusters as an argument, i.e.,


Joke.hierarchical_clustering(2)
Joke.kmeans_clustering(2)


For better results use hierarchical clustering, for faster results use kmeans.



Update: New release Clusterer + other plugins

Tuesday, August 22, 2006


On this day:

Ruby Clustering Library for Text Data

Few days back I came across, the Carrot Clustering Framework this inspired me to write something similar for Ruby. So, I started off with this project, and have right now implemented the basic K-Means and Hierarchical Clustering algorithms.

The first release can be downloaded from Rubyforge using the following command


gem install clusterer


The gem requires the stemmer gem, as a dependency.

There are also two example files which shows, how to use the library by clustering search results returned by Yahoo and Google. To try the example, the corresponding API key is needed.

Basically, one has to pass an array of strings to the clustering algorithm, and it will return the index of the clustered elements.


Clusterer::Clustering.kmeans_clustering(["hello world","mea culpa","goodbye world"])
Clusterer::Clustering.hierarchical_clustering(["hello world","mea culpa","goodbye world"])


The result might be something like [[1,3],[2]].

The method signature for K-means is as follows


def kmeans_clustering (docs, k = nil, max_iter = 10, &similarity_function)


K-means is a simple hill climbing algorithm, and can get stuck at local maxima, but it fast in nature. Just to ensure that the algorithm doesn't gets stuck in a state where it oscillates the max number of iteration is necessary.

When k=nil the algorithm finds k = Math.sqrt(docs.size) clusters.


def hierarchical_clustering (docs, k = nil, &similarity_function)


Hierarchical clustering gives much better results, but is comparatively slower, when data volume is quite high.

If you are using this gem in a live public facing site, then let me know; I would like to link to that.

Update: New release Clusterer + other plugins

Sunday, August 20, 2006


On this day:

IE the Cancer

Internet Explorer is a cancer, it has an extremely poor web standards support. A nightmare for web developers and designers.


It doesn't seems that things are likely to improve much with the IE7 release.


As the above and this slashdot articles point out http://slashdot.org/article.pl?sid=06/08/16/1315237


Hopefully one day, most web users will get smarter and switch to Firefox or some other standards friendly browser.

Friday, August 04, 2006


On this day:

Rails model - next and previous objects

Its been a long time since, I posted anything. So, I thought I will let something go.

In one of my Rails application, while showing an object I need to show links to the previous and next objects. I think, this is a common task in many Rails application, the way I am doing it is I have extended the Active Record Base class so that all my models have this function. There are numerous way to include this, choose one which you think most suits your purpose or which you like.



module ActiveRecord #:nodoc:
class Base
def next_id
@next ||= self.class.find(:first,:select => ['id'],:conditions => ['id > ?', id], :order => 'id')
@next ? @next.id : nil
end

def previous_id
@previous ||= self.class.find(:first,:select => ['id'],:conditions => ['id < ?', id], :order => "id desc")
@previous ? @previous.id : nil
end
end
end




Let this file be called 'base_ext.rb' and put it in 'lib' folder of your application.

Now, in your model files add the line


require 'base_ext'



at the top this will ensure that this file is loaded before your model class is declared.

Now, in your views you can use something like this:



<%= link_to '« Previous',{:id => @creation.previous_id},:class => "alignleft" if @creation.previous_id %>
<%= link_to 'Next »',{:id => @creation.next_id},:class => "alignright" if @creation.next_id %>