首頁 > 後端開發 > php教程 > php根據詞頻產生tag雲

php根據詞頻產生tag雲

WBOY
發布: 2016-07-25 08:42:56
原創
1344 人瀏覽過

给定一段文本,此代码分析文本的词频分布,生成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[] = ''
  52. . htmlspecialchars( stripslashes( $tag ) ) . '';
  53. }
  54. return join( "n", $cloudTags ) . "n";
  55. }
  56. ?>
  57. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  58. Tag Cloud Demo
  59. Sample Tag Cloud

  • 复制代码
    php,標籤


    相關標籤:
    來源:php.cn
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板