首頁  >  文章  >  後端開發  >  php分析與操作字串以及標籤雲的生成

php分析與操作字串以及標籤雲的生成

墨辰丷
墨辰丷原創
2018-06-11 15:30:341221瀏覽

這篇文章主要介紹了php實現根據詞頻生成tag雲的方法,涉及php分析與操作字符串以及標籤雲的生成技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php實作根據詞頻產生tag雲的方法。具體如下:

這裡給定一段文本,分析文本的詞頻分佈,產生tag雲


<?php
/**
 * 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.
Phasellus vestibulum ullamcorper tortor. Aenean quis lacus quis neque
adipiscing ultricies. Pellentesque tincidunt ligula vitae nibh ornare
pharetra. Proin dignissim tortor. Donec et ipsum nec tellus gravida
tempor. Aliquam ullamcorper purus vel felis. Praesent faucibus.
Curabitur porta. Nulla in lorem quis mi lacinia fringilla. Integer
adipiscing mi quis felis. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Quisque sagittis ante in
arcu. Sed libero enim, venenatis sit amet, vestibulum at, porttitor id,
neque. Vestibulum ornare semper erat. Sed tincidunt nibh et massa. Cras
sed diam. Quisque blandit enim.
Sed nonummy. Aenean mollis turpis quis enim. Nam massa nulla, varius
molestie, aliquet et, feugiat eget, nisi. Sed mollis, leo ut pretium
placerat, nibh turpis egestas ipsum, sed aliquam neque enim in risus.
Nullam nisl. Sed tincidunt leo quis tellus. Mauris non lorem. Aenean
tristique justo at arcu. Fusce et lorem. Nam sodales. Mauris condimentum
diam. Nam commodo. Cum sociis natoque penatibus et magnis dis parturient
montes, nascetur ridiculus mus. Cras ac risus. Proin et dolor laoreet mi
gravida sodales. Duis bibendum, ipsum posuere egestas posuere, dui lacus
feugiat turpis, id tincidunt urna est sit amet est. Cras eu sem.
";
// Get inpidual 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 = &#39;&#39;;
 $cloudTags = array();
 $spread == 0 && $spread = 1;
 foreach( $data as $tag => $count )
 {
 $size = $minFontSize + ( $count - $minimumCount )
 * ( $maxFontSize - $minFontSize ) / $spread;
 $cloudTags[] = &#39;<a style="font-size: &#39; . floor( $size ) . &#39;px&#39;
 . &#39;" class="tag_cloud" href="http://www.google.com/search?q=&#39;
 . $tag
 . &#39;" title="\&#39;&#39; . $tag . &#39;\&#39; returned a count of &#39; . $count
 . &#39;">&#39;
 . htmlspecialchars( stripslashes( $tag ) ) . &#39;</a>&#39;;
 }
 return join( "\n", $cloudTags ) . "\n";
} 
?>
<!DOCTYPE html>
<html>
<head>
 <title>Tag Cloud Demo</title>
 <style type="text/css" media="screen">
 /*<![CDATA[*/
 .tag_cloud { padding: 3px; text-decoration: none; }
 .tag_cloud:link { color: #81d601; }
 .tag_cloud:visited { color: #019c05; }
 .tag_cloud:hover { color: #ffffff; background: #69da03; }
 .tag_cloud:active { color: #ffffff; background: #ACFC65; }
 /*]]>*/
 </style>
</head>
<body>
 <h1>Sample Tag Cloud</h1>
 <p id="wrapper">
 <?php echo getCloud( $freqData ) ?>
 </p>
</body>
</html>

總結:以上就是本篇的全部內容,希望對大家的學習有所幫助。

相關推薦

php結合html5的websocket通訊的使用方法

##php圖片上傳及javascript外掛程式的使用

php針對上傳圖片檔案的功能詳解

############################

以上是php分析與操作字串以及標籤雲的生成的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn