TinyPortal

Development => Block Codes => Topic started by: JPDeni on August 04, 2006, 08:34:56 PM

Title: Word cloud
Post by: JPDeni on August 04, 2006, 08:34:56 PM
This is what I'd started out to make, but it was a bit complicated and I wanted something simpler first, so I did the poster cloud. Now I think I've got the word cloud done. This was adapted from someone else's code who wrote it for Wordpress.

The script takes the most recent 30 posts from the forum, counts the words, dumps all the punctuation and such and prints out the 50 most frequently occurring words that are 4 or more letters long. All of this is adjustable, of course, so you can tweak it to fit your situation.

There's a list of common words that you probably don't want in your cloud because they're not very interesting. There may be more that you want to add.  There's also some html code things that I've caught -- non-breaking space, apostrophe, exclamation point, quotation mark -- but there will likely be others. The best thing to do is to check it from time to time to see if there's anything you need to add.

One thing to particularly take note of... If you're trying to get rid of html codes, you can't just put the code in. TP will translate it and it'll be pointless to try. You need to do it in two pieces. (What did she say?!?!!?)  :o

Example: To define the non-breaking space, I had to use:

$nbsp = 'nb' . 'sp;';

You'll see others in the code and you may need to come up with some more as you work with it. But it does work.  ;D

global $db_prefix;
$number_of_words = 50;
$min_length = 4;

// This is the list of words to exclude from your cloud
  $exclude_words = array(
    '@http://@',
    '@ about @',
    '@ also @',
    '@ amp @',
     '@ because @',
    '@ been @',
    '@ cant @',
    '@ could @',
    '@ didnt @',
    '@ doesnt @',
    '@ dont @',
    '@ even @',
    '@ from @',
    '@ going @',
    '@ have @',
    '@ havent @',
    '@ here @',
    '@ http_request @',
    '@ into @',
    '@ its @',
    '@ just @',
    '@ like @',
    '@ look @',
    '@ make @',
    '@ many @',
    '@ more @',
    '@ much @',
    '@ must @',
    '@ need @',
    '@ should @',
    '@ shouldnt @',
    '@ some @',
    '@ someone @',
    '@ such @',
    '@ the @',
    '@ take @',
    '@ that @',
    '@ their @',
    '@ then @',
    '@ there @',
     '@ theres @',
   '@ these @',
    '@ they @',
    '@ this @',
    '@ this @',
    '@ want @',
    '@ well @',
    '@ were @',
    '@ what @',
    '@ when @',
    '@ where @',
    '@ which @',
    '@ will @',
    '@ with @',
    '@ without @',
    '@ would @',
    '@ wouldnt @',
    '@ your @',
    '@ youre @'
  );

// Various punctuation that should be filtered from the cloud
  $exclude_symbs = array('@[0-9]@','@\.@','@\,@','@\:@','@"@','@\?@','@\(@','@\)@','@\!@','@\/@','@\&@');
  $apostrophe = '&#'. '39;';
  $exclamation = '&#'. '33;';
  $nbsp = 'nb' . 'sp;';
  $quot = 'qu' . 'ot;';
  $low_count = 0;

