sphider+scws

一、安装scws
============================================

1. 取得 scws-1.2.0 的代码
wget http://www.ftphp.com/scws/down/scws-1.2.0.tar.bz2

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

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

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

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

gbk.tar.bz2
[hightman@d1 /usr/local/scws/etc]$ wget http://www.ftphp.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

6. 如果您需要在 php 中调用分词,建议继续阅读本文安装 php 扩展(和安装APC一样)。
假设您已经将 scws 按上述步骤安装到 /usr/local/scws 中。
tar -zxvf xampp-linux-devel-1.7.7.tar.gz

cp -r ./lampp/include /opt/lampp/

yum install php-devel pcre-devel
yum groupinstall "Development Tools"

cd ~/scws-1.2.0/phpext/
/opt/lampp/bin/phpize
whereis php-config

找到php-config的路径,下面用

./configure --with-scws=/usr/local/scws --with-php-config=/opt/lampp/bin/php-config
make
make install

vi /opt/lampp/etc/php.ini
添加

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

/opt/lampp/lampp stopapache
/opt/lampp/lampp startapache

    6) 命令行下执行 php -m 就能看到 scws 了或者在 phpinfo() 中看看关于 scws 的部分,记得要重

启 web 服务器
       才能使新的 php.ini 生效。


二、安装sphider
============================================
1、创建shpider数据库(GBK编码)
2、导入table.sql或者用admin/install.php安装,可能数据库创建会失败
3、sphider/setting/database.php修改配置


三、结合
============================================
两者部署无误后,修改Sphider,找到admin文件夹下的spider文件,首先在开始加入代码初始化分词程序
    $cws = scws_new();  
    $cws->set_charset('gbk');  
    $cws->set_rule('/usr/local/scws/etc/rules.ini'); //注意路径  
    $cws->set_dict('/usr/local/scws/etc/dict.xdb');  
    $cws->set_ignore(true);  

注意这里使用的gbk,如果你的网页用的utf8编码,要把这里以及词典和规则文件的位置更改一下

 

在index_url函数中,把原有的英文分词替换掉,在$wordarray = unique_array(explode(" ", $data

['content']));前面加上

    global $cws;
    $cws->send_text($data['content']);  
    $list = $cws->get_tops(1000, $xattr);  
    settype($list, 'array');  
    $wordarray=array();  
    $i=0;  
    // segment  
    foreach ($list as $tmp)  
    {         
        $wordarray[$i][1]=$tmp['word'];  
        $wordarray[$i][2]=$tmp['times'];  
        $i++;  
    }  

删除
    $wordarray = unique_array(explode(" ", $data['content']));  

    $wordarray = calc_weights ($wordarray, $title, $host, $path, $data['keywords']);  

两个语句,因为Sphider原有的英文分词这里就完全没必要用了,这里可以自行对$wordarray进行限制和

优化,这里我写的很简单。

 

修改完成后,爬虫就能正常对中文进行分词了,效果还不错,注意如果出现乱码注意网页或者辞典编码是

utf8还是gb2312。

把/sphider/templates/standard/header.html的charset改为gb2312
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值