Jump to content

Using this algorithm somethings gone wrong?


ojsimon

Recommended Posts

Hi

On my video site this is the code that merges the results and prepares them for displaying. <?

function getall($searchterms) {

<?
function getall($searchterms) {
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(str_replace("+","-",$searchterms));	
}
$bannedwords = "bannedwords.txt";
$fp = fopen($bannedwords, "r");
$wordstmp = fread($fp, filesize($bannedwords));
fclose($fp);
$words = explode("\n",$wordstmp);
$words = array_map("strtolower",$words);

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

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

return $all;	
}

?>

 

 

At the moment this shuffles the results so they are in random order, i want to make them in order of relevance so i found this algorythym

usort($multifeed,'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(),$term))
        $x = 2;
    elseif (stripos($a->get_content(),$term))
        $x = 1;

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

    return $x <= $y
}

 

I have tried just putting this in the place of shuffle and changing the variables, but it doesnt work, how can i make this work? any help is appreciated.

 

Thanks

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.