// Reset our class globals and other variables
  $cloudy = '';
  $word_list = array();
  $cnt = 0;
  $totalwords = '';

  $query = db_query(
    "SELECT body
     FROM {$db_prefix}messages AS mess
     LEFT JOIN {$db_prefix}boards AS board
     ON mess.ID_BOARD = board.ID_BOARD
     WHERE FIND_IN_SET(-1, board.memberGroups)
     AND board.permission_mode=0
     AND board.ID_BOARD <> 2
      AND board.ID_BOARD <> 12
     AND board.ID_BOARD <> 21
     ORDER BY posterTime DESC
     LIMIT 50", __FILE__, __LINE__);

  while ($row = mysql_fetch_assoc($query))
  {
    $words = $row['body'];
    $words = parse_bbc($words,1);
    $words = strip_tags($words); // Clean HTML tags
    $words = strtolower($words); // Make all words lower case
    $words = str_replace($apostrophe,'',$words); // remove apostrophes
    $words = str_replace($exclamation,'',$words); // remove exclamations
    $words = str_replace($nbsp,'',$words); // remove non-breaking space
    $words = str_replace($quot,'',$words); // remove quote
    $words = preg_replace($exclude_symbs, ' ', $words); // Strip excluded symbols
    $words = preg_replace($exclude_words, ' ', $words); // Strip excluded words
    $words = preg_replace('/\s\s+/', ' ', $words); // Strip extra white space
    $totalwords .= $words;
  }
  $words = '';
  $wordslist = explode(' ', $totalwords); // Turn it back into an array
  $word_count = array_count_values($wordslist); // Count word usage

// Clear out the big array of words.
  arsort($word_count); // Sort the array by usage count

// Here we build our smaller array of words that will be used.
  foreach ($word_count as $key => $val) {
    if (strlen($key) >= $min_length) {
      $word_list[$key] = $val;
      $cnt++;
    }
    if ($cnt >= $number_of_words) {
      $low_count = $val;
      break;
    }
  }


// Sort the array randomly for the cloud
  $random = array_rand($word_list, $number_of_words);
echo '<div style="text-align: center">';
// Build the cloud's HTML
  foreach ($random as $value) {
    $fsize = intval($word_list[$value] / $low_count) *4;
    $fsize = $fsize + 4;
    if ($fsize > 20) { $fsize= 20; }
    echo '<span style="font-size:' . $fsize . 'pt;">' . $value . '</span> ';
  }
echo '</div>';


I'll add a screen shot of what it looks like when there's room in the upload folder. It's pretty much the same as the Poster Cloud, though.
Title: Re: Word cloud
Post by: Assistance on August 07, 2006, 05:51:09 AM
intresting idea
kinda power hungry
and didnt work
wouldnt even let me go to index page
Title: Re: Word cloud
Post by: JPDeni on August 07, 2006, 01:29:17 PM
I suppose it's one of those "Your mileage may vary" things. Works fine on mine and doesn't seem to slow things down at all. I'll see if there's room on the server here to upload my screenshot this time.
Title: Re: Word cloud
Post by: sburke930 on August 19, 2006, 06:32:36 AM
I cant seem to get this to work.  Is there something in the code that I need to change that would be specific to my forum?
Title: Re: Word cloud
Post by: akulion on August 19, 2006, 08:46:34 AM
nice would come in handy for letting people know whats the most discussed thing :D

thanks
Title: Re: Word cloud
Post by: JPDeni on August 19, 2006, 02:23:17 PM
Quote from: sburke930 on August 19, 2006, 06:32:36 AM
I cant seem to get this to work.  Is there something in the code that I need to change that would be specific to my forum?
No. There's nothing specific to a forum. It uses the smf database tables. When you say "I can't seem to get this to work," what do you mean? What does it do?

It might slow things down, especially if your server is being slow. If you want to try to play around with it a bit, change

LIMIT 30

to some other number, like 10 or 20.

akulion, I think it would encourage people to create substantive posts. They would be able to see the results of their posts pretty quickly.
Title: Re: Word cloud
Post by: sburke930 on August 19, 2006, 08:27:22 PM
There is nothing in the block, it is completely empty.

Here is what I have

global $db_prefix;
$number_of_words = 50;
$min_length = 4;

// This is the list of words to exclude from your cloud
  $exclude_words = array(
    '@http://@',
    '@ about @',
    '@ also @',
    '@ because @',
    '@ been @',
    '@ cant @',
    '@ could @',
    '@ didnt @',
    '@ doesnt @',
    '@ dont @',
    '@ even @',
    '@ from @',
    '@ going @',
    '@ have @',
    '@ havent @',
    '@ here @',
    '@ http_request @',
    '@ into @',
    '@ its @',
    '@ just @',
    '@ like @',
    '@ look @',
    '@ make @',
    '@ many @',
    '@ more @',
    '@ much @',
    '@ must @',
    '@ need @',
    '@ should @',
    '@ shouldnt @',
    '@ some @',
    '@ someone @',
    '@ such @',
    '@ the @',
    '@ take @',
    '@ that @',
    '@ their @',
    '@ then @',
    '@ there @',
     '@ theres @',
   '@ these @',
    '@ they @',
    '@ this @',
    '@ this @',
    '@ want @',
    '@ well @',
    '@ were @',
    '@ what @',
    '@ when @',
    '@ where @',
    '@ which @',
    '@ will @',
    '@ with @',
    '@ without @',
    '@ would @',
    '@ wouldnt @',
    '@ your @',
    '@ youre @'
  );

// Various punctuation that should be filtered from the cloud
  $exclude_symbs = array('@[0-9]@','@\.@','@\,@','@\:@','@"@','@\?@','@\(@','@\)@','@\!@','@\/@','@\&@');
  $apostrophe = '&#'. '39;';
  $exclamation = '&#'. '33;';
  $nbsp = 'nb' . 'sp;';
  $quot = 'qu' . 'ot;';

// Reset our class globals and other variables
  $cloudy = '';
  $word_list = array();
  $cnt = 0;
  $high_count = 0;
  $low_count = 0;
  $totalwords = '';

  $query = db_query(
    "SELECT body
     FROM {$db_prefix}messages AS mess
     LEFT JOIN {$db_prefix}boards AS board
     ON mess.ID_BOARD = board.ID_BOARD
     WHERE FIND_IN_SET(-1, board.memberGroups)
     AND board.permission_mode = 2
     ORDER BY posterTime DESC
     LIMIT 30", __FILE__, __LINE__);

  while ($row = mysql_fetch_assoc($query))
  {
    $words = $row['body'];
    $words = parse_bbc($words,1);
    $words = strip_tags($words); // Clean HTML tags
    $words = strtolower($words); // Make all words lower case
    $words = str_replace($apostrophe,'',$words); // remove apostrophes
    $words = str_replace($exclamation,'',$words); // remove exclamations
    $words = str_replace($nbsp,'',$words); // remove non-breaking space
    $words = str_replace($quot,'',$words); // remove quote
    $words = preg_replace($exclude_symbs, ' ', $words); // Strip excluded symbols
    $words = preg_replace($exclude_words, ' ', $words); // Strip excluded words
    $words = preg_replace('/\s\s+/', ' ', $words); // Strip extra white space
    $totalwords .= $words;
  }
  $words = '';
  $wordslist = explode(' ', $totalwords); // Turn it back into an array
  $word_count = array_count_values($wordslist); // Count word usage

// Clear out the big array of words.
  arsort($word_count); // Sort the array by usage count

// Here we build our smaller array of words that will be used.
  foreach ($word_count as $key => $val) {
    if (strlen($key) >= $min_length) {
      if ($high_count == 0)
        $high_count = $val;
      $word_list[$key] = $val;
      $cnt++;
    }
    if ($cnt >= $number_of_words) {
      $low_count = $val;
      break;
    }
  }


// Get the high and low, and calculate the range.
// This is used to weight the size of the words

  $range = ($high_count - $low_count) / 5;


// Sort the array randomly for the cloud
  $random = array_rand($word_list, $number_of_words);
echo '<div style="text-align: center">';
// Build the cloud's HTML
  foreach ($random as $value) {
    $fsize = $word_list[$value] + 7;
    echo '<span style="font-size:' . $fsize . 'pt;">' . $value . '</span> ';
  }
echo '</div>';
Title: Re: Word cloud
Post by: JPDeni on August 19, 2006, 08:47:14 PM
I copied your code into a block and got the same thing as I had before. The only thing I can figure is that the posts on your forum have a lot of short words in them that are being eliminated.

To test it out, comment out -- put // at the beginning of -- the line
    $words = preg_replace($exclude_words, ' ', $words); // Strip excluded words]

You also might want to change the value of the $min_length variable.
Change the number in the line
$min_length = 4;
to 3 or 2 or even 1.

If you still don't get anything (and you have posts in your forum! :)) I'm not sure what to tell you. It may just not work for you for some reason.
Title: Re: Word cloud
Post by: Harro on August 20, 2006, 10:57:37 PM
Another great block code!
Gonna add this one aswell I think! :D

[edit]
Also not working for me.
And getting this in my error log:
2: Invalid argument supplied for foreach()
File: /home/f5186for/public_html/Themes/default/languages/Stats.english.php (main_below sub template - eval?)
Line: 139
Title: Re: Word cloud
Post by: JPDeni on August 20, 2006, 11:25:59 PM
You know, I wish they would just let it not work so we'd get a real error message instead of the error log. I can never figure out where the problem is from the little bit of info they give.

It's probably best just not to use this code.
Title: Re: Word cloud
Post by: Harro on August 20, 2006, 11:41:56 PM
True... That error is not really helpfull...
I'll take another look at it once I had some sleep and when my headache is over :)
Title: Re: Word cloud
Post by: JPDeni on August 20, 2006, 11:50:16 PM
I hope it goes away soon. Headaches are no fun.

Maybe there's a difference in the version of MySQL or php that we have. I've got both "clouds" active and I'm getting no errors at all.
Title: Re: Word cloud
Post by: eitbuzz on September 10, 2006, 12:48:04 PM
Thanks JPDeni , its Great :up:
Title: Re: Word cloud
Post by: gijs on October 25, 2006, 09:06:14 AM
also tried it, but also empty block...

would really love to get this working on my site! Any ideas what is wrong?
Title: Re: Word cloud
Post by: akulion on October 25, 2006, 10:53:55 AM
if ur using 1.0.8 it may not work...are u?
Title: Re: Word cloud
Post by: gijs on October 25, 2006, 11:02:21 AM
SMF 1.1 RC3 and TinyPortal v0.9.5 ...
Title: Re: Word cloud
Post by: akulion on October 25, 2006, 12:15:13 PM
then it should definately work....can u post the code ur using here (exactly from ur block)

That way when JPDeni comes around she can have a look :up:
Title: Re: Word cloud
Post by: gijs on October 25, 2006, 12:44:29 PM
This is resulting in just an empty block. My forum has hundreds of topics on it though..

Here we go:


global $db_prefix;
$number_of_words = 50;
$min_length = 4;

// This is the list of words to exclude from your cloud
  $exclude_words = array(
    '@http://@',
    '@ about @',
    '@ also @',
    '@ because @',
    '@ been @',
    '@ cant @',
    '@ could @',
    '@ didnt @',
    '@ doesnt @',
    '@ dont @',
    '@ even @',
    '@ from @',
    '@ going @',
    '@ have @',
    '@ havent @',
    '@ here @',
    '@ http_request @',
    '@ into @',
    '@ its @',
    '@ just @',
    '@ like @',
    '@ look @',
    '@ make @',
    '@ many @',
    '@ more @',
    '@ much @',
    '@ must @',
    '@ need @',
    '@ should @',
    '@ shouldnt @',
    '@ some @',
    '@ someone @',
    '@ such @',
    '@ the @',
    '@ take @',
    '@ that @',
    '@ their @',
    '@ then @',
    '@ there @',
     '@ theres @',
   '@ these @',
    '@ they @',
    '@ this @',
    '@ this @',
    '@ want @',
    '@ well @',
    '@ were @',
    '@ what @',
    '@ when @',
    '@ where @',
    '@ which @',
    '@ will @',
    '@ with @',
    '@ without @',
    '@ would @',
    '@ wouldnt @',
    '@ your @',
    '@ youre @'
  );

// Various punctuation that should be filtered from the cloud
  $exclude_symbs = array('@[0-9]@','@\.@','@\,@','@\:@','@"@','@\?@','@\(@','@\)@','@\!@','@\/@','@\&@');
  $apostrophe = '&#'. '39;';
  $exclamation = '&#'. '33;';
  $nbsp = 'nb' . 'sp;';
  $quot = 'qu' . 'ot;';

// Reset our class globals and other variables
  $cloudy = '';
  $word_list = array();
  $cnt = 0;
  $high_count = 0;
  $low_count = 0;
  $totalwords = '';

  $query = db_query(
    "SELECT body
     FROM {$db_prefix}messages AS mess
     LEFT JOIN {$db_prefix}boards AS board
     ON mess.ID_BOARD = board.ID_BOARD
     WHERE FIND_IN_SET(-1, board.memberGroups)
     AND board.permission_mode = 2
     ORDER BY posterTime DESC
     LIMIT 30", __FILE__, __LINE__);

  while ($row = mysql_fetch_assoc($query))
  {
    $words = $row['body'];
    $words = parse_bbc($words,1);
    $words = strip_tags($words); // Clean HTML tags
    $words = strtolower($words); // Make all words lower case
    $words = str_replace($apostrophe,'',$words); // remove apostrophes
    $words = str_replace($exclamation,'',$words); // remove exclamations
    $words = str_replace($nbsp,'',$words); // remove non-breaking space
    $words = str_replace($quot,'',$words); // remove quote
    $words = preg_replace($exclude_symbs, ' ', $words); // Strip excluded symbols
    $words = preg_replace($exclude_words, ' ', $words); // Strip excluded words
    $words = preg_replace('/\s\s+/', ' ', $words); // Strip extra white space
    $totalwords .= $words;
  }
  $words = '';
  $wordslist = explode(' ', $totalwords); // Turn it back into an array
  $word_count = array_count_values($wordslist); // Count word usage

// Clear out the big array of words.
  arsort($word_count); // Sort the array by usage count

// Here we build our smaller array of words that will be used.
  foreach ($word_count as $key => $val) {
    if (strlen($key) >= $min_length) {
      if ($high_count == 0)
        $high_count = $val;
      $word_list[$key] = $val;
      $cnt++;
    }
    if ($cnt >= $number_of_words) {
      $low_count = $val;
      break;
    }
  }


// Get the high and low, and calculate the range.
// This is used to weight the size of the words

  $range = ($high_count - $low_count) / 5;


// Sort the array randomly for the cloud
  $random = array_rand($word_list, $number_of_words);
echo '<div style="text-align: center">';
// Build the cloud's HTML
  foreach ($random as $value) {
    $fsize = $word_list[$value] + 7;
    echo '<span style="font-size:' . $fsize . 'pt;">' . $value . '</span> ';
  }
echo '</div>';
Title: Re: Word cloud
Post by: gijs on October 25, 2006, 01:59:54 PM
I think I've narrowed it down to this line of code. When I remove it, I see a tag cloud, but not sure what's the impact of leaving out the code:

WHERE FIND_IN_SET(-1, board.memberGroups) AND board.permission_mode = 2
Title: Re: Word cloud
Post by: JPDeni on October 25, 2006, 02:12:54 PM
I've found the same problem, although I don't know when it started. If you remove the
AND board.permission_mode = 2
you'll get it to work. The first part of the code just makes sure that guests are able to see the board so that posts from admin-only boards aren't included. I'll have to go see what the permission_mode thing does. I don't remember why I put it in there. :)

BTW, when you posted this, it was 1 am where I was. I haven't been ignoring you. I've been asleep.
Title: Re: Word cloud
Post by: JPDeni on October 25, 2006, 02:20:36 PM
Actually, it should be
AND board.permission_mode = 0
which will eliminate announce-only boards.
Title: Re: Word cloud
Post by: gijs on October 25, 2006, 03:20:50 PM
Quote from: JPDeni on October 25, 2006, 02:12:54 PM

BTW, when you posted this, it was 1 am where I was. I haven't been ignoring you. I've been asleep.

thanx so far, I'll give it a try asap. BTW, I just figured that you might not follow all ongoing discussions, I wasn't impatient :-)
Title: Re: Word cloud
Post by: JPDeni on October 25, 2006, 03:32:27 PM
I understand. :)

