douxiong5438 2013-03-10 17:43
浏览 31
已采纳

回声字符串从最高到最低的相似度

Hard to explain, but I need some help. So I have, say 4 strings $query = 'google awesome';, $result1 = 'google is cool';, $result1 = 'google is awesome';, and $result3 = 'other page';.

Lets say I used PHP's similar_text();, and $result1 is 60% similar, $result2 is 70% similar and $result3 is 5% similar.

How do I echo them in order from highest to lowest. Note that I am using more that 3 strings, I just echo the results using foreach();.

EDIT: HERE is my piece of code.

if(isset($_GET['q'])) {
    $results = file(__DIR__ . '/data/urls.txt');
    $query = $_GET['q'];
    foreach($results as $result) {
        $explode = explode('::', $result);
        $site = $explode[0];
        $title = $explode[1];
        /* if $query = similar to $title, echo ordered by similarity. */
    }
}
  • 写回答

4条回答 默认 最新

  • dourunlao1642 2013-03-10 17:51
    关注

    For exampe you could build an array, with the results and the text, than sort the array and print.

     if(isset($_GET['q'])) {
        $results = explode("
    ",file_get_contents(__DIR__ . '/data/urls.txt')); // i assume all url is in a new line
        $query = $_GET['q'];
        $similarity = array();
        $map = array();        
        foreach($results as $result) {
            list($site,$title) = explode('::', $result);
            $similarity[$site] = similar_text($title,$query); //calculate similari by title for sites 0 is similar bigger is not realy similar
            $map[$site] = $title;            
        }
        asort($similarity,SORT_NUMERIC); //sort the results
    
        $limit = 10;
        foreach($similarity as $site=>$sim){
             print "<a href='{$site}'>{$map[$site]}</a>({$sim} % differnece what you need)<br/>";
             if( --$limit < 1) break;
        }
    }
    

    Links that should be read:

    http://www.php.net/manual/en/function.arsort.php

    http://www.php.net/manual/en/function.each.php

    http://www.php.net/manual/en/function.list.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大