当前位置:Gxlcms > PHP教程 > php根据词频生成tag云

php根据词频生成tag云

时间:2021-07-01 10:21:17 帮助过:2人阅读

给定一段文本,此代码分析文本的词频分布,生成tag云

  1. /**
  2. * Tag cloud demo based on word frequency
  3. * @author: unknown
  4. * @since: 2007-02-27
  5. */
  6. // Store frequency of words in an array
  7. $freqData = array();
  8. // Random words
  9. $lorem = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  10. Phasellus vestibulum ullamcorper tortor. Aenean quis lacus quis neque
  11. adipiscing ultricies. Pellentesque tincidunt ligula vitae nibh ornare
  12. pharetra. Proin dignissim tortor. Donec et ipsum nec tellus gravida
  13. tempor. Aliquam ullamcorper purus vel felis. Praesent faucibus.
  14. Curabitur porta. Nulla in lorem quis mi lacinia fringilla. Integer
  15. adipiscing mi quis felis. Pellentesque habitant morbi tristique senectus
  16. et netus et malesuada fames ac turpis egestas. Quisque sagittis ante in
  17. arcu. Sed libero enim, venenatis sit amet, vestibulum at, porttitor id,
  18. neque. Vestibulum ornare semper erat. Sed tincidunt nibh et massa. Cras
  19. sed diam. Quisque blandit enim.
  20. Sed nonummy. Aenean mollis turpis quis enim. Nam massa nulla, varius
  21. molestie, aliquet et, feugiat eget, nisi. Sed mollis, leo ut pretium
  22. placerat, nibh turpis egestas ipsum, sed aliquam neque enim in risus.
  23. Nullam nisl. Sed tincidunt leo quis tellus. Mauris non lorem. Aenean
  24. tristique justo at arcu. Fusce et lorem. Nam sodales. Mauris condimentum
  25. diam. Nam commodo. Cum sociis natoque penatibus et magnis dis parturient
  26. montes, nascetur ridiculus mus. Cras ac risus. Proin et dolor laoreet mi
  27. gravida sodales. Duis bibendum, ipsum posuere egestas posuere, dui lacus
  28. feugiat turpis, id tincidunt urna est sit amet est. Cras eu sem.
  29. ";
  30. // Get individual words and build a frequency table
  31. foreach( str_word_count( $lorem, 1 ) as $word )
  32. {
  33. // For each word found in the frequency table, increment its value by one
  34. array_key_exists( $word, $freqData ) ? $freqData[ $word ]++ : $freqData[ $word ] = 0;
  35. }
  36. // ==============================================================
  37. // = Function to actually generate the cloud from provided data =
  38. // ==============================================================
  39. function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )
  40. {
  41. $minimumCount = min( array_values( $data ) );
  42. $maximumCount = max( array_values( $data ) );
  43. $spread = $maximumCount - $minimumCount;
  44. $cloudHTML = '';
  45. $cloudTags = array();
  46. $spread == 0 && $spread = 1;
  47. foreach( $data as $tag => $count )
  48. {
  49. $size = $minFontSize + ( $count - $minimumCount )
  50. * ( $maxFontSize - $minFontSize ) / $spread;
  51. $cloudTags[] = ' . '" class="tag_cloud" href="http://www.google.com/search?q=' . $tag
  52. . '" title="\'' . $tag . '\' returned a count of ' . $count . '">'
  53. . htmlspecialchars( stripslashes( $tag ) ) . '';
  54. }
  55. return join( "\n", $cloudTags ) . "\n";
  56. }
  57. ?>
  58. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  59. Tag Cloud Demo
  60. Sample Tag Cloud


php, tag

人气教程排行