Generating Tag Cloud
We always see tag cloud or text cloud from the web, tags could be single words or more, and the counts of each tag appeared is shown with font size or color.
This tutorial does not need IPCV or other extra modules to be installed, it could be done by simply using Scilab. However, the output image could be imported into Scilab with IPCV module.
First, we will show a few simple techniques to plot strings or texts on Scilab figure, followed by using a pre-written library to generate more complicated tag cloud by giving the vector of string with the corresponding frequencies of each string.
Let’s start with a simple example:
tag_text = ['Hello';'World']; tag_count = [6;4]; plot2d([0, 1], [0, 1], 0); xstring(0.1, 0.9, tag_text(1)); gce().font_size = tag_count(1); xstring(0.3, 0.9, tag_text(2)); gce().font_size = tag_count(2); |
This will generate the image as below:
We could further working on the strings properties such as the color by modifying the entities’ properties. For example, changing the color of the word “world” could be done by following line:
gce().font_color = 2; |
gives following result:
In order to build a nicer tag cloud, we created a function to automate the process. Download the function and sample data from this link , unzip it and change the current folder to the location in Scilab console to use the library.
load generate_textcloud.bin; load tagsdata.sod; generate_textcloud(tag_text,tag_count); |
The following image would be generated.
The function “generate_textcloud” could take more input parameters to make the text cloud looks better, the full calling syntax is:
generate_textcloud(tag_text,tag_count,x_spacing,y_spacing,sz_inc,colormap,isbold)
where:
tag_text : A (m x 1) string matrix of the text to generate text cloud
tag_count : A (m x 1) double matrix correspond to the counts of the text appearance
x_spacing : Spacing between words in a row, in the range of 0.01 – 0.2
y_spacing : Spacing between words in col, in the range of 0.01 – 0.2
sz_inc : Size to increase or decrese for all words, in the range of 1 – 10
colormap : Colormap could be in 3 format, [r g b], color string such as “blue”, and standard colormap such as hsvcolormap(64) etc.
isbold : To plot the text in bold, %t or %f
The default parameters are:
generate_textcloud(tag_text,tag_count,0.02,0.1,0,’blue’,%t)
Some examples of using different parameters:
1. Generating text cloud in red color
generate_textcloud(tag_text,tag_count,0.02,0.1,0,[1 0 0],%t); |
Result:
2. Generating text cloud in colormap
generate_textcloud(tag_text,tag_count,0.02,0.1,0,hsvcolormap(6),%t); |
Result:
3. Generating text cloud with more x spacing
generate_textcloud(tag_text,tag_count,0.1,0.1,0,hsvcolormap(6),%t); |
Result:
4. Generating text cloud with bigger font
generate_textcloud(tag_text,tag_count,0.02,0.1,1,hsvcolormap(6),%t); |
Result:
The image could then be saved by clicking on the menu item File –> Export to, and give a filename for the exported image.
If you need to read the image into Scilab, use IPCV module.