[UPHPU] keyword substring scoring
Brandon Stout
hplsbyufan at imapmail.org
Wed Jul 12 16:52:57 MDT 2006
I'd like to search text strings for instances of a keyword, give the
string a score based on them, and group by the scores. The obvious
problem is the while loop within a while loop. This splits up the
scoring when an item has more than one keyword (I think...), but I can't
think of any other way. In addition, I'll want to group by scores
(descending). Right now, I'm doing this:
[code]
// iterate over each item row (haystack)
while ($item = mysql_fetch_array($items, MYSQL_ASSOC)) {
global $keywdqry;
$keywdqry = mysql_query($keywdsql);
// iterate over each keyword (needle) within each row (haystack)
while ($keywd = mysql_fetch_array($keywdqry, MYSQL_ASSOC)) {
// search the link field for any of the keywords, multiply by pointvalue
$linksrch = (
substr_count($item['link'], $keywd['keyword'])
* $keywd['pointvalue']
);
// search the title field for any of the keywords, multiply by pointvalue
$titlesrch = (
substr_count($item['title'], $keywd['keyword'])
* $keywd['pointvalue']
);
// search the desc field for any of the keywords, multiply by pointvalue
$descsrch = (
substr_count($item['desc'], $keywd['keyword'])
* $keywd['pointvalue']
);
// total the score
$srchtotal = $linksrch + $titlesrch + $descsrch;
// print to screen any items that at least have a score (herodic)
if ($srchtotal > 0) {
echo <<<str
<li><a href="{$item['link']}" rel="external"
title="{$item['title']}">{$item['title']}</a>
</li>\r\n
str;
}
}
}
[/code]
Any ideas?
Thank you,
Brandon Stout
http://mscis.org
More information about the UPHPU
mailing list