Home >Backend Development >PHP Tutorial >How to use PHPAnalysis to extract Chinese word segmentation of keywords in php

How to use PHPAnalysis to extract Chinese word segmentation of keywords in php

藏色散人
藏色散人forward
2020-06-24 13:51:353593browse

Requirements: When doing SEO keywords, you need to extract keywords from the title or text

1.PHPAnalysis download address

https://github.com/feixuekeji/PHPAnalysis

After downloading, unzip and place it in the extend directory (taking tp5 as an example, other directories are also acceptable)

How to use PHPAnalysis to extract Chinese word segmentation of keywords in php

2. Encapsulation

<?php
/**
 * @auther: xxf
 * Date: 2019/8/19
 * Time: 11:04
 */

namespace WordAnalysis;

/**
 * 中文分词提取关键字
 */
class Analysis
{
    /**
     * Notes:关键字提取
     * @auther: xxf
     * Date: 2019/8/19
     * Time: 11:09
     * @param string $content
     * @param int $num 获取数量
     * @return string
     */
    public static function getKeywords($content = "",$num = 3) {
        if (empty ( $content )) {
            return &#39;&#39;;
        }

        require_once &#39;phpanalysis.class.php&#39;;


        \PhpAnalysis::$loadInit = false;
        $pa = new \PhpAnalysis ( &#39;utf-8&#39;, &#39;utf-8&#39;, false );
        $pa->LoadDict ();
        $pa->SetSource ($content);
        $pa->StartAnalysis ( true );

        $tags = $pa->GetFinallyKeywords ($num); // 获取文章中的n个关键字
        return $tags;//返回关键字
    }

}

3. Use

$data[&#39;seo&#39;][&#39;keyword&#39;] = Analysis::getKeywords($article_info[&#39;title&#39;]);

For more related technical articles, please visit PHP Chinese website!

The above is the detailed content of How to use PHPAnalysis to extract Chinese word segmentation of keywords in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete