IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Centos下安装sphinx,SCWS PHP插件

    二愣子小强发表于 2015-12-17 05:18:07
    love 0

    1. 取得 scws-1.2.2 的代码

    [root@localhost ~]# wget http://www.xunsearch.com/scws/down/scws-1.2.2.tar.bz2

    2. 解开压缩包

    [root@localhost ~]# tar xvjf scws-1.2.2.tar.bz2

    3. 进入目录执行配置脚本和编译

    [root@localhost ~]# cd scws-1.2.2
    [root@localhost scws-1.2.2]# ./configure --prefix=/usr/local/scws ; make ; make install

    4. 顺利的话已经编译并安装成功到 /usr/local/scws 中了,执行下面命令看看文件是否存在

    [root@localhost scws-1.2.2]# ls -al /usr/local/scws/lib/libscws.la

    5. 试试执行 scws-cli 文件

    [root@localhost scws-1.2.2]# /usr/local/scws/bin/scws -h
    scws (scws-cli/1.2.2)
    Simple Chinese Word Segmentation - Command line usage.
    Copyright (C)2007 by hightman.

    Usage: scws [options] [input] [output]
      -i <file|string> input string or filepath
                       (default: try to read from <stdin> everyline)
      -o <file>        output filepath (default to <stdout>)
      -c <charset>     set the charset (default: gbk)
                       charset must been same with dictionary & ruleset
      -r <file>        set the ruleset file (default: none)
      -d <file>        set the dictionary file[s] (default: none)
                       if there are multi files, split filepath use ':'
                       if the file suffix is .txt, it will be treated as plain text dict.
      -M <1~15>        use multi child words mode(中国人->中国+人+中国人)
                       1|2|4|8: short|duality|zmain|zall
      -I               ignore the all mark symbol such as ,:
      -A               show the word attribute
      -E               import the xdb dict into xtree(memory)
      -N               don't show time usage and warnings
      -D               debug segment, see the segment detail
      -U               use duality algorithm for single chinese
      -t <NUM>         fetch the top words instead of segment
      -a [~]<attr1,attr2,...>   prefix by ~ means exclude them.
                       For topwords, exclude or include some word attrs
      -v        Show the version.
      -h        Show this page for help.
    Report bugs to <hightman2@yahoo.com.cn>

    6 用 wget 下载并解压词典,或从主页下载然后自行解压再将 *.xdb 放入 /usr/local/scws/etc 目录中

    [root@localhost scws-1.2.2]# cd /usr/local/scws/etc
    [root@localhost etc]# wget http://www.xunsearch.com/scws/down/scws-dict-chs-gbk.tar.bz2
    [root@localhost etc]# wget http://www.xunsearch.com/scws/down/scws-dict-chs-utf8.tar.bz2
    [root@localhost etc]# tar xvjf scws-dict-chs-gbk.tar.bz2
    [root@localhost etc]# tar xvjf scws-dict-chs-utf8.tar.bz2

    7. 写个小程序测试一下

    [root@localhost etc]# cat > test.c
    #include <scws.h>
    #include <stdio.h>
    main()
    {
      scws_t s;
      s = scws_new();
      scws_free(s);
      printf("test ok!\n");
    }

    8. 编译测试程序

    gcc -o test -I/usr/local/scws/include -L/usr/local/scws/lib test.c -lscws -Wl,--rpath -Wl,/usr/local/scws/lib ./test

    9. 这样就好顺利安装完毕可以使用 libscws 这套 C-API 了

    10. 如果您需要在 php 中调用分词,建议继续阅读本文安装 php 扩展,否则可跳过不看。

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

    1) 进入源码目录的 phpext/ 目录

    [root@localhost etc]# cd /root/scws-1.2.2/phpext

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

    [root@localhost etc]# whereis php
    php: /usr/bin/php /etc/php.ini /etc/php.d /usr/lib64/php /usr/include/php /usr/share/php /usr/share/man/man1/php.1.gz

    3) 执行

    [root@localhost etc]# /usr/bin/phpize ./configure --with-scws=/usr/local/scws
    Configuring for:
    PHP Api Version:         20121113
    Zend Module Api No:      20121212
    Zend Extension Api No:   220121212

    若 php 安装在特殊目录 $php_prefix, 则请在 configure 后加上 –with-php-config=$php_prefix/bin/php-config

    4) 执行 make 然后用 root 身份执行 make install
    5) 在 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

    6) 命令行下执行 php -m 就能看到 scws 了或者在 phpinfo() 中看看关于 scws 的部分,记得要重启 web 服务器才能使新的 php.ini 生效。
    7) 这样就算安装完成了,余下的工作只是PHP代码编写问题了。
    关于 PHP 扩展的使用说明请参看代码中 phpext/README.md 文件或其它文档章节。


    本文固定链接:成事博客→Centos下安装sphinx,SCWS PHP插件


沪ICP备19023445号-2号
友情链接