Tag Cloud Creation Project

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Tag Cloud Creation Project

Post by webdew »

The attached code only go as far as giving you an array of the occurrences of each word in the text.
This is what I need to do:
I've created a input box for the user - please test on localhost
They have to enter a minimum of 100 words.
If they input less than a 100 words they should be advised and enabled to correct it.

What the code should do

1. The first is to clean the text. Remove punctuation from it and convert everything
to lower case. Remove all stop words. words like the, and, you etc.,
2. The code should count the number of occurrences of each word in the text.
3. Once you know how many times the words appear in the text you can construct your
cloud.


Cloud Construction:

The tag cloud must always consist of 20 tags which must be sorted alphabetically.
If the user lets say entered 150 words, you have to select the 20 words which occurred most and display these in the cloud.

Must implement a clearly visible visual hierarchy to distinguish between more and less important terms.

User Options:

When the tag cloud is displayed to the end user, you should provide him with the option to
update the tag cloud by adding additional text to it.

It is very important, therefore, that you store the original text entered by the user in a session variable and combine it with any new text he enters.
Remember that the new cloud should be constructed using both the original and the new text.
Alternatively the user must have the option to create a completely new tag cloud.

index.php

Code: Select all

<?php 
 
if(isset($_POST['submitted']))  {
    if(!strlen(trim($_POST['text']))) {
        print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
        include('form.php');
    }
    else {
        // format text
        $cloud_one_text = $_POST['text'];
        $cloud_one_text = strtolower(stripslashes($cloud_one_text));
        $cloud_one_text = ereg_replace('[^a-zA-Z ]+', '', $cloud_one_text);
        
        // remove stopwords
        $stopwords = array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", 
                           "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", 
                           "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", 
                           "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", 
                           "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", 
                           "enough", "etc", "even", "ever", "every", "everyone", "everything", 
                           "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", 
                           "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", 
                           "go","had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", 
                           "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", 
                           "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", 
                           "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", 
                           "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", 
                           "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", 
                           "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", 
                           "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", 
                           "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", 
                           "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", 
                           "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", 
                           "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", 
                           "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", 
                           "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", 
                           "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", 
                           "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", 
                           "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", 
                           "your", "yours", "yourself", "yourselves", "the"); 
 
        $cloud_one_text = preg_replace('/\b('.implode("|",$stopwords).')\b/i', '', $cloud_one_text);
        // extract words
        $cloud_one_words = explode(' ',$cloud_one_text);
        $cloud_one_word_count = count($cloud_one_words);
 
        if($cloud_one_word_count < 100) {
            print"<span style='color:red;font-weight:bold;'> You must enter atleast 100 words!</span><p/>";
            include('form.php');
        }
        else {
            print "Thanks for entering the required amount of words";
            // remove empty words
            foreach($cloud_one_words as $key => $word) {
                if(!strlen($word)) {
                    unset($cloud_one_words[$key]);
                }
            }
            
            
            $cloud_one_word_occurances = array_count_values($cloud_one_words);
            
            print "<pre>";
                print_r($cloud_one_word_occurances);
            print "</pre>";
            exit();
        }
    }
}
else {
    include('form.php');
}
 
?>
form.php

Code: Select all

<form name='cloud_constructor' method='post' action='index.php'>
 
<table cellspacing='10px'>
<tr><td style="vertical-align:top;color:white;font-family:tahoma;font-size:12;">Please enter some text and click submit to create a cloud: </td><td><textarea name='text' rows='10' cols='30'></textarea>
 
</table>
<input type="hidden" name="submitted" value="TRUE"/>
<input type='submit' value='Submit'/>
</form>
Thank you
j
Attachments
tagcloud.zip
(2.38 KiB) Downloaded 2 times
Last edited by webdew on Wed May 20, 2009 8:01 pm, edited 2 times in total.
webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Re: Tag Cloud Creation Project

Post by webdew »

I'm new to devnetwork and would really like someone to help me, how long do registered members normally wait for replies?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Tag Cloud Creation Project

