php实现根据词频生成tag云的方法
网络编程 2021-07-05 09:09www.168986.cn编程入门
这篇文章主要介绍了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 us 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 modo. 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://.google./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://.w3./TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://.w3./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程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程