php 中文分词使用

来源:互联网 发布:java多态 编辑:程序博客网 时间:2024/06/06 20:57

scws 中文分词 文档地址 :http://www.xunsearch.com/scws/docs.php


1. 取得 scws-1.2.3 的代码

wget http://www.xunsearch.com/scws/down/scws-1.2.3.tar.bz2


2. 解开压缩包
[hightman@d1 ~]$ tar xvjf scws-1.2.3.tar.bz2


3. 进入目录执行配置脚本和编译
[hightman@d1 ~]$ cd scws-1.2.3[hightman@d1 ~/scws-1.2.3]$ ./configure --prefix=/usr/local/scws && make && make install


注:这里和通用的 GNU 软件安装方式一样,具体选项参数执行 ./configure --help 查看。
常用选项为:--prefix=<scws的安装目录>


4. 顺利的话已经编译并安装成功到 /usr/local/scws 中了,执行下面命令看看文件是否存在
[hightman@d1 ~/scws-1.2.3]$ ls -al /usr/local/scws/lib/libscws.la


5. 试试执行 scws-cli 文件
[hightman@d1 ~/scws-1.2.3]$ /usr/local/scws/bin/scws -hscws (scws-cli/1.2.3)Simple Chinese Word Segmentation - Command line usage.Copyright (C)2007 by hightman.


6 用 wget 下载并解压词典,或从主页下载然后自行解压再将 *.xdb 放入 /usr/local/scws/etc 目录中
[hightman@d1 ~/scws-1.2.3]$ cd /usr/local/scws/etc[hightman@d1 /usr/local/scws/etc]$ wget http://www.xunsearch.com/scws/down/scws-dict-chs-gbk.tar.bz2[hightman@d1 /usr/local/scws/etc]$ wget http://www.xunsearch.com/scws/down/scws-dict-chs-utf8.tar.bz2[hightman@d1 /usr/local/scws/etc]$ tar xvjf scws-dict-chs-gbk.tar.bz2[hightman@d1 /usr/local/scws/etc]$ tar xvjf scws-dict-chs-utf8.tar.bz2


7.8.9. 这三步是用C程序测试 直接跳过 我们要做的是php测试


10. 在 php 中调用分词,安装 php 扩展。

    假设您已经将 scws 按上述步骤安装到 /usr/local/scws 中。
    安装此扩展要求您的 php 和系统环境安装了相应的 autoconf automake 工具及 phpize 。


    1) 进入源码目录的 phpext/ 目录 ( cd ~/scws-1.2.3 )
    这里出现
    -bash: /usr/local/src/php-5.6.3/scripts/phpize: Permission denied
    解决办法:
    
    $ cd ~/scws-1.2.3/phpext/    $ locate phpize


    2) 执行 phpize (在PHP安装目录的bin/目录下)
    
$ /usr/local/php/bin/phpize

    
    3) 执行 ./configure --with-scws=/usr/local/scws 
       若 php 安装在特殊目录 $php_prefix, 则请在 configure 后加上 --with-php-config=$php_prefix/bin/php-config
       编译出现错误
       configure: error: Cannot find php-config. Please use --with-php-config=PATH
       解决办法:
   下面第一条命令 查找php-config 目录位置    
 $ locate php-config $ ./configure --with-php-config=/usr/local/php/bin/php-config --with-scws=/usr/local/scws


    4) 执行 make 然后用 root 身份执行 make install     

   # make && make install


    安装成功后 扩展共享目录:/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

    5) 在 php.ini 中加入以下几行
    我的地址:/usr/local/php/etc/php.ini

[scws];; 注意请检查 php.ini 中的 extension_dir 的设定值是否正确, 否则请将 extension_dir 设为空,; 再把 extension = scws.so 指定绝对路径。;extension = scws.soscws.default.charset = utf8scws.default.fpath = /usr/local/scws/etc


    6) 命令行下执行 php -m 就能看到 scws 了或者在 phpinfo() 中看看关于 scws 的部分,记得要重启 web 服务器
       才能使新的 php.ini 生效。
    注意 可能有的php 客户端版本比较低, 使用 php -m 显示的是客户端的版本 并不是实际安装的版本
    那么可以通过 程序中的 phpinfo() 在浏览器上访问查看
    重启脚本如下
    $ service php-fpm restart    $ service nginx restart


    7) 这样就算安装完成了,余下的工作只是PHP代码编写问题了。
       关于 PHP 扩展的使用说明请参看代码中 phpext/README.md 文件或其它文档章节。

    

$ cat README.md  #获取函数使用说明

下面 写一个常用的 去重的分词结果:

<?php$so = scws_new();$so->set_charset('utf8');// 这里没有调用 set_dict 和 set_rule 系统会自动试调用 ini 中指定路径下的词典和规则文件$so->send_text("我是一个中国人,我会C++语言,我也有很多T恤衣服");$words = array();while ($tmp = $so->get_result()){    foreach ($tmp as $val) {    if(!new_in_array($val, $words,'word')){            $words[] = array(        'word' => $val['word'],        'weight' => $val['idf'],    );    }    }}print_r($words);$so->close();function new_in_array($need,$array,$column=''){$flag = false;foreach ($array as $val) {if($val[$column] == $need[$column]){$flag = true;break;}}return $flag;}?>





2 0
原创粉丝点击