Not going to lie, this is a massive script, and I’m so close to having it done and working, however it seems the major issue is my use of arrays – I’ve done a ton of troubleshooting and can’t figure it out, however I believe by the end of the script my array is something like this:
array('Array','Array', 'Array', 'Array', 'Array', 'Array');
So after a few hours, I decided to post it in case anyone wants to try and figure it out.
Functions:
max_value = Simply finds the array with the highest integer and returns that arrays name.
isTag = Uses regex to check if the tag is in the description by itself
reallyseriouslysortdealshardcore = GOAL: Sort through a list of predefined tags and find them in a description, count how often they’re used - Then returns a category based on what category the most often used tags are in, and returns a list of the tags.
function max_value($args)
{
$results = array();
$max_value = max($args);
$results[] = $max_value;
//now get the key for the first occurence of max value
$found_key = false;
foreach ($args as $key => $value)
{
if ($value == $max_value && !$found_key)
{
$results[] = $key;
$found_key = true;
}
}
return $results;
}
function isTag($tag, $descrip)
{
$matches=array();
preg_match_all('/\\b'.$tag.'\\b/', $descrip, $matches);
// If the tag 'car' is found 10 times, it only returns car once
$matches = array_unique($matches);
// No tags found = return false, otherwise return all tags found
if (count($matches)==0)
{
return 'false';
} else {
return $matches;
}
}
function reallyseriouslysortdealshardcore($description)
{
$apparelTags = array('apparel','clothe','shoe','jacket','shirt','lingerie');
$autoTags = array('auto','car','audio','speaker','tire','receiver','gps','tomtom','vehicle');
$beautyTags = array('beauty','makeup','nail polish','hair','nails','mascara','eye liner','foundation','lipstick','lotion','skin','skin care','pendant','sterling','gold');
$booksTags = array('books','ebook','novel','paperback','hardback','kindle','nook','ibook');
$electronicsTags = array('electronics','television','tv','hd','hdtv','speaker','home theatre','home theater','receiver','pc','hard drive','camera','projector','watch','watches','intel','amd','asus',
'samsung','corsair','lite-on','gigabyte','logitech','wii','ps3','xbox','laptop','netbook','ipad','xps');
$entertainmentTags = array('entertainment','video games','music','movie','blu-ray','dvd');
$foodTags = array('food','healthy','diet','recipe','chocolate','organic','fryer');
$musicTags = array('music','cd','mp3','itune','headphones','album');
$sportsTags = array('sports','football','basketball','soccer','tennis','softball','hockey','jerseys','baseball');
$toysTags = array('toys','legos','barbie','hot wheels');
$travelTags = array('travel','airfare');
// These are the tags the current description contains
$runningTags = array();
$apparelCount = 0;
$autoCount = 0;
$beautyCount = 0;
$booksCount = 0;
$electronicsCount = 0;
$entertainmentCount = 0;
$foodCount = 0;
$musicCount = 0;
$sportsCount = 0;
$toysCount = 0;
$travelCount = 0;
// Run through apparel tags and add any tags found to running tags
foreach ($apparelTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$apparelCount++;
}
}
}
foreach ($autoTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$autoCount++;
}
}
}
foreach ($beautyTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$beautyCount++;
}
}
}
foreach ($booksTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$booksCount++;
}
}
}
foreach ($electronicsTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
//echo $arraytag;
//
$runningTags[] = $arraytag;
$electronicsCount++;
}
}
}
foreach ($entertainmentTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$entertainmentCount++;
}
}
}
foreach ($foodTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$foodCount++;
}
}
}
foreach ($musicTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$musicCount++;
}
}
}
foreach ($sportsTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$sportsCount++;
}
}
}
foreach ($toysTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$toysCount++;
}
}
}
foreach ($travelTags as $tag)
{
// Returns either false, or an array of tags
$findTags = isTag($tag, $description);
if ($findTags!='false')
{
// For each result in the array -- add it to the runningTags array
// and add a count to that tags category
foreach ($findTags as $arraytag)
{
$runningTags[] = $arraytag;
$travelCount++;
}
}
}
// Trim all whitespace in each tag in the running tags array to make tag display nice
/*foreach($runningTags as $tag)
{
$tag = trim($tag);
//$runningTags[] = $tag;
}*/
// formatted running tags
//$runningTags = implode(', ', $runningTags);
//echo $runningTags;
echo print_r($runningTags).'<Br />';
// Figure which category the tags most fit...
$counts = array(
'apparel' => $apparelCount,
'auto' => $autoCount,
'beauty' => $beautyCount,
'books' => $booksCount,
'electronics' => $electronicsCount,
'entertainment' => $entertainmentCount,
'food' => $foodCount,
'music' => $musicCount,
'sports' => $sportsCount,
'toys' => $toysCount,
'travel' => $travelCount);
$results = max_value($counts);
// proper category
$properCategory = $results[1];
//echo $properCategory.'<br>';
//echo $results[1];
return $runningTags.'|'.$properCategory;
}
This is the code I use to test it:
$description = "Shine some light on the holiday. Known for their ruggedness and dependability, Hella auxiliary lamps must pass a series of endurance tests before branded with the \\"Hella\\" name. This Xenon lamp provides long-range, powerful illumination that resembles daylight for added safety and peace of mind. It includes a magnesium reflector and housing in a size that is ideal for mounting in the front grill or air dam. This kit includes two Micro DE Xenon lamps with D2S Xenon capsules, two Generation 3 ballast units, one wiring harness with relay and mount, and a one year limited warranty.";
$returns = reallyseriouslysortdealshardcore($description);
$returnspieces = explode('|', $returns);
$theTags = $returnspieces[0];
$theCat = $returnspieces[1];
echo 'Tags: '.$theTags.'<br>';
echo 'Category: '.$theCat.'<br>';
Thank you for any assistance.