PDA

View Full Version : Use Relevance Algorithm



ojsimon
02-17-2008, 03:30 PM
Hi
This is a script which combines many different video feeds together, at the moment you can see that it
shuffle($all); 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?
<?
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;
}

?>

This is the sort of 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 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.