Sample code below defines a best match 'department id' and 'category id' based on term rank for "miscellaneous" products.
The problem I am having is trying to define a proper term value (weight) for each match and not having the final score entry overwritten in the ranks array.
Static whole numbers for term value can lead to duplicate scores. I'm thinking a float (1,10) or (2,10) would be better.
Any Suggestions?
Sample code below:
<?php
$string = "EVGA GeForce GTX 980 Ti 6GB 384-Bit GDDR5 PCI Express 3.0 x16 HDCP Ready SLI Support";
$terms_array = array(
'10' => array(
'120' => 'Geforce, NVidia, GTX, Geforce GTX, Radeon, Radeon R7, Radeon R9, AMD, ATI, EVGA, Asus, PNY, MSI, Gigabyte, Sapphire Tech, VisionTek, XFX, SLI, CrossFire, CrossFireX',
'121' => 'term, term, term'
),
'12' => array(
'115' => 'term, term, term',
'181' => 'term, term, term'
),
);
Function Get_Top_Rank($string, $terms_array) {
$result = "";
$ranks = array();
$termvalue = 10;
foreach ($terms_array as $key1 => $val1) {
foreach ($val1 as $key2 => $val2) {
$score = 0;
$terms = explode(",", $val2);
foreach ($terms as $word) {
IF (stripos($string, $word) !== false) {
$score += termvalue;
}
}
$ranks[''.$score.''] = array('key1' => $key1, 'key2' => $key2);
}
}
/*
Example Ranks Result
array(
'130' => array('key1' => '10', 'key2' => '120'),
'20' => array('key1' => '10', 'key2' => '121'),
)
*/
IF (!empty($ranks)) {
krsort($ranks);
$result = reset($ranks);
}
return $result; // array('key1' => '10', 'key2' => '120')
}
?>
I decided to go with the following.
<?php $score += round((strlen($word) * 1.987654321), 8); ?>