ThinkPHP3.2.3擴充之自動分詞擷取關鍵字

來源:互聯網
上載者:User

標籤:技術分享   content   json   編程   tle   3.2   simple   zha   事物   

ThinkPHP自動擷取關鍵詞調用線上discuz詞庫

先按照路徑放好外掛程式

        /**     * 自動擷取關鍵詞(調用第三方外掛程式)     * @return [type] [description]     * www.shouce.ren     */    public function keyword()    {        Vendor(‘autokeyword.AutoKeyword‘);        $keyword = new \AutoKeyword();        $str=‘自動擷取關鍵詞並發大資料我們大家好吃飯啦調用第三方外掛程式‘;        $title = trim($str);        $keys=$keyword::discuz($title);        var_dump($keys);    }

外掛程式源碼:

AutoKeyword.php

<?php /** * 分詞 * @author zhao jinhan <[email protected]> *  */class AutoKeyword{     /**     * 使用discuz詞庫     * @param unknown_type $title     * @param unknown_type $content     */    public static function discuz ($title = ‘‘, $content = ‘‘)    {        $subjectenc = rawurlencode(strip_tags($title));        $messageenc = rawurlencode(strip_tags(preg_replace("/\[.+?\]/U", ‘‘, $content)));        $data = @implode(‘‘, file("http://keyword.discuz.com/related_kw.html?title=$subjectenc&content=$messageenc&ics=utf-8&ocs=utf-8"));         if ($data) {            $parser = xml_parser_create();            xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);            xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);            xml_parse_into_struct($parser, $data, $values, $index);            xml_parser_free($parser);            $kws = array ();            foreach ($values as $valuearray) {                if ($valuearray[‘tag‘] == ‘kw‘ || $valuearray[‘tag‘] == ‘ekw‘)                    $kws[] = trim($valuearray[‘value‘]);            }//             $return = ‘‘;//             $dot=‘‘;//             if ($kws) {//                 foreach ($kws as $kw) {//                     $kw = CHtml::encode(strip_tags($kw));//                     $return .= $dot.$kw ;//                     $dot = ‘,‘;//                 }//                 $return = trim($return);//             }             return $kws;        }     }

 

/**     * 簡易自訂擷取關鍵詞     * @param string $title     * @param string $content     * @return JSON     */    public static function simple($title = ‘‘, $content=‘‘){        $words = include_once dirname(__FILE__).DIRECTORY_SEPARATOR.‘words.php‘;        $data = array();        $max = 10; //最多匹配10個        if($words){            foreach((array)$words as $word){                if(count($data) > $max){                    break;                }else{                    if($title && preg_match(‘/‘.$word.‘/is‘, $title)){                        array_push($data, $word);                    }                    if($content && preg_match(‘/‘.$word.‘/is‘, $content)){                        if(!in_array($word, $data)){                            array_push($data, $word);                        }                    }                }            }        }        return array_unique($data);    }  }

word.php

<?php/** * 詞庫(權重越高越靠前) */return array(    ‘php‘,    ‘mysql‘,    ‘web‘,    ‘html‘,    ‘js‘,    ‘jquery‘,    ‘sql‘,    ‘myisam‘,    ‘innodb‘,    ‘apache‘,    ‘nginx‘,    ‘yii‘,    ‘linux‘,    ‘cms‘,    ‘yiifcms‘,    ‘redis‘,    ‘memcache‘,    ‘explain‘,    ‘ueditor‘,    ‘kindeditor‘,    ‘api‘,    ‘w3c‘,    ‘高效能‘,    ‘大資料‘,    ‘預存程序‘,    ‘事物‘,    ‘觸發器‘,    ‘索引‘,    ‘並發‘,    ‘編程‘,    ‘演算法‘,    ‘排序‘,    ‘安裝包‘,    ‘下載‘,    ‘伺服器‘,    ‘手冊‘,    ‘指南‘,    ‘文章‘,    ‘圖集‘,);

 

ThinkPHP3.2.3擴充之自動分詞擷取關鍵字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.