php实现根据词频生成tag云的方法


Posted in PHP onApril 17, 2015

本文实例讲述了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 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程序设计有所帮助。

PHP 相关文章推荐
php SQL之where语句生成器
Mar 24 PHP
深入array multisort排序原理的详解
Jun 18 PHP
简单的php文件上传(实例)
Oct 27 PHP
yii的CURD操作实例详解
Dec 04 PHP
PHP实现自动对图片进行滚动显示的方法
Mar 12 PHP
php自动给网址加上链接的方法
Jun 02 PHP
PHP开发中常用的十个代码样例
Feb 02 PHP
PHP编写daemon process详解及实例代码
Sep 30 PHP
php 中奖概率算法实现代码
Jan 25 PHP
php中的抽象方法和抽象类
Feb 14 PHP
PHP面向对象之事务脚本模式(详解)
Jun 07 PHP
解决form中action属性后面?传递参数 获取不到的问题
Jul 21 PHP
php计算两个坐标(经度,纬度)之间距离的方法
Apr 17 #PHP
php使用GD创建保持宽高比缩略图的方法
Apr 17 #PHP
PHP中preg_match正则匹配中的/u、/i、/s含义
Apr 17 #PHP
php和editplus正则表达式去除空白行
Apr 17 #PHP
PHP生成唯一订单号的方法汇总
Apr 16 #PHP
微信access_token的获取开发示例
Apr 16 #PHP
微信自定义菜单的处理开发示例
Apr 16 #PHP
You might like
php设计模式 Strategy(策略模式)
2011/06/26 PHP
php 数组动态添加实现代码(最土团购系统的价格排序)
2011/12/30 PHP
php另类上传图片的方法(PHP用Socket上传图片)
2013/10/30 PHP
PHP的preg_match匹配字符串长度问题解决方法
2014/05/03 PHP
ThinkPHP3.1.3版本新特性概述
2014/06/19 PHP
php三种实现多线程类似的方法
2015/10/30 PHP
php获取本机真实IP地址实例代码
2016/03/31 PHP
laravel 框架结合关联查询 when()用法分析
2019/11/22 PHP
经验几则 推荐
2006/09/05 Javascript
JavaScript.Encode手动解码技巧
2010/07/14 Javascript
js设置文本框中焦点位置在最后的示例代码(简单实用)
2014/03/04 Javascript
深入探寻seajs的模块化与加载方式
2015/04/14 Javascript
微信小程序 免费SSL证书https、TLS版本问题的解决办法
2016/12/14 Javascript
JS常见DOM节点操作示例【创建 ,插入,删除,复制,查找】
2018/05/14 Javascript
Vue 中的受控与非受控组件的实现
2018/12/17 Javascript
JS实现json数组排序操作实例分析
2019/10/28 Javascript
python getopt详解及简单实例
2016/12/30 Python
Python列表推导式、字典推导式与集合推导式用法实例分析
2018/02/07 Python
Python检测网络延迟的代码
2018/05/15 Python
python数据结构之线性表的顺序存储结构
2018/09/28 Python
Django跨域请求CSRF的方法示例
2018/11/11 Python
一个可以套路别人的python小程序实例代码
2019/04/09 Python
python控制nao机器人身体动作实例详解
2019/04/29 Python
python中while和for的区别总结
2019/06/28 Python
python psutil模块使用方法解析
2019/08/01 Python
Flask框架学习笔记之使用Flask实现表单开发详解
2019/08/12 Python
Python GUI库PyQt5图形和特效样式QSS介绍
2020/02/25 Python
Python爬虫爬取百度搜索内容代码实例
2020/06/05 Python
Django ModelForm组件原理及用法详解
2020/10/12 Python
CSS3 实现时间轴动画
2020/11/25 HTML / CSS
AmazeUI 面板的实现示例
2020/08/17 HTML / CSS
群胜软件Java笔试题
2012/09/29 面试题
程序运行正确, 但退出时却"core dump"了,怎么回事
2014/02/19 面试题
公司总经理岗位职责
2014/03/15 职场文书
关键在于落实心得体会
2014/09/03 职场文书
入团介绍人意见范文
2015/06/04 职场文书