I was looking at this again and found that most of my words were the same size, or close enough to the same so it didn't make a nice cloud. I changed the printout a bit.

echo '<div style="text-align: center">';
// Build the cloud's HTML
  foreach ($random as $value) {
    $fsize = intval($word_list[$value] / $low_count) *4;
    $fsize = $fsize + 4;
    if ($fsize > 20) { $fsize= 20; }
    echo '<span style="font-size:' . $fsize . 'pt;">' . $value . '</span> ';
  }
echo '</div>';

Gives much more variation, while making the range of sizes still between 8 and 20.
Title: Re: Word cloud
Post by: gijs on October 25, 2006, 04:15:07 PM
I would be great if the words in the cloud are links that link to a search on that words in the forum (with 'search order'=most recent topics first).

Think I can't do that by simply adding s/t to the url that is linked to?
Title: Re: Word cloud
Post by: JPDeni on October 25, 2006, 04:31:12 PM
That's way beyond the scope of this little snippet and would require a complete rewrite.As in starting from scratch. The only thing you could do, I suppose, would be to link to a search for that word. I'd have to investigate to be able to figure out how to do it, but it most likely could be done.
Title: Re: Word cloud
Post by: gijs on October 25, 2006, 04:51:01 PM
Quote from: JPDeni on October 25, 2006, 04:31:12 PM
The only thing you could do, I suppose, would be to link to a search for that word.

