public static function extractTags($content, $top_k = 20, $options = array())
    {

        $defaults = array(
            'mode'=>'default'
        );

        $options = array_merge($defaults, $options);

        $tags = array();

        if (isset($options['allowPOS'])) {
            $wordsPos = Posseg::cut($content);

            foreach ($wordsPos as $key => $word) {
                if (in_array($word['tag'], $options['allowPOS'])) {
                    $words[] = $word['word'];
                }
            }
        } else {
            $words = Jieba::cut($content);
        }

        $freq = array();
        $total = 0.0;

        foreach ($words as $w) {
            $w = trim($w);
            if (mb_strlen($w, 'UTF-8')<2) {
                continue;
            }

            if (in_array(strtolower($w), self::$stop_words)) {
                continue;
            }
            if (isset($freq[$w])) {
                $freq[$w] = $freq[$w] + 1.0;
            } else {
                $freq[$w] = 0.0 + 1.0;
            }
            $total = $total + 1.0;
        }

        foreach ($freq as $k => $v) {
            $freq[$k] = $v/$total;
        }

        $tf_idf_list = array();

        foreach ($freq as $k => $v) {
            if (isset(self::$idf_freq[$k])) {
                $idf_freq = self::$idf_freq[$k];
            } else {
                $idf_freq = self::$median_idf;
            }
            $tf_idf_list[$k] = $v * $idf_freq;
        }

        arsort($tf_idf_list);

        $tags = array_slice($tf_idf_list, 0, $top_k, true);

        return $tags;

    }
public static function extractTags($content, $top_k = 20, $options = array())
    {

        $defaults = array(
            'mode'=>'default'
        );

        $options = array_merge($defaults, $options);

        $tags = array();

        if (isset($options['allowPOS'])) {
            $wordsPos = Posseg::cut($content);

            foreach ($wordsPos as $key => $word) {
                if (in_array($word['tag'], $options['allowPOS'])) {
                    $words[] = $word['word'];
                }
            }
        } else {
            $words = Jieba::cut($content);
        }

        $freq = array();
        $total = 0.0;

        foreach ($words as $w) {
            $w = trim($w);
            if (mb_strlen($w, 'UTF-8')<2) {
                continue;
            }

            if (in_array(strtolower($w), self::$stop_words)) {
                continue;
            }
            if (isset($freq[$w])) {
                $freq[$w] = $freq[$w] + 1.0;
            } else {
                $freq[$w] = 0.0 + 1.0;
            }
            $total = $total + 1.0;
        }

        foreach ($freq as $k => $v) {
            $freq[$k] = $v/$total;
        }

        $tf_idf_list = array();

        foreach ($freq as $k => $v) {
            if (isset(self::$idf_freq[$k])) {
                $idf_freq = self::$idf_freq[$k];
            } else {
                $idf_freq = self::$median_idf;
            }
            $tf_idf_list[$k] = $v * $idf_freq;
        }

        arsort($tf_idf_list);

        $tags = array_slice($tf_idf_list, 0, $top_k, true);

        return $tags;

    }
public static function extractTags ( $ content , $ top_k = 20 , $ options = array ( ) ) { $ defaults = array ( 'mode' => 'default' ) ; $ options = array_merge ( $ defaults , $ options ) ; $ tags = array ( ) ; if ( isset ( $ options [ 'allowPOS' ] ) ) { $ wordsPos = Posseg :: cut ( $ content ) ; foreach ( $ wordsPos as $ key => $ word ) { if ( in_array ( $ word [ 'tag' ] , $ options [ 'allowPOS' ] ) ) { $ words [ ] = $ word [ 'word' ] ; } } } else { $ words = Jieba :: cut ( $ content ) ; } $ freq = array ( ) ; $ total = 0.0 ; foreach ( $ words as $ w ) { $ w = trim ( $ w ) ; if ( mb_strlen ( $ w , 'UTF-8' ) < 2 ) { continue ; } if ( in_array ( strtolower ( $ w ) , self :: $ stop_words ) ) { continue ; } if ( isset ( $ freq [ $ w ] ) ) { $ freq [ $ w ] = $ freq [ $ w ] + 1.0 ; } else { $ freq [ $ w ] = 0.0 + 1.0 ; } $ total = $ total + 1.0 ; } foreach ( $ freq as $ k => $ v ) { $ freq [ $ k ] = $ v / $ total ; } $ tf_idf_list = array ( ) ; foreach ( $ freq as $ k => $ v ) { if ( isset ( self :: $ idf_freq [ $ k ] ) ) { $ idf_freq = self :: $ idf_freq [ $ k ] ; } else { $ idf_freq = self :: $ median_idf ; } $ tf_idf_list [ $ k ] = $ v * $ idf_freq ; } arsort ( $ tf_idf_list ) ; $ tags = array_slice ( $ tf_idf_list , 0 , $ top_k , true ) ; return $ tags ; }
Static method extractTags

@param string  $content  # input content
@param int     $top_k    # top_k
@param array   $options  # other options

@return array $tags
Static method extractTags