zoukankan      html  css  js  c++  java
  • ThinkPHP自动获取关键词(调用第三方插件)

    ThinkPHP自动获取关键词调用在线discuz词库

    先按照下图路径放好插件

    2016-03-29_114754.jpg

    方法如下

    1.         /**
    2.      * 自动获取关键词(调用第三方插件)
    3.      * @return [type] [description]
    4.      * www.shouce.ren
    5.      */
    6.     public function keyword()
    7.     {
    8.         Vendor('autokeyword.AutoKeyword');
    9.         $keyword = new AutoKeyword();
    10.         $str='自动获取关键词并发大数据我们大家好吃饭啦调用第三方插件';
    11.         $title = trim($str);
    12.         $keys=$keyword::discuz($title);
    13.         var_dump($keys);
    14.     }
    复制

    插件源码:

    AutoKeyword.php

    1. <?php
    2.  
    3. /**
    4.  * 分词
    5.  * @author zhao jinhan <326196998@qq.com>
    6.  * 
    7.  */
    8. class AutoKeyword
    9. {
    10.  
    11.     /**
    12.      * 使用discuz词库
    13.      * @param unknown_type $title
    14.      * @param unknown_type $content
    15.      */
    16.     public static function discuz ($title = '', $content = '')
    17.     {
    18.         $subjectenc = rawurlencode(strip_tags($title));
    19.         $messageenc = rawurlencode(strip_tags(preg_replace("/[.+?]/U", '', $content)));
    20.         $data = @implode('', file("http://keyword.discuz.com/related_kw.html?title=$subjectenc&content=$messageenc&ics=utf-8&ocs=utf-8"));
    21.  
    22.         if ($data) {
    23.             $parser = xml_parser_create();
    24.             xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    25.             xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    26.             xml_parse_into_struct($parser, $data, $values, $index);
    27.             xml_parser_free($parser);
    28.             $kws = array ();
    29.             foreach ($values as $valuearray) {
    30.                 if ($valuearray['tag'] == 'kw' || $valuearray['tag'] == 'ekw')
    31.                     $kws[] = trim($valuearray['value']);
    32.             }
    33. //             $return = '';
    34. //             $dot='';
    35. //             if ($kws) {
    36. //                 foreach ($kws as $kw) {
    37. //                     $kw = CHtml::encode(strip_tags($kw));
    38. //                     $return .= $dot.$kw ;
    39. //                     $dot = ',';
    40. //                 }
    41. //                 $return = trim($return);
    42. //             }
    43.  
    44.             return $kws;
    45.         }
    46.  
    47.     }
    48.     /**
    49.      * 简易自定义获取关键词
    50.      * @param string $title
    51.      * @param string $content
    52.      * @return JSON
    53.      */
    54.     public static function simple($title = '', $content=''){
    55.         $words = include_once dirname(__FILE__).DIRECTORY_SEPARATOR.'words.php';
    56.         $data = array();
    57.         $max = 10; //最多匹配10个
    58.         if($words){
    59.             foreach((array)$words as $word){
    60.                 if(count($data) > $max){
    61.                     break;
    62.                 }else{
    63.                     if($title && preg_match('/'.$word.'/is', $title)){
    64.                         array_push($data, $word);
    65.                     }
    66.                     if($content && preg_match('/'.$word.'/is', $content)){
    67.                         if(!in_array($word, $data)){
    68.                             array_push($data, $word);
    69.                         }
    70.                     }
    71.                 }
    72.             }
    73.         }
    74.         return array_unique($data);
    75.     }
    76.  
    77.  
    78. }
    复制

    words.php

    1. <?php
    2. /**
    3.  * 词库(权重越高越靠前)
    4.  */
    5. return array(
    6.     'php',
    7.     'mysql',
    8.     'web',
    9.     'html',
    10.     'js',
    11.     'jquery',
    12.     'sql',
    13.     'myisam',
    14.     'innodb',
    15.     'apache',
    16.     'nginx',
    17.     'yii',
    18.     'linux',
    19.     'cms',
    20.     'yiifcms',
    21.     'redis',
    22.     'memcache',
    23.     'explain',
    24.     'ueditor',
    25.     'kindeditor',
    26.     'api',
    27.     'w3c',
    28.     '高性能',
    29.     '大数据',
    30.     '存储过程',
    31.     '事物',
    32.     '触发器',
    33.     '索引',
    34.     '并发',
    35.     '编程',
    36.     '算法',
    37.     '排序',
    38.     '安装包',
    39.     '下载',
    40.     '服务器',
    41.     '手册',
    42.     '指南',
    43.     '文章',
    44.     '图集',
    45. );
  • 相关阅读:
    java中的Iterator和Iterable 区别
    Ubuntu 12.04 部署 PostGIS 2.1
    postgres模板数据库
    在ubuntu 10.04 上QGIS的安装步骤
    js----解决异步之Generator && async
    js----promise.all() promise.race()
    js----异步之Promise,Generator,Async
    js----CSRF-跨站请求伪造攻击
    js----js实现继承的方式及其优缺点
    vue----nextTick获取最新dom结构
  • 原文地址:https://www.cnblogs.com/ZDPPU/p/5823803.html
Copyright © 2011-2022 走看看