/** * Set label for each tweet based on label definitions */ public function actionSetLabels() { //step 0: set label weight based on attribute counting //$this->actionSetLabelWeight(); //step 1: get all labels $arrLabelList = array(); $objLabels = Label::model()->findAll(array('order' => 'weight DESC')); $objTweet = Tweet::model()->findAll('label_id IS NULL'); $arrTweetList = array(); foreach ($objLabels as $label) { $labelList = explode(" ", $label->name); foreach ($objTweet as $tweet) { $arrTweetWords = explode(" ", strtolower($tweet->tweet2)); asort($arrTweetWords); $wordCounter = 0; foreach ($labelList as $labelItem) { if (in_array(strtolower($labelItem), $arrTweetWords)) { $wordCounter++; } } if ($wordCounter > 0) { $arrTweetList[$tweet->id][$label->id] = $wordCounter; } } } //step 2: get all tweets and set label foreach ($objTweet as $tweet) { if (isset($arrTweetList[$tweet->id])) { if (count($arrTweetList[$tweet->id]) > 0) { asort($arrTweetList[$tweet->id]); $label_id = end(array_keys($arrTweetList[$tweet->id])); $tweet->label_id = $label_id; $tweet->save(); } } } //foreach print_r($arrTweetList); exit; //step 3: match tweets with labels }