本篇文章給大家介紹一下php使用PHPAnalysis提取關(guān)鍵字中文分詞的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。
需求:做SEO的keywords時,需要從標(biāo)題或者正文里提取關(guān)鍵字
1.PHPAnalysis下載地址
http://www.phpbone.com/phpanalysis/#api
原下載地址打不開,已上傳到github
https://github.com/feixuekeji/PHPAnalysis
下載后解壓放到extend目錄下(以tp5為例,其他目錄也行)
2.封裝
<?php /** * @auther: xxf * Date: 2019/8/19 * Time: 11:04 */ namespace WordAnalysis; /** * 中文分詞提取關(guān)鍵字 */ class Analysis { /** * Notes:關(guān)鍵字提取 * @auther: xxf * Date: 2019/8/19 * Time: 11:09 * @param string $content * @param int $num 獲取數(shù)量 * @return string */ public static function getKeywords($content = "",$num = 3) { if (empty ( $content )) { return ''; } require_once 'phpanalysis.class.php'; PhpAnalysis::$loadInit = false; $pa = new PhpAnalysis ( 'utf-8', 'utf-8', false ); $pa->LoadDict (); $pa->SetSource ($content); $pa->StartAnalysis ( true ); $tags = $pa->GetFinallyKeywords ($num); // 獲取文章中的n個關(guān)鍵字 return $tags;//返回關(guān)鍵字 } }
使用
$data['seo']['keyword'] = Analysis::getKeywords($article_info['title']);
推薦學(xué)習(xí):php視頻教程