Redis: Tag Cloud using Redis

25 / Sep / 2012 by Mohd Farid 5 comments

I would like to share a simple and efficient way of creating tagclouds that I discovered very recently. A Tag Cloud is a pictorial representation of some tags/text. The size of the tag/text is drirectly proportional to the weightage of that tag/text. A sample tag cloud could be seen on our blogs site.

How to do it?

[java]A very simple way of making it possible is just by making a Map having the Tag as key and its occurrences as the value. [/java]

The presence of redis makes me think that it is not at all taxing for my poor server.

Redis provides a data-structure called Sorted Set. In Sorted sets, every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated.

We can use the Sorted Set to contain the Tags as value and their corresponding weightage as score. This way we have a Sorted Set of all the Tags. We may choose to select top 50 Tags from the Sorted Set.

Prerequisite: You must have Redis installed and running on your machine. Here is a link to Redis site showing how to do it http://redis.io/download

Here is an example of showing top 50 tags from Blog domain class.

[java]
class Blog{
String content
String title
static hasMany = [categories: String]
}
[/java]

The code for generating the tagCloud is:

[java]
Jedis redis = new Jedis("localhost");
redis.connect();

for (def blog: Blog.list()) {
blog.categories.each {catgry ->
redis.zincrby(‘tag-cloud-sorted-set’, 1, "${catgry}")
}
}

Map tagCloud = [:]
Set<String> categories = redis.zrevrange(‘tag-cloud-sorted-set’, 0, 50)
int size = 30
categories.eachWithIndex {catgry, idx ->
if (idx % 15 == 0) {
size = size – 3
}
tagCloud.put(catgry, size.toString())
}
return tagCloud

[/java]

Once we get the Map of tagCloud we can render it as follows:
[java]
<g:each in="${tagCloud.keySet()}" var="key" status="idx">
<g:link action="search" controller="feed" params="[category: key]"
style="font-size: ${tagCloud.get(key)}px;">${key}</g:link>
</g:each>
[/java]

Hope this helps!!!

Best Regards
Mohd Farid
farid@intelligrape.com
@faridiflex

FOUND THIS USEFUL? SHARE IT

comments (5)

  1. Nollman

    …..yes her problems are in the past and im so very happy she has moved on from them. She is a human being and like everyone else makes mistakes, she has had to grow up in front of the cameras, the paparazzi and her management people have basicly controlled her every move. So i think we can allow her to go a little nuts from time to time, really its none of our business what she does in her privaate life but the paparazzi dont allow her to have one, can you imagine how that must feel? <3 uBrit

    Reply
  2. Chalker

    typo…. Yo nelly I feel u bro I just lost a girl because she moved out of the country and it was a girl I loved spool I feel u

    Reply
  3. Rampton

    Is anyone here preparing for MCAT or AP biology. I have Biology question bank containing few thousand question. Do you think you guys would like to try that by enrolling in my website.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *