免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3290 | 回复: 5
打印 上一主题 下一主题

php中有计算文章相关性的模块吗? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-14 11:27 |只看该作者 |倒序浏览
rt, php中有计算文章相关性的模块吗?
读者读取一篇文章后,实验推荐与其文献内容相近或相似的文章,此功能如何实验啊?多谢

论坛徽章:
0
2 [报告]
发表于 2008-03-14 16:47 |只看该作者
没听说过还有这个功能,一般都是用数据库去查。

论坛徽章:
0
3 [报告]
发表于 2008-03-14 17:34 |只看该作者
similar_text — Calculate the similarity between two strings

Description
int similar_text ( string $first, string $second [, float &$percent] )

This calculates the similarity between two strings as described in Oliver [1993]. Note that this implementation does not use a stack as in Oliver's pseudo code, but recursive calls which may or may not speed up the whole process. Note also that the complexity of this algorithm is O(N**3) where N is the length of the longest string.

论坛徽章:
0
4 [报告]
发表于 2008-03-15 00:07 |只看该作者

把这个转给你罢

php怎么获得与文章标题相关的文章?
前一段时间想做个php网站,要求在一篇文章后面附带与该文章相关的文章列表。
在网上搜索了一下,多数人使用的方法大概是:建立一个关键词列表,判断每篇文章包含有那些关键词,最后根据关键词找出与某篇文章最相关

的文章。对于内容比较复杂的网站,确定关键列表词显然比较麻烦。
后来我查阅了一些php函数,感觉similar_text(php4,php5)函数能够十分方便的达到我的要求。这个思路是:从文章列表中取出所有的文章

标题,将所有的文章标题都同当前标题对比,将对比结果生成一个数组,按照相似度的大小由大到标题,利用similar_text将这些文章标题同

原文章标题做对比,按标题的相似程度重新排列标题,就得到了与原文章相似的文章列表。
这个思路用到的关键函数是:
int similar_text ( string $first, string $second [, float $percent] )
它返回的是两个字根串的相同字节数。
按照这个思路,我们建立如下的函数,这个函数的功能是把$arr_title数组按照同$title相似的的顺序重新排列数组。
<?php
//***************************************************************
//作者:ysight@gmail.com
//主页:http://freelyweb.googlepages.com/
//日期:2007-5-18
//转载本程序请保留该以上信息!
//***************************************************************
function getSimilar($title,$arr_title)//$title当前标题,$arrayTitle为需要查找的数组
{
$arr_len=count($arr_title);
for($i=0;$i<=($arr_len-1);$i++)
{
$arr_similar[$i]=similar_text($arr_title[$i],$title);//取得两个字符串相似的字节数
}
arsort($arr_similar);//按照相似的字节数由高到低排序
reset($arr_similar);//将指针移到数组的第一单元
$index=0;
foreach($arr_similar as $old_index=>$similar)
{
$new_title_array[$index]=$arr_title[$old_index];
$index++;
}
return $new_title_array;
}
?>
演示:我们建立一个5个数据的数组,并取出与目标标题最相关的前三个标题。
<?php
$demo_title="php如何获得与文章标题相关的文章";
$demo_arr_title=array("php学习教程","asp学习教程","php如何实现上传文件","php学习教程","asp和php那个更容易学习");
$new_array=getSimilar($demo_title,$demo_arr_title);
//print_r($new_array);
echo "与[$demo_title]最相关的前三个文章是:<br/>";
for($j=0;$j<=2;$j++)
{
echo ($j+1).":".$new_array[$j]."<br/>";
}
?>
以上的代码将获得如下的结果:
与[php如何获得与文章标题相关的文章]最相关的前三个文章是:
1:php如何实现上传文件
2:asp和php那个更容易学习
3:php学习教程

注意:1、关于similar_text速度,有人做过这个一个测试(http://cn.php.net/manual/zh/function.similar-text.php):
The speed issues for similar_text seem to be only an issue for long sections of text (>20000 chars).

I found a huge performance improvement in my application by just testing if the string to be tested was less than 20000 chars

before calling similar_text.

20000+ took 3-5 secs to process, anything else (10000 and below) took a fraction of a second.
Fortunately for me, there was only a handful of instances with >20000 chars which I couldn't get a comparison % for.
如果要直接使用正文作对比速度可能会比较慢。
2、这个函数用于英文的效果可能不太好(本人没有尝试过)。用与英文时可以将英文句子用空格分开成多个单词后再写一个类似于

similar_text的函数。
3、句子中含有比较多“的”、“了”等非关键词字符时,得到的结果可能不太理想。

由于本人是php初学者程序有什么不足之处或者有更好的解决办法请多多指教。

论坛徽章:
0
5 [报告]
发表于 2008-03-16 09:40 |只看该作者
谢谢shuweich

论坛徽章:
0
6 [报告]
发表于 2008-03-16 11:29 |只看该作者
对于中文先分词后计算的效果比较好~~
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP