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. );
  • 相关阅读:
    洛谷 P1260 工程规划(差分约束)
    洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
    [模板]单调队列
    [模板]LIS(最长上升子序列)
    洛谷 P2899 [USACO08JAN]手机网络Cell Phone Network(树形动规)
    如何求数字n的因数个数及因数和
    [模板]tarjan缩点+拓扑排序
    itext生成pdf(附带页眉,页脚,页码)
    工作总结03
    工作总结02(海报上传模块)
  • 原文地址:https://www.cnblogs.com/ZDPPU/p/5823803.html
Copyright © 2011-2022 走看看