如何用php检测文本的相似度


How to detect similarity ratio of texts with php?

假设我有两句话:

"敏捷的棕色狐狸跳过懒惰的狗"

"敏捷的棕色兔子跳过懒惰的猫"

有没有一种算法可以检测这两个句子的相似度?例如:

function similarity_ratio($text1, $text2) {
code code code
return $similarity_ratio;
}
$text1 = "The quick brown fox jumps over the lazy dog";
$text2 = "The quick brown cat jumps over the lazy chicken";
echo similarity_ratio($text1, $text2);
// output 88%
function similarity_ratio($text1, $text2) {
     similar_text($text1, $text2, $similarity_ratio);
     return $similarity_ratio;
}
$text1 = "The quick brown fox jumps over the lazy dog";
$text2 = "The quick brown fox jumps over the lazy cat";
echo similarity_ratio($text1, $text2);
Output: 93.023255813953

看看这个PHP函数:http://php.net/manual/en/function.similar-text.php

你在找这个吗?http://php.net/manual/en/function.similar-text.php.如果速度很重要,请考虑:http://php.net/manual/en/function.levenshtein.php