I must have explained it wrong, but that's exactly what I was looking for. For example the word 'paris' in the cloud should link to a search on the word 'paris'..

Title: Re: Word cloud
Post by: JPDeni on October 25, 2006, 05:12:30 PM
Oh. I thought you wanted a link directly to the most recent post with that word in it. I guess I'm still not quite awake yet. :)

First, you need to change the first line to
global $db_prefix,$scripturl;
Then change the printout section to
echo '<div style="text-align: center">';
// Build the cloud's HTML
  foreach ($random as $value) {
    $fsize = intval($word_list[$value] / $low_count) *4;
    $fsize = $fsize + 4;
    if ($fsize > 20) { $fsize= 20; }
    echo '<span style="font-size:' . $fsize . 'pt;">' . '<a href="',$scripturl,'?action=search2;search=',$value,';sort=ID_MSG|desc">',$value . '</a></span> ';
  }
echo '</div>';

Title: Re: Word cloud
Post by: gijs on October 25, 2006, 05:27:39 PM
 ;D I just amazed myself ...

Here is what I was looking for:

global $db_prefix;
$number_of_words = 40;
$min_length = 5;

// This is the list of words to exclude from your cloud
 $exclude_words = array(
   '@http://@',
   '@ about @',
   '@ also @',
   '@ because @',
   '@ been @',
   '@ cant @',
   '@ could @',
   '@ didnt @',
   '@ doesnt @',
   '@ dont @',
   '@ even @',
   '@ from @',
   '@ going @',
   '@ have @',
   '@ havent @',
   '@ here @',
   '@ http_request @',
   '@ into @',
   '@ its @',
   '@ just @',
   '@ like @',
   '@ look @',
   '@ make @',
   '@ many @',
   '@ more @',
   '@ much @',
   '@ must @',
   '@ need @',
   '@ should @',
   '@ shouldnt @',
   '@ some @',
   '@ someone @',
   '@ such @',
   '@ the @',
   '@ take @',
   '@ that @',
   '@ their @',
   '@ then @',
   '@ there @',
    '@ theres @',
  '@ these @',
   '@ they @',
   '@ this @',
   '@ this @',
   '@ want @',
   '@ well @',
   '@ were @',
   '@ what @',
   '@ when @',
   '@ where @',
   '@ which @',
   '@ will @',
   '@ with @',
   '@ without @',
   '@ would @',
   '@ wouldnt @',
   '@ your @',
   '@ youre @'
 );

