Say I have 5 text strings. How do I find the 2 that are most "different" from the rest?
<p>I'm using PHP. </p>

<p>Say I have 5 strings:</p>

<pre><code>"The quick brown fox"
"The sly brown fox"
"The sly brown chicken"
"Totally different text here"
"Not like the others"
</code></pre>

<p>and I want to find the 2 that are most "different" from the rest. I'd like to be able to expand it to 1000 text strings and getting the, say, 300 most "different".</p>

<p>Any ideas where to start?</p>

<p><strong>EDIT</strong></p>

<p>How to define "different" is up for discussion!</p>

<p><strong>* EDIT 2 *</strong></p>

<p>We defined "different" as different according to the PHP <code>similar_text</code> function. But there could be other definitions. The real problems is how to compare all the text strings. Jean suggested counting the totals, and this is the hacked version of Phillipe's code that does so:</p>

<pre><code>$strings = array(
    "The quick brown fox",
    "The sly brown fox",
    "The sly brown chicken",
    "Totally different text here",
    "Not like the others"
);

$n = 3;
$sim = array();

for ($i = 0; $i &lt; count($strings); $i++) {

    $total = 0;

    for ($j = 0; $j &lt; count($strings); $j++) {

        if($strings[$i] != $strings[$j]) {

            $sim_val = similar_text($strings[$i], $strings[$j]);
            $total += $sim_val;
            $sim[$strings[$i]][] = array(
                "sim" =&gt; $sim_val,
                "w1" =&gt; $strings[$i],
                "w2" =&gt; $strings[$j]
            );

        }
    }

    $sim[$strings[$i]]['total'] = $total;

}

uasort($sim, function($w1, $w2) {
    return $w1["total"] &gt; $w2["total"];
});

$sim = array_keys($sim);
$sim = array_slice($sim,0,$n);
</code></pre>

<p>That returns</p>

<pre><code>Array
(
    [0] =&gt; Not like the others
    [1] =&gt; Totally different text here
    [2] =&gt; The quick brown fox
)
</code></pre>

<p>which does seem like the correct answer. Thanks to all (apart from those who downvoted the question. Booo to you ;-)</p>

<p><em><strong></em> EDIT 3 <em>*</em></strong></p>

<p>OK, so I've been testing this with my 1000 strings. They each have around 500 unique words, and a <code>strlen</code> of around 14000. So ... to get this running quickly we can immediately forget about <code>similar_text</code> 'cos as pointed out, it's slow. I wrote a quick `compare_words' function:</p>

<pre><code>function same_words($text1,$text2) {

    $words_1 = array_unique(explode(" ",$text1));
    $words_2 = array_flip(array_unique(explode(" ",$text2)));       

    foreach($words_1 AS $word) {
        if($words_2[$word]) {
            $count++;   
        }
    }

    return $count;

}   
</code></pre>

<p>but that's too slow too. </p>