Post by requinix »

webdew wrote:I'm new to devnetwork and would really like someone to help me, how long do registered members normally wait for replies?
As long as it takes.

Longer posts mean more reading, more understanding, and more thinking. Be patient.
webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Re: Tag Cloud Creation Project

Post by webdew »

Sorry my words came out wrong.... will wait thank you
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Tag Cloud Creation Project

Post by Benjamin »

Yeah, you may want to summarize the question.
webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Re: Tag Cloud Creation Project

Post by webdew »

astions wrote:Yeah, you may want to summarize the question.
Hi astions,

Thank you for the reply, that's the goal i can't make it shorter that's what i need to do for my school project. If you test my 2 files on localhost wamp or xampp you will see that it only gives you an array of the occurrences of each word in the text.

how do i change the code , that when the tags are print it is actually the words that was entered into the textbox/textarea?
Attachments
test.JPG
test.JPG (40.19 KiB) Viewed 163 times
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Tag Cloud Creation Project

Post by Benjamin »

webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Re: Tag Cloud Creation Project

Post by webdew »

It's more like this one http://www.tagcrowd.com

Only 20 words need to display in the cloud even if the user enters 150 words
I need to go onto the next step and finish my code to display the cloud

For now it's just giving you an array of the occurrences of each word in the text. I've spend hours getting to here.

I don't know how to proceed further, i've been on google the whole day going through tutorials and how to's but no help..i got the following code from a site, maybe i can use it but don't know how to build it into my code....

Code: Select all

function printTagCloud($tags) {
        // $tags is the array
        
        arsort($tags);
        
        $max_size = 32; // max font size in pixels
        $min_size = 12; // min font size in pixels
        
        // largest and smallest array values
        $max_qty = max(array_values($tags));
        $min_qty = min(array_values($tags));
        
        // find the range of values
        $spread = $max_qty - $min_qty;
        if ($spread == 0) { // we don't want to divide by zero
                $spread = 1;
        }
        
        // set the font-size increment
        $step = ($max_size - $min_size) / ($spread);
        
        // loop through the tag array
        foreach ($tags as $key => $value) {
                // calculate font-size
                // find the $value in excess of $min_qty
                // multiply by the font-size increment ($size)
                // and add the $min_size set above
                $size = round($min_size + (($value - $min_qty) * $step));
        
                echo '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
        }
}
 
$tags = array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47, 'windows' => 82, 'apple' => 27);
 
printTagCloud($tags); Logged
Last edited by Benjamin on Wed May 20, 2009 8:27 pm, edited 1 time in total.
Reason: Changed code type from text to php.
webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Re: Tag Cloud Creation Project

Post by webdew »

Thank you for all the replies I really appreciate any help i can get need to finish this project for the 22nd.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Tag Cloud Creation Project

Post by Benjamin »

Pass printTagCloud your $cloud_one_words variable and see what happens.
webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Re: Tag Cloud Creation Project

Post by webdew »

astions can you give me a sample code. then i can test it quickly.
webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Re: Tag Cloud Creation Project

Post by webdew »

Astions it seems fine I just need someone to check the code.


Code: Select all

<?php 
 
