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 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