public static function afterTopicInsert($tc) { $editor = new \app\lib\Editor(['editor' => Yii::$app->params['settings']['editor']]); $content = $editor->parse($tc->content); $pa = new PhpAnalysis(); $pa->SetSource($tc->topic->title . strip_tags($content)); $pa->resultType = 2; $pa->differMax = true; $pa->StartAnalysis(); $tagNames = $pa->GetFinallyKeywords(3); $tagNames = explode(',', $tagNames); $tags = static::find()->select(['id', 'name'])->where(['in', 'name', $tagNames])->indexBy('name')->all(); foreach ($tagNames as $tn) { if (!empty($tags) && !empty($tags[$tn])) { $tag = $tags[$tn]; $tagTopic = new TagTopic(['tag_id' => $tag->id, 'topic_id' => $tc->topic_id]); $tagTopic->save(false); $tag->updateCounters(['topic_count' => 1]); } else { $tag = new static(['name' => $tn, 'topic_count' => 1]); $tag->save(false); $tagTopic = new TagTopic(['tag_id' => $tag->id, 'topic_id' => $tc->topic_id]); $tagTopic->save(false); } } }
/** * 实现分词功能 */ public function run($content, $num) { PhpAnalysis::$loadInit = false; $pa = new PhpAnalysis('utf-8', 'utf-8', false); $pa->LoadDict(); $pa->SetSource($content); $pa->StartAnalysis(false); $tags = $pa->GetFinallyKeywords($num - 1); return $tags; }