if(isset($_POST['submitted']))
{
 
if(!strlen(trim($_POST['text']))) 
{
    print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
    include('form.php');
    
}
 
else {
 
//format text
 
 $cloud_one_text=$_POST['text'];
 $cloud_one_text=strtolower(stripslashes($cloud_one_text));
 $cloud_one_text=ereg_replace('[^a-zA-Z ]+', '', $cloud_one_text);
 
//remove stopwords
 
 $stopwords=array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"); 
 $cloud_one_text=preg_replace('/\b('.implode("|",$stopwords).')\b/i', '', $cloud_one_text);
 
//extract words
 
 $cloud_one_words=explode(' ',$cloud_one_text);
 $cloud_one_word_count=count($cloud_one_words);
 
if ($cloud_one_word_count<100)
{
 print"<span style='color:red;font-weight:bold;'> You must enter atleast 100 words!</span><p/>";
 include('form.php');
 
}
 
else {
 
print"Thanks for entering the required amount of words";
$count_words = array_count_values($cloud_one_words);
}
}
function printTagCloud ($count_words) {
        // $tags is the array
        
        arsort($count_words);
        
        $max_size = 32; // max font size in pixels
        $min_size = 12; // min font size in pixels
        
        // largest and smallest array values
        $max_qty = max(array_values($count_words));
        $min_qty = min(array_values($count_words));
        
        // find the range of values
        $spread = $max_qty - $min_qty;
        if ($spread == 0) { // we don't want to divide by zero
                $spread = 1;
        }
        
        // set the font-size increment
        $step = ($max_size - $min_size) / ($spread);
        
        // loop through the tag array
        foreach ($count_words as $key => $value) {
                // calculate font-size
                // find the $value in excess of $min_qty
                // multiply by the font-size increment ($size)
                // and add the $min_size set above
                $size = round($min_size + (($value - $min_qty) * $step));
        
                echo '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
        }
}
 
printTagCloud($count_words);
}
 
 
else
{
 include('form.php');
 
}
 
?>
Last edited by Benjamin on Thu May 21, 2009 8:38 am, edited 1 time in total.
Reason: Changed code type from text to php.
webdew
Forum Newbie
Posts: 9
Joined: Wed May 20, 2009 6:59 pm

Re: Tag Cloud Creation Project

Post by webdew »

Can anyone help me with these errors?

How can I fix this: The argument i'm passing to the tag cloud function isn't an array, but is used as an array within the function.

Do anyone have an idea what to do here. Please help How do i change the argument into an array

Warning: arsort() expects parameter 1 to be array, null given in C:\server\xampp\htdocs\tagcloud\index.php on line 64

Warning: array_values() [function.array-values]: The argument should be an array in C:\server\xampp\htdocs\tagcloud\index.php on line 70

Warning: Wrong parameter count for max() in C:\server\xampp\htdocs\tagcloud\index.php on line 70

Warning: array_values() [function.array-values]: The argument should be an array in C:\server\xampp\htdocs\tagcloud\index.php on line 71

Warning: Wrong parameter count for min() in C:\server\xampp\htdocs\tagcloud\index.php on line 71

Warning: Invalid argument supplied for foreach() in C:\server\xampp\htdocs\tagcloud\index.php on line 83
Attachments
errors.JPG
errors.JPG (62.63 KiB) Viewed 126 times
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Tag Cloud Creation Project

Post by jaoudestudios »

webdew wrote:Can anyone help me with these errors?

Warning: arsort() expects parameter 1 to be array, null given in C:\server\xampp\htdocs\tagcloud\index.php on line 64

Warning: array_values() [function.array-values]: The argument should be an array in C:\server\xampp\htdocs\tagcloud\index.php on line 70

Warning: Wrong parameter count for max() in C:\server\xampp\htdocs\tagcloud\index.php on line 70

Warning: array_values() [function.array-values]: The argument should be an array in C:\server\xampp\htdocs\tagcloud\index.php on line 71

Warning: Wrong parameter count for min() in C:\server\xampp\htdocs\tagcloud\index.php on line 71

Warning: Invalid argument supplied for foreach() in C:\server\xampp\htdocs\tagcloud\index.php on line 83
The errors above tell you exactly what the problem is.

For example...Warning: arsort() expects parameter 1 to be array, null given in C:\server\xampp\htdocs\tagcloud\index.php on line 64...On line 64 you are using arsort function but the 1st parameter you are passing is not an array. If you pass it an array...problem solved :)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Tag Cloud Creation Project

Post by onion2k »

Some times I think we should just let people fail their class rather than doing their work for them.

Thread locked. This is not a free "we'll write your code for you" forum.

Moral of the story: pay more attention in school.
Locked