Ad

How To Get Most Differents String From A List

- 1 answer

I have a list of many strings that have similarities, example :

$str = array('monkey eat a banana',
             'dog eat a banana',
             'cat devour an apple',
             'cat dine a coco'); //etc

I would like to extract X strings from this array that are the most different each other. Example : if i want to extract 3 one, it will be : 'monkey eat a banana' and 'cat dine a coco' and 'cat devour an apple'.

How can i implement this ? I have found the similar_text() function, i think i may use it but how to extract them with any value of X ?

thanks for your advices

ps : i use this for SEO, the goal is to avoid the most possible duplicate content.

Ad

Answer

$Hope to be helpful

$str = array(
    'cat devour an apple',
    'dog eat a banana',
    'monkey eat a banana',
    'cat dine a coco',
); //etc

$overal_scores = [];
foreach ($str as $i => $s) {
    $overal_scores[$i] = 0;
    foreach ($str as $j => $d) {
        if ($i != $j) {
            $overal_scores[$i] += similar_text($s, $d);
        }
    }
}
asort($overal_scores);
$x = 3;
$results_index = array_slice(array_keys($overal_scores), 0, $x);
$result_str = [];
foreach ($results_index as $index) {
    $result_str[] = $str[$index];
}
var_dump($result_str);
Ad
source: stackoverflow.com
Ad