// Various punctuation that should be filtered from the cloud
 $exclude_symbs = array('@[0-9]@','@\.@','@\,@','@\:@','@"@','@\?@','@\(@','@\)@','@\!@','@\/@','@\&@');
 $apostrophe = '&#'. '39;';
 $exclamation = '&#'. '33;';
 $nbsp = 'nb' . 'sp;';
 $quot = 'qu' . 'ot;';

// Reset our class globals and other variables
 $cloudy = '';
 $word_list = array();
 $cnt = 0;
 $high_count = 0;
 $low_count = 0;
 $totalwords = '';

 $query = db_query(
   "SELECT body
    FROM {$db_prefix}messages AS mess
    LEFT JOIN {$db_prefix}boards AS board
    ON mess.ID_BOARD = board.ID_BOARD
    ORDER BY posterTime DESC
    LIMIT 30", __FILE__, __LINE__);

 while ($row = mysql_fetch_assoc($query))
 {
   $words = $row['body'];
   $words = parse_bbc($words,1);
   $words = strip_tags($words); // Clean HTML tags
   $words = strtolower($words); // Make all words lower case
   $words = str_replace($apostrophe,'',$words); // remove apostrophes
   $words = str_replace($exclamation,'',$words); // remove exclamations
   $words = str_replace($nbsp,'',$words); // remove non-breaking space
   $words = str_replace($quot,'',$words); // remove quote
   $words = preg_replace($exclude_symbs, ' ', $words); // Strip excluded symbols
   $words = preg_replace($exclude_words, ' ', $words); // Strip excluded words
   $words = preg_replace('/\s\s+/', ' ', $words); // Strip extra white space
   $totalwords .= $words;
 }
 $words = '';
 $wordslist = explode(' ', $totalwords); // Turn it back into an array
 $word_count = array_count_values($wordslist); // Count word usage

// Clear out the big array of words.
 arsort($word_count); // Sort the array by usage count

// Here we build our smaller array of words that will be used.
 foreach ($word_count as $key => $val) {
   if (strlen($key) >= $min_length) {
     if ($high_count == 0)
       $high_count = $val;
     $word_list[$key] = $val;
     $cnt++;
   }
   if ($cnt >= $number_of_words) {
     $low_count = $val;
     break;
   }
 }


// Get the high and low, and calculate the range.
// This is used to weight the size of the words

 $range = ($high_count - $low_count) / 5;

// start form
echo '<script language="JavaScript" type="text/javascript">
<!--
function searchfromcloud ( selectedtype )
{
 document.scl.search.value = selectedtype ;
 document.scl.submit() ;
}
-->
</script>
<div style="text-align: center"><form name="scl" action="http://www.xxxx.com/portal/index.php?action=search2" method="post">
<input type="hidden" name="advanced" value="0"><input type="hidden" name="sort" value="ID_MSG|desc"><input type="hidden" name="search">

';

// Sort the array randomly for the cloud
 $random = array_rand($word_list, $number_of_words);
// Build the cloud's HTML
 foreach ($random as $value) {
   $fsize = intval($word_list[$value] / $low_count) *4;
   $fsize = $fsize + 3;
   if ($fsize > 17) { $fsize= 17; }
echo '<a href="javascript:searchfromcloud(\''.$value .'\')" style="font-size:' . $fsize . 'pt;">' . $value . '</a> ';
 }
echo '</form></div>';


Check the last part, with the javascripty in there. Linking to the most recent post of the search directly, would be even better. But now you see what I mean  :coolsmiley:
Title: Re: Word cloud
Post by: JPDeni on October 25, 2006, 05:34:28 PM
I guess you can do the javascript thing, but the code I posted above will give a link to a search.

QuoteLinking to the most recent post of the search directly, would be even better.
And would require a complete rewrite of the block. There's no way to connect a word to a message number with the way it's written now and doing a search for each word as it's printed out would be very server-intensive. People complain that it takes too long to run this as it is. :)
Title: Re: Word cloud
Post by: gijs on October 25, 2006, 05:51:37 PM
and you amazed me  :D way simpler that way!

Only thing I added, was ;maxage=100 to the searchstring, to make it faster (and you don't click on a link in the cloud to find an ancient post)
Title: Re: Word cloud
Post by: JPDeni on October 25, 2006, 06:18:22 PM
Good idea. I wouldn't be able to test that out my test site at this point. I imported my old database back in June, so all of my posts are more than 100 days old. :)

You might want to add something to the original search to indicate only pulling up posts from the most recent 100 days. It usually would not be an issue because most forums would have more than 40 posts in 100 days, but you never know when things get slow.
Title: Re: Word cloud
Post by: whatever on February 20, 2007, 01:06:32 PM
You've got this code just right. This is a lovely mod.
Thanks!
Title: Re: Word cloud
Post by: JPDeni on February 20, 2007, 01:13:15 PM
I'm glad you like it. :)
Title: Re: Word cloud
Post by: evilicy on October 12, 2007, 10:07:16 PM
I just put this on my site works great no problems at all! I have a poetry site so I'm sure everyone on the site will appreciate it!

Thanks for this wonderful block code!
Title: Re: Word cloud
Post by: Santa on May 26, 2008, 11:49:41 PM
Yeah this is a very cool mod and worked like a charm.  Thank You all very very much for all your hard work.  :up:
Title: Re: Word cloud
Post by: JPDeni on July 31, 2009, 05:33:32 AM
Since TP doesn't work with SMF 2.0 and since the code is supposed to be for TP, I don't see much point in making it compatible with SMF 2.0 at this point.
Title: Re: Word cloud
Post by: JPDeni on July 31, 2009, 10:47:54 PM
I am seriously not happy with people using my code with other portals. I wrote it to be used with TP and I personally take offense at people using it in any other way.
Title: Re: Word cloud
Post by: ZarPrime on July 31, 2009, 11:40:52 PM
Yeah, that is definitely not cool.

ZP