Use Relevancy Algorythym

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ojsimon
    New Member
    • May 2007
    • 59

    Use Relevancy Algorythym

    Hi
    This is a script which combines many different video feeds together, at the moment you can see that it [PHP]shuffle($all);[/PHP] instead of an algorithm to sort the records, i would like to change it so that it is sorted by relevancy to a string which i could specify. Here is the whole code for the page, and bellow that is an algoryhtym which i tried to implement into the code but would not work. Can anyone either help me implement this algorithm or write one that will work with this code? [PHP]<?
    function getall($searcht erms) {
    require("yt.php ");
    require("gv.php ");
    require("dm.php ");
    require("mc.php ");

    $yt = yt($searchterms );
    $gv = gv($searchterms );
    $dm = dm($searchterms );
    $mc = mc($searchterms );

    $all = array_merge($yt ,$gv,$dm,$mc);
    if(count($all)) {
    include_once "latest.php ";
    updatelatest(st r_replace("+","-",$searchterms) );
    }
    $bannedwords = "bannedwords.tx t";
    $fp = fopen($bannedwo rds, "r");
    $wordstmp = fread($fp, filesize($banne dwords));
    fclose($fp);
    $words = explode("\n",$w ordstmp);
    $words = array_map("strt olower",$words) ;

    foreach($all as $index=>$arr) {
    $arrtitle = $arr['title'];
    foreach($words as $index2=>$word) {
    if(strstr(strto lower($arrtitle ),strtolower(st r_replace("\n", "",str_replace( "\r","",$word)) ))) {
    unset($all[$index]);
    break;
    }
    }
    }

    srand((float)mi crotime() * 1000000);
    shuffle($all);
    foreach($all as $j=>$k) {
    if(!trim($k['title'])||!trim($k['link']))
    unset($all[$j]);
    }

    return $all;
    }

    ?>[/PHP]

    This is the sort of algorythym

    [PHP]usort($multifee d,'funnySort');

    function funnySort($a,$b )
    {
    // assign numerical value so we can sort on
    // whether "term" is in title or content or neither

    $term = 'funny';

    $x = 0;
    if (stripos($a->get_title(),$t erm))
    $x = 2;
    elseif (stripos($a->get_content(), $term))
    $x = 1;

    $y = 0;
    if (stripos($b->get_title(),$t erm))
    $y = 2;
    elseif (stripos($b->get_content(), $term))
    $y = 1;

    return $x <= $y
    }[/PHP]


    i also tried to use this code on this site http://www.webmastersherpa.com/tools/search.txt


    Please can someone either help me adapt either of these algorithms or help me write one that will work with my code.
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Is it is the algoritm that does not return a value at all times?
    Then use the ternary operator in your return statement, like
    [php]return ($x <= $y) ? $x : $y;[/php]
    that will either return the lowest of the $x or the $y value.

    Ronald

    Comment

    • ojsimon
      New Member
      • May 2007
      • 59

      #3
      Originally posted by ronverdonk
      Is it is the algoritm that does not return a value at all times?
      Then use the ternary operator in your return statement, like
      [php]return ($x <= $y) ? $x : $y;[/php]
      that will either return the lowest of the $x or the $y value.

      Ronald
      Thanks but i am trying to make it so that all the records are given a score by their relevance to the score and then sorted descendingly, thats what the 2 algorythyms i provided are meant to do, but when i trey to implement these 2 they do not work in my code provided, please could you help me implement them so that the code works.

      thanks

      Comment

      Working...