Home > php教程 > php手册 > Thinkphp3.2使用scws中文分词 提取关键词,

Thinkphp3.2使用scws中文分词 提取关键词,

WBOY
Release: 2016-06-13 08:52:52
Original
1051 people have browsed it

Thinkphp3.2使用scws中文分词 提取关键词,

SCWS 是 Simple Chinese Word Segmentation 的首字母缩写(即:简易中文分词系统)。
1.下载scws官方提供的类(这里使用的是pscws第四版的)
http://www.xunsearch.com/scws/down/pscws4-20081221.tar.bz2
下载XDB 词典文件 (这里使用的是utf8简体中文词典包)
http://www.xunsearch.com/scws/down/scws-dict-chs-utf8.tar.bz2
2.解压scws类Pscws.class.php(这里我把pscws4.class.php文件名换成了pscws.class.php)和XDB_R.class.php(这里我把xdb_r.class.php文件名换成了大写的XDB_R.class.php)放到ThinkPHP\Library\Org\Util目录下面。
3.然后修改Pscws.class.php
加入命名空间

<span>1</span> namespace Org\Util;
Copy after login

将类的名称改为Pscws

把require_once (<span>dirname</span>(<span>__FILE__</span>) . '/XBD_R.class.php');这段代码删除掉。
Copy after login

修改XDB_R.class.php
加入命名空间

namespace Org\Util;
Copy after login

4.解压XDB 词典文件
在Public\admin目录下新建一个dict文件夹,然后将XDB 词典文件的dict.utf8.xdb解压到词目录下,再把scws类中的etc下面的rules.utf8.ini放到此目录下面。
5.在入口文件加入一行常量定义代码(其实就是定义词典文件和配置文件的路径)

<span>define</span>("CONF_PATH", <span>dirname</span>(<span>__FILE__</span>)."/Public/admin/dict/");
Copy after login

6.在IndexController.class.php控制器里面建立一个私有方法,供其他方法调用

 <span>/*</span><span>*
     * 中文分词  
         * @params string $title 需要分词的语句 
         * @params int $num  分词个数,默认不用填写
     *</span><span>*/</span>
    <span>private</span> <span>function</span> get_tags(<span>$title</span>,<span>$num</span>=<span>null</span><span>){        
        </span><span>$pscws</span> = <span>new</span> \Org\Util\Pscws('utf8'<span>);
        </span><span>$pscws</span>->set_dict(CONF_PATH . 'dict.utf8.xdb'<span>);
        </span><span>$pscws</span>->set_rule(CONF_PATH . 'rules.utf8.ini'<span>);
        </span><span>$pscws</span>->set_ignore(<span>true</span><span>);
        </span><span>$pscws</span>->send_text(<span>$title</span><span>);
        </span><span>$words</span> = <span>$pscws</span>->get_tops(<span>$num</span><span>);
        </span><span>$pscws</span>-><span>close();
        </span><span>$tags</span> = <span>array</span><span>();
        </span><span>foreach</span> (<span>$words</span> <span>as</span> <span>$val</span><span>) {
            </span><span>$tags</span>[] = <span>$val</span>['word'<span>];
        }
        </span><span>return</span> <span>implode</span>(',', <span>$tags</span><span>);
    }
      </span><span>/*</span><span>*
     * 商品搜索结果页
     *</span><span>*/</span>
    <span>public</span> <span>function</span><span> search(){
        </span><span>$rzt</span>=<span>$this</span>->get_tags("新款 牛漆皮小尖头直跟高跟单鞋910033 灰羊猄(7.31发货) 39"<span>);
        </span><span>print_r</span>(<span>$rzt</span><span>);
    }</span>
Copy after login

显示的结果为:

漆皮,单鞋,尖头,高跟,新款,发货,910033,7.31,39
Copy after login

 

 

 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template