Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a tag cloud in Java, with OpenCloud? [closed]

I was looking for a library to create tag clouds in a Java application, and I found OpenCloud.

I don't want to have to use a web server, which OpenCloud will require to get the output in, won't it? Is there a way to get OpenCloud to work in a Java/Swing panel? I want something for a stand alone application. If this isn't possible, where else can I look for such an API?

like image 331
coder Avatar asked Jul 14 '12 06:07

coder


People also ask

How is a tag cloud created?

They usually generate the tag clouds automatically by analyzing the keywords or frequent words on a website or text. However, the tools may differ depending on whether a website is static or dynamic. The use of tag clouds has advantages as well as some disadvantages, which may vary depending on the user goals.

How does a tag cloud works?

Word clouds (also known as text clouds or tag clouds) work in a simple way: the more a specific word appears in a source of textual data (such as a speech, blog post, or database), the bigger and bolder it appears in the word cloud. A word cloud is a collection, or cluster, of words depicted in different sizes.

What is tag cloud visualization?

A tag cloud visualization is a chart that helps visualize text data and is mainly used to conduct the word-frequency analysis across tags, keywords, and so on. Tags are usually single words, and the font size of each tag indicates its measure.


1 Answers

Actually OpenCloud does not require a Web server. Simply use Swing rendering instead of HTML/JSP. Here is a small snippet illustrating a very basic Swing tag cloud using OpenCloud library. It can be improved, but it gives you the gist:

import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.mcavallo.opencloud.Cloud;
import org.mcavallo.opencloud.Tag;

public class TestOpenCloud {

    private static final String[] WORDS = { "art", "australia", "baby", "beach", "birthday", "blue", "bw", "california", "canada", "canon",
            "cat", "chicago", "china", "christmas", "city", "dog", "england", "europe", "family", "festival", "flower", "flowers", "food",
            "france", "friends", "fun", "germany", "holiday", "india", "italy", "japan", "london", "me", "mexico", "music", "nature",
            "new", "newyork", "night", "nikon", "nyc", "paris", "park", "party", "people", "portrait", "sanfrancisco", "sky", "snow",
            "spain", "summer", "sunset", "taiwan", "tokyo", "travel", "trip", "uk", "usa", "vacation", "water", "wedding" };

    protected void initUI() {
        JFrame frame = new JFrame(TestOpenCloud.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        Cloud cloud = new Cloud();
        Random random = new Random();
        for (String s : WORDS) {
            for (int i = random.nextInt(50); i > 0; i--) {
                cloud.addTag(s);
            }
        }
        for (Tag tag : cloud.tags()) {
            final JLabel label = new JLabel(tag.getName());
            label.setOpaque(false);
            label.setFont(label.getFont().deriveFont((float) tag.getWeight() * 10));
            panel.add(label);
        }
        frame.add(panel);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TestOpenCloud().initUI();
            }
        });
    }

}

This code is based on the Example 1 of the OpenCloud library

Here is an output of what I got:

Swing tag cloud demo image

like image 181
Guillaume Polet Avatar answered Sep 23 '22 15:09

Guillaume Polet