一般做内容网站,需要在每一篇文章出现与该文章相关的文章列表。多数人使用的方法大概是:建立一个关键词列表,判断每篇文章包含有那些关键词,最后根据关键词找出与某篇文章最相关的文章。对于内容比较复杂的网站,确定关键列表词显然比较麻烦。
后来我查阅了一些php函数,感觉similar_text(php4,php5)函数能够十分方便的达到我的要求。这个思路是:从文章列表中取出所有的文章标题,将所有的文章标题都同当前标题对比,将对比结果生成一个数组,按照相似度的大小由大到标题,利用similar_text将这些文章标题同原文章标题做对比,按标题的相似程度重新排列标题,就得到了与原文章相似的文章列表。
这个思路用到的关键函数是:

它返回的是两个字根串的相同字节数。
按照这个思路,我们建立如下的函数,这个函数的功能是把$arr_title数组按照同$title相似的的顺序重新排列数组。
程序运行结果:

有些需要注意的地方:
1、关于similar_text速度,有人做过这个一个测试,结果是:
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函数,感觉similar_text(php4,php5)函数能够十分方便的达到我的要求。这个思路是:从文章列表中取出所有的文章标题,将所有的文章标题都同当前标题对比,将对比结果生成一个数组,按照相似度的大小由大到标题,利用similar_text将这些文章标题同原文章标题做对比,按标题的相似程度重新排列标题,就得到了与原文章相似的文章列表。
这个思路用到的关键函数是:

它返回的是两个字根串的相同字节数。
按照这个思路,我们建立如下的函数,这个函数的功能是把$arr_title数组按照同$title相似的的顺序重新排列数组。

程序运行结果:

有些需要注意的地方:
1、关于similar_text速度,有人做过这个一个测试,结果是:
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、句子中含有比较多“的”、“了”等非关键词字符时,得到的结果可能不太理想。