WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

Creating Word Cloud Widget Using Google Visualization API

on 2011/07/26 11:33 AM stored in: Uncategorized and tagged:

Introduction To Word Cloud

Word Cloud is a technique where we analyze a string to find words inside the string and the number of appearances of the word. Word Cloud displays words in different styles according to the word density in the string. Common format is that words with most number of occurrences are displayed with a higher font size and words with least number of occurrences is displayed with small font size.

Why We Need a Word Cloud

Word Cloud can be used as a interactive widget which enables users to easily identify what the web site is about. Making the word cloud dynamic and interactive means that more users will be interested in using its functionality. Hence you will get more traffic and publicity to your web site.

Google Visualization API Word Cloud

Google Visualization API provides a built in word cloud widget using js. We can pass text content to javascript variables and generate the word cloud. Default code provided by google code uses data table to create the cloud. So we need to create a data table with rows and columns in order to use this script. Following Code shows you how to use google word cloud widget.


  
    
    
    
  
  
    
Code Explanation
  • First you have to include the 2 js files and css file provided by google visualization api.
  • Next define the container to load the word cloud ( Ex: Div with the id wcdiv).
  • Then we load the visualization api and call a function called draw to create the word cloud.
  • Next we need to create the data table to search using google.visualization.DataTable().
  • Since we have created the data table now we need to add columns and rows.
  • This example uses only 1 column. You can add as many columns as you like using addColumn() function.
  • Then we need to specify the number of rows we going to use using addRows() function.
  • Next we have to set the table cell values like an array indexes using setCell() function.
  • Finally we specify the output container and create the WordCloud object to draw the word cloud.

 

This is the most basic way of using Google Visualization API Word Cloud. You can use this script in various kind of ways using server side code and database values to create more meaningful word clouds.Next section i’ll show you how to create a plugin using google widget to interact with the server side.

Extending Word Cloud Widget Using Jquery and PHP

In this section I’m going to explain how to make a extend word cloud to interact with server side data using the Google Word Cloud Widget. I am going to use jquery for making ajax requests.


$con = mysql_connect("localhost","admin","admin");
mysql_select_db("wordpress");

$result = mysql_query("select post_title from wp_posts where post_status='publish' limit 50");
$x = 0;
$str = array();
while ($row = mysql_fetch_array($result)) {
        array_push($str, $row['post_title']);
        $x++;
}
echo json_encode(array("result"=>$str,"rows"=>mysql_num_rows($result)));
Code Explanation
  • You can use the same code with few modifications to interact with server side. I am going to explain the modified code only.
  • I have added some code to make ajax request inside the draw function..
  • You can specify the a php page to get data from server and return the data in a json format.
  • Then assign the returned content to js variable using jquery parseJSON function.
  • Then create the data table as above and assign the number of rows returned from server to addRow function.
  • Next you can traverse through each item result returned from the server and add to data table using setCell function.
  • Finally assign the output to a container as above.
  • In the server side php code connect to database and get what ever data you want to display in the word cloud and add to an array.
  • Then encode the words array and number of items using json_encode as shown above and echo it.

 

Practical Examples of a Word Cloud

There are many situations where you can create a word cloud. In the following section I'll show you some practical examples of a word cloud.

  • Creating a Ajax Based Live Site Search

    You can create a word cloud and when the user moves the mouse over each word you can show the links of the pages which contains that word in a dynamic div.

  • Searching Words on Google

    There can be some words in your web site content that some users doesn't know the meaning. In such situations you can assign links to words in the word cloud to search in google.

  • Finding Keyword Density in Web Pages

    Keyword density is how much percentage keyword value has compared to total number of words in a page. We can create a word cloud which displays density of each word on mouse over.

6 Responses to “Creating Word Cloud Widget Using Google Visualization API”

  1. kliknij Says:

    hey there and thank you for your info – I’ve definitely picked up something new from right here. I did however expertise some technical issues using this site, as I experienced to reload the site many times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I’m complaining, but sluggish loading instances times will very frequently affect your placement in google and can damage your high-quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my e-mail and can look out for much more of your respective intriguing content. Ensure that you update this again very soon..

  2. tutaj Says:

    Attractive section of content. I just stumbled upon your website and in accession capital to assert that I get in fact enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently quickly.

  3. zobacz Says:

    Just wish to say your article is as surprising. The clarity in your post is simply excellent and i can assume you are an expert on this subject. Well with your permission allow me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please keep up the rewarding work.

  4. admin Says:

    Thanks for your interest in innovativephp

  5. Darin Esser Says:

    Hi, thanks for this. I found your website from bing. If anyone requires any information which is website related, just contact me via my link.

  6. uczelnie Says:

    We’re a group of volunteers and starting a new scheme in our community. Your site provided us with valuable information to work on. You have done an impressive job and our entire community will be thankful to you.

Leave a Reply

Follow Us On Facebook