注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

php根据词频生成tag云

PHP 水墨上仙 1483次浏览 已收录 手机上查看

给定一段文本,此代码分析文本的词频分布,生成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 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[] = '<a style="font-size: ' . floor( $size ) . 'px' 
		. '" class="tag_cloud" href="http://www.google.com/search?q=' . $tag 
		. '" title="\'' . $tag  . '\' returned a count of ' . $count . '">' 
		. htmlspecialchars( stripslashes( $tag ) ) . '</a>';
	}
 
	return join( "\n", $cloudTags ) . "\n";
}	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<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>
	<div id="wrapper">
		<?php echo getCloud( $freqData ) ?>
	</div>
</body>
</html>


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明php根据词频生成tag云
喜欢 (0)
[开心洋葱]
分享 (0)
关于作者:
水墨上仙
……
加载中……