<?php
include ("../includes/db_functions.inc.php");

function wordsArray($string){
    $stopWords = array( 'the','of','and','a','to','in','is','you','that','it','he','was','for','on','are','as','with','his','they','I','at','be','this','have','from','or','one','had','by','word','but','not','what','all','were','we','when','your','can','said','there','use','an','each','which','she','do','how','their','if','will','up','other','about','out','many','then','them','these','so','some','her','would','make','like','him','into','has','two','more','write','go','see','number','no','way','could','people','my','than','first','water','been','call','who','oil','its','now','find','long','down','day','did','get','come','made','may','part','click');
	$punct = array('.',';','?','!',':');
	$string = str_replace($punct, ' ', $string);
	$string = preg_replace('/ss+/i', '', $string);
    $string = trim($string); // trim the string
    $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); // only take alphanumerical characters, but keep the spaces and dashes too…
    $string = strtolower($string); // make it lowercase
    $matchWords = explode(" ", $string);
	foreach ( $matchWords as $key=>$item ) {
        if ( $item == '' || in_array(strtolower($item), $stopWords) || strlen($item) <= 3 ) {
            unset($matchWords[$key]);
        }
    }
    $wordCountArr = array();
    if ( is_array($matchWords) ) {
        foreach ( $matchWords as $key => $val ) {
            $val = strtolower($val);
            if ( isset($wordCountArr[$val]) ) {
                $wordCountArr[$val]++;
            } else {
                $wordCountArr[$val] = 1;
            }
        }
    }
    arsort($wordCountArr);
   return $wordCountArr;		
}

function getCategory($words) {
	$allwords = array_keys($words);
	$keywords = array('puzzle,level,piece,block,ball,item,object,card,picture,tile,word,number,difference,skill,letter,color,pair,group,brick,contain,animal',
				   'action,level,track,race,point,course,wheel,enemies,enemy,save,ball,bonus,weapon,block,obstacle,increase,defend,stunt,truck,car,skill,star,upgrade,graphic,player,bomb,ship,wave,item,alien,steroid,bubble,score,feature,type,zombie,friend,high,object',
				   'adventure,level,item,game,points,enemies,object,treasure,rescue,skill,obstacle,star,weapon,graphic,clue,creature,player,series,defend,need,feature,zombie,alien,character,stone,dangerous',
				   'sports,sport,level,point,ball,again,obstacle,player,skill,bonus,finish,track,high,friend,opponent,score,team,time,race,goal,tennis,arrow,court,graphic,achievement,trick,shot,mode,character,stunt,jump,challenge,hole,card,level,piece,tile,pair,ball,block,letter,rule,mode,square,pile,opponent,board',
				   'shoot,shooting,shooter,shot,gun,weapon,aim,enemy,enemies,skill,bullet,point,alien,target,space,bomb,tank,invader,balloon,invade,dangerous,rocket,laser',
				   'casino,card,suit,pile,number,slot,slots,bonus,graphic,rule,foundation,hand,texas,level,high,score,skill,sequence,ball,friend,spin,time,crap,combination,opponent,mode,credit,chip,chips,colour,column,color',
				   'dress,dress-up,clothing,clothes,styling,style,styles,accessories,girl,outfit,item,various,hair,look,shoes,color,love,delicious,nail,nails,gorgeous,pretty,necklace,fantasy,artist,flower,fabulous,glamorous,famous,costume',
				   'arcade,retro,classic,pac-man,pacman,level,points,jump,atari,skill,block,brick,metroid,worm,mega,extra',
				   'strategy,level,point,defense,enemies,enemy,block,tower,item,puzzle,ball,skill,object,wave,weapon,piece,card,upgrade,ship,type,finish,bomb,achievement,number,tile,find,hidden');
	$count = array(0,0,0,0,0,0,0,0,0,0);
	foreach ($allwords as $word) {
		for($i=0; $i<=9;$i++) {
			$keys = explode(",", $keywords[$i]);
			foreach ($keys as &$key) {
				if(in_array($word, $keys)) {
					if($word == 'action') { return 2; } //action
					if($word == 'adventure') { return 3; } //adventure
					if($word == 'puzzle') { return 1; } //puzzle
					if($word == 'sports') { return 4; } //sports
					if($word == 'shooter') { return 5; } //shooter
					if($word == 'casino') { return 6; } //casino
					if($word == 'dressup') { return 8; } //dressup
					if($word == 'arcade') { return 9; } //arcade
					if($word == 'startegy') { return 10; } //strategy
					$count[$i]++;
				}
			}	
		}
	}
	if (max($count) == 0) { return 7; } // no matches so return "other" category
	print_r($count);
	$max = array_keys($count, max($count));
	return $max[0] + 1;
}

$query = yasDB_select("SELECT `title`, `description` FROM `fogfeed` WHERE `id` = 47");
$data = '';
while ($text = $query->fetch_array(MYSQLI_ASSOC)) {
	$data = $data . ' ' . $text['title'] . ' ' . $text['description'];
}
$words = wordsArray($data);
echo "<br/>".getCategory($words);
?>