By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Use Relevancy Algorythym

P: 59
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($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_rep lace("\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;
}

?>[/PHP]

This is the sort of algorythym

[PHP]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
}[/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.
Feb 17 '08 #1
Share this Question
Share on Google+
2 Replies


ronverdonk
Expert 2.5K+
P: 4,258
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
Feb 17 '08 #2

P: 59
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
Feb 17 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.