Given one piece of text, this code analyzes text's word frequency distribution, generation tag云
- /**
- * Tag cloud demo based on word frequency
- * @author: unknown
- * @since: 2007-02-27
- */
-
- // Store frequency of words in an array
- $freqData = array();
-
- // Random words
- $lorem = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec et ipsum gravida
-
-
- Sed nonummy. For no mass
- molestie, but feugiat
- Sed soft
- pleasant, but no one in risus.
- players But what is the land of the land? Mauris no lorem. Aeneas was sad just at the bow. Check and check. For the members Mauritian sauce
- diam For convenience. Together with their companions, the mountains will give birth to the feathered and great push, and a ridiculous mouse will be born. Tomorrow and laughter. Proin et dolor laoreet mi
- gravida members. You have to drink, you have to put it, you have to put it, you have to run away from the lake, it's a tin can, it's important. Cras eu sem.
- ";
-
- // Get individual words and build a frequency table
- foreach( str_word_count( $lorem, 1 ) as $word )
- {
- // For each word found in the frequency table, increment its value by one
- array_key_exists( $word, $freqData ) ? $freqData[ $word ]++ : $freqData[ $word ] = 0;
- }
- // ============= ================================================
- / / = Function to actually generate the cloud from provided data =
- // =================================== ===========================
- function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )
- {
- $minimumCount = min( array_values( $data ) );
- $maximumCount = max( array_values( $data ) );
- $spread = $maximumCount - $minimumCount;
- $cloudHTML = '';
- $cloudTags = array() ;
-
- $spread == 0 && $spread = 1;
-
- foreach( $data as $tag => $count )
- {
- $size = $minFontSize + ( $count - $minimumCount )
- * ( $maxFontSize - $minFontSize ) / $spread;
- $cloudTags[] = ''
- . htmlspecialchars( stripslashes( $tag ) ) . '';
- }
-
- return join( "n", $cloudTags ) . "n";
- }
- ?>
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
- < ;html xmlns="http://www.w3.org/1999/xhtml">
- Tag Cloud Demo
-
- /*< color: #019c05; }
- .tag_cloud: #69da03; }
- /*/
-
-
-
Sample Tag Cloud
-
-
-
|