this is surely not optimal withit works pretty fine : var HashCounter = Class.create(Hash, { initialize : function( $super ) { $super( {} ); }, store: function(data) { found = this.seek(data); if( !found) { newid = (this.keys().map(parseInt).max() || 0) + 1; this.set(newid, $H( {id : newid, data: data, count: 1} )); } else { var id = found.get('id'); var newcount = 1 + found.get('count'); this.set(id, $H( {id: id, count: newcount, data: data} )); } }, seek: function(data) { var pair = this.detect( function(entry) { return data == entry[1].get('data'); }); return pair ? pair[1] : false; }, toString : function() { var text = "toString\n"; this.each( function(pair) { text += pair.key + ' => ' + pair.value.get('data') + ' (' + pair.value.get('count') + ')\n'; }); return text; } });
// -- USAGE // var Tags = new HashCounter(); // Tags.store('abc'); // Tags.store('abc'); // Tags.store('def'); // -- displayed with Firebug console // console.log(Tags.toString()); it is the first step for building javascript cloud tags parsing an Ajax string like ARTICLE1_TITLE TAG1, TAG2, TAG3 ARTICLE2_TITLE TAG4, TAG5, TAG1 ARTICLE3_TITLE TAG6, TAG7, TAG2, TAG3 (a big list...) the number I attemp to compute is the STRENGTH of the coupling between all possible pair of tags : e.g. given a pair of tags, find the number of articles sharing this pair for example strength(TAG1, TAG7) = 0 strength(TAG2, TAG3) = 2 because of ARTICLE1 and ARTICLE3 strength(TAG1, TAG2) = 1 * etc, for all pairs --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---