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
- "great white fox jumped"
- "I like it like that"
- "great white hen eats"
- "Today is friday"
- "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);
?>
精彩评论