开发者

PHP - Compare array elements for similarity and create new array with most similar pairs ranking first

Say I have an array of sentences like this

  1. "great white fox jumped"
  2. "I like it like that"
  3. "great white hen eats"
  4. "Today is friday"
  5. "hahaha, I did a great jump"

I would like to compare each sentence to each other with php simila开发者_JAVA技巧r_text function, and create a new array with these pairs so that I get a new array starting with most similar pairs and the similarity ratio.


Try this:

    <?php

$words = array( "great white fox jumped", "I like it like that", "great white hen eats", "Today is friday", "hahaha, I did a great 

jump");

$count = 0;
for ($i=0;$i<count($words)-1;$i++){

 for ($j = $i+1;$j<count($words);$j++){
 $arr[$count][0] = similar_text($words[$i], $words[$j]);
  $arr[$count][1] = $words[$i];
 $arr[$count][2] = $words[$j];
    $count++;
 }
}

rsort($arr);

var_dump($arr);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