generating tag cloud in Python

Tag Clouds are an amazing way to emphasize on what is needed. Here, is a simple code snippet in Python that will generate tag cloud, on the basis of tags and their count provided.

The algorithm for generating tag cloud is used from http://en.wikipedia.org/wiki/Tag_cloud#Creation_of_a_tag_cloud

def generate_tag_cloud(tags, baseurl=’http://www.example.com/tag/’, fmax = 20):
“””Generates HTML of tag cloud.
Input params:
tags : A dictionary containing the key as the tag name, and
value as the count of times, it appears
ex: {‘tag1’: 2, ‘tag2’: 1, ‘tag3’: 45}
fmax : The maximum font size to display. pixel

http://en.wikipedia.org/wiki/Tag_cloud#Creation_of_a_tag_cloud
“””
size = lambda f_max, t_max, t_min, t_i : t_i > t_min and int(math.ceil((f_max*(t_i – t_min))/(t_max – t_min))) or 10

f_max = fmax
t_min = min(tags.values())
t_max = max(tags.values())
cloud = []

for tag in tags:
cloud.append({‘tag’: tag,
‘count’: tags.get(tag),
‘size’: size(f_max,t_max,t_min,tags.get(tag))})
s = []
for onetag in cloud:
s.append(‘%s‘
%(onetag.get(‘size’), baseurl+onetag.get(‘tag’), onetag.get(‘count’), tag ))
return ‘

‘ + ”.join(s) + ‘

‘

# Assumes the data structure for tag as
# class Tag(object):
# def __init__(self, name, count, permalink):
# self.name = name
# self.count = count
# self.permalink = permalink
# And why would one have such a structure? Well, watch out for http://beamto.us
# source when it is released
#
def gen_tagcloud(tags):
“””Generates the dict for tag cloud based on the input provided
tags = [tag1, tag2, tag3]
“””
conf = ‘log’ # for logarithmic cloud, try ‘linear’ for linear
min_weight = min([x.count for x in tags])
max_weight = max([x.count for x in tags])
tag_cloud = []
if conf.get(‘ALGO’).lower().startswith(‘linear’):
for tag in tags:
w = (tag.count – min_weight)/(max_weight – min_weight)
f = conf.get(‘MIN-FONT’) + (conf.get(‘MAX-FONT’) – conf.get(‘MIN-FONT’) )*w
tag_cloud.append({‘tag’:tag, ‘count’:tag.count+1, ‘font’: f,
‘permalink’: tag.permalink})
if conf.get(‘ALGO’).lower().startswith(‘log’):
for tag in tags:
w = (math.log(tag.count or 1) – math.log(min_weight or 1))/\
(math.log(max_weight or 1) – math.log(min_weight or 1))
f = conf.get(‘MIN-FONT’) + (conf.get(‘MAX-FONT’) – conf.get(‘MIN-FONT’) )*w
tag_cloud.append({‘tag’:tag, ‘count’:tag.count + 1, ‘font’:f,
‘permalink’: tag.permalink})
return tag_cloud

You can tweak and create better versions. Also this is on the Python side, there could be a similar one for JavaScript as well, which generates tag clouds on the fly.

BeamtoUs – Discover Vote Share

Welcome Beamto.us (twitter @beamtous), a completely new and beautiful music sharing portal. We love to call each single element that you share as a beam. This is also my current project, where I am involved in close coordination with caffeine powered code monkeys – Ritesh Nadhani (idea bubble generator), Taras, Freechin and others. Beamto.us.

Beamto.us is a music sharing portal targeting specific to genres like psy trance, chillout trance, dark trance, progressive trance and other sub genres in them. You can easily upload your music, or if you have already done that on youtube, soundcloud or other portals, can link from Beamto.us. I’d like to talk more on what are the interesting things that you can do with Beamto.us. The official About Us page says

“Beamto.us Aggregates. The only website in the World that aggregates all electronic music. We use the best back-end technology to bring an excellent interface to all the users. The main page of the site aggregates and lists only the most popular songs voted by the users. Our algorithm has been simulated and tested to be absolutely fair to rankings.”






Features

  1. Beamto.us allows you to login through your existing Google Account, so you don’t need to go through all the registration hassle and pain.
  2. You can upload your music onto our servers or can hotlink your existing website/link for your music at Beamto.us.
  3. Vote Up/ Down a particular beam. And yeah, the votes and rankings do decay so if your beam is hot and new, you are going to be at the top.
  4. Easily share your beams on Facebook, MySpace, Twitter and many other favorite sites, where your friends can see them
  5. Concerned about copyright issues or something else, just let us know. We deal with abuse reports very seriously.
  6. We have lots of space and our servers are on the cloud, so don’t worry about a downtime 🙂 Whenever someone wants to go through your beam, we make sure that it is always up and serving hot.
  7. Its FREE to use. Yes, you don’t need to pay in order to use or share your beams. Just come over and we will be doing the things for you.

The portal is still in its early phases and we have some very exiting plans to make it fun. Yes, that’s our concern. Make beaming fun, simple and share !!

And yes, we are always listening to our users who tell us about our faults and where we need to improve. And we do work on them. So, whenever you feel something is missing, just let us know.