[文章/程序 作者:张宴 本文版本:v1.3 最后修改:2009.07.06 转载请注明原文链接:http://blog.zyan.cc/phpcws_v100/]

  注:最新的分词系统 HTTPCWS 已经发布,用来取代 PHPCWS。

  请点击以下网址下载 HTTPCWS:


  http://code.google.com/p/httpcws

  原来的 PHPCWS 停止更新。




  名称:PHPCWS(PHP中文分词扩展)
  协议:New BSD License
  作者:张宴
  网址:http://code.google.com/p/phpcws/
  SVN:http://code.google.com/p/phpcws/source/browse/#svn/trunk/phpcws

  一、PHPCWS 简介

  1、什么是 PHPCWS ?
  PHPCWS 是一款开源的PHP中文分词扩展,目前仅支持Linux/Unix系统。

  PHPCWS 先使用“ICTCLAS 3.0 共享版中文分词算法”的API进行初次分词处理,再使用自行编写的“逆向最大匹配算法”对分词和进行词语合并处理,并增加标点符号过滤功能,得出分词结果。

  ICTCLAS(Institute of Computing Technology, Chinese Lexical Analysis System)是中国科学院计算技术研究所在多年研究工作积累的基础上,基于多层隐马模型研制出的汉语词法分析系统,主要功能包括中文分词;词性标注;命名实体识别;新词识别;同时支持用户词典。ICTCLAS经过五年精心打造,内核升级6次,目前已经升级到了ICTCLAS3.0,分词精度98.45%,各种词典数据压缩后不到3M。ICTCLAS在国内973专家组组织的评测中活动获得了第一名,在第一届国际中文处理研究机构SigHan组织的评测中都获得了多项第一名,是当前世界上最好的汉语词法分析器。

  ICTCLAS 3.0 商业版是收费的,而免费提供的 ICTCLAS 3.0 共享版不开源,词库是根据人民日报一个月的语料得出的,很多词语不存在。所以本人对ICTCLAS分词后的结果,再采用逆向最大匹配算法,根据自己补充的一个9万条词语的自定义词库(与ICTCLAS词库中的词语不重复),对ICTCLAS分词结果进行合并处理,输出最终分词结果。

  由于 ICTCLAS 3.0 共享版只支持GBK编码,因此,如果是UTF-8编码的字符串,可以先用PHP的iconv函数转换成GBK编码,再用phpcws_split函数进行分词处理,最后转换回UTF-8编码。

  2、PHPCWS 中文分词在线演示

  演示网址:http://blog.zyan.cc/demo/phpcws/

  3、PHPCWS 分词速度及用途

  初次使用时,Apache 或 php-cgi(FastCGI) 进程,需要加载一次词库到内存中,需要0.0X秒。58字节的一句话——“2009年2月13日,我编写了一款PHP中文分词扩展:PHPCWS 1.0.0。”,分词速度只需0.0003秒。

  PHPCWS 属于《亿级数据的高并发通用搜索引擎架构设计》的一部分,用作“搜索查询接口”的关键字分词处理。在此架构中,Sphinx索引引擎对于CJK(中日韩)语言支持一元切分,假设【反恐行动是国产主视角射击网络游戏】这段文字,Sphinx会将其切成【反 恐 行 动 是 国 产 主 视 角 射 击 网 络 游 戏】,然后对每个字建立反向索引。如果用这句话中包含的字组成一个不存在的词语,例如【恐动】,也会被搜索到,所以搜索时,需要加引号,例如搜索【"反恐行动"】,就能完全匹配连在一起的四个字,不连续的【"恐动"】就不会被搜索到。但是,这样还有一个问题,搜索【"反恐行动游戏"】或【"国产网络游戏"】就会搜索不到。所以,我在搜索层写了个PHP中文分词扩展,搜索“反恐行动游戏”、“国产网络游戏”,会被PHPCWS中文分词函数分别切分为“反恐行动 游戏”、“国产 网络游戏”,这时候,用PHP函数给以空格分隔的词语加上引号,去搜索【"反恐行动" "游戏"】或【"国产" "网络游戏"】,就能搜索到这条记录了。由于PHPCWS位于搜索层,中文分词词库发生增、删、改,只需平滑重启一次Web服务器或php-cgi进程即可,无需重建搜索索引。

  根据上述情况,对于那些采用二元交叉切分的搜索引擎,PHPCWS用在前端搜索层对用户输入的搜索关键字、短语进行分词处理,同样适合。PHPCWS开发的目的正在于此,对于短句、小文本中文分词切分,速度非常之快。

  4、自定义词库

  自定义词库名称为userdict.tch,格式为 Tokyo Cabinet DBM 的 Abstract key-value 内存哈希数据库(key为GBK编码的词语名词,value为词频。目前词频均填1,暂时用不上)。自定义词库的修改在安装步骤中会详细介绍。



  二、phpcws 1.0.1 安装步骤

  1、安装 Tokyo Cabinet 数据库:
wget http://tokyocabinet.sourceforge.net/tokyocabinet-1.4.17.tar.gz
tar zxvf tokyocabinet-1.4.17.tar.gz
cd tokyocabinet-1.4.17/
./configure --prefix=/usr/local/tokyocabinet-1.4.17
make && make install
mkdir -p /usr/local/tokyocabinet-1.4.17/lib/static/
cp -f /usr/local/tokyocabinet-1.4.17/lib/*.a /usr/local/tokyocabinet-1.4.17/lib/static/
echo "/usr/local/tokyocabinet-1.4.17/lib" >> /etc/ld.so.conf
/sbin/ldconfig
cd ../


  2、安装 PHPCWS 扩展:
wget http://phpcws.googlecode.com/files/phpcws-1.0.1.tar.gz
tar zxvf phpcws-1.0.1.tar.gz
cd phpcws-1.0.1/
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-tc=/usr/local/tokyocabinet-1.4.17
make && make install
cd ../

  注:其中/usr/local/webserver/php/为您的PHP安装目录。

  3、安装 ICTCLAS 3.0 词库和 PHPCWS 自定义词库(词库汉字编码均为GBK):
cd /usr/local/
wget http://phpcws.googlecode.com/files/phpcws-dict-1.0.0.tar.gz
tar zxvf phpcws-dict-1.0.0.tar.gz
mv phpcws-dict-1.0.0 phpcws


  您可以往自定义词库内增加自己的词语,方法如下:
cd /usr/local/phpcws/

#如果userdict.tch已经被PHP加载,请拷贝一个备份
cp userdict.tch userdict_new.tch

#添加新词语
/usr/local/tokyocabinet-1.4.11/bin/tcamgr put userdict_new.tch 词语一 1
/usr/local/tokyocabinet-1.4.11/bin/tcamgr put userdict_new.tch 词语二 1

查询某个词语是否已经在词库内
/usr/local/tokyocabinet-1.4.11/bin/tcamgr get userdict_new.tch 词语名词

#删除一个词语
/usr/local/tokyocabinet-1.4.11/bin/tcamgr out userdict_new.tch 词语名称

#将修改后的新词库覆盖回原词库
mv userdict_new.tch userdict.tch

#重启您的Apache服务器,或php-cgi(FastCGI)进程。
#例如重启php-cgi(FastCGI)进程:
/usr/local/webserver/php/sbin/php reload

  您可以从搜狗细胞词库网站下载自己需要的行业分类文本词库,整合到PHPCWS自定义词库中。

  4、修改您的php.ini文件,增加以下几行内容:
[phpcws]
extension = "phpcws.so"
phpcws.dict_path = "/usr/local/phpcws"


  5、重启您的Apache服务器,或php-cgi(FastCGI)进程

  6、查看您的phpinfo信息,如果搜索到以下内容,则表明安装成功:

  点击在新窗口中浏览此图片



  三、phpcws 1.0.1 调用方式

  1、PHPCWS扩展拥有一个PHP函数:
  string phpcws_split ( string $text [, string $interpunction ] )

  参数说明:
  $text 为GBK编码的文本内容;

  $interpunction 为可选参数,用来控制是否过滤标点符号。
  ①、值为空时,表示不过滤;

  ②、值为"default"时,过滤掉默认的标点符号,即以下标点符号:
  ,.!!"#¥%&`'()〔〕〈〉《》「」『』〖〗【】.*+,-.。、?…—·ˉˇ¨‘’“”々~‖∶"/:;|〃<=>?@[\]^_`{|} ̄

  ③、值为其他字符串时,表示过滤掉自定义的标点符号,例如值为",.!,。!"即过滤掉半角、全角的逗号、句号、感叹号。

  2、PHP中文分词实例:

  ①、对GBK编码的字符串进行中文分词处理(example_gbk.php):

  ②、对UTF-8编码的字符串进行中文分词处理(example_utf8.php):

  输出结果为:
分词结果为:
2009年 2月 13日 , 我 编写 了 一款 PHP 中文 分词 扩展 : PHPCWS 1.0.0 。

过滤掉默认标点符号的分词结果为:
2009年 2月 13日 我 编写 了 一款 PHP 中文 分词 扩展 PHPCWS 1.0.0

过滤掉全角逗号与冒号的分词结果为:
2009年 2月 13日 我 编写 了 一款 PHP 中文 分词 扩展 PHPCWS 1.0.0 。


  以上两个实例PHP程序文件可以从SVN中获取:







技术大类 » 搜索引擎技术 | 评论(116) | 引用(0) | 阅读(125731)
xujie123
2012-5-21 11:29
Our online store offers you discounted Designer louis vuitton replica wallet at present. You could find them in desirable quality and price. If you don't mind high class louis vuitton uk, have a good time here.louis vuitton Store Online Handbags can also bring great accuracy as well as practical applicability and fashionable.Have you ever dreamed of being as charming as Madonna? Have you ever thought of becoming an envy of all your friends? If so, come to louis vuitton outlet.<br/>
xujie555
2012-5-21 13:56
coach factory outlet uses graceful accessories to match the classical logo of coach, which is the best combination of coach. The handmade coach products?can make you more charming and graceful.coach factory online provides people many coach goods. If you wish to snatch the coach handbag, then this best method is made for that you like for coach discount.coach factory outlet online is a fashion brand to ensure its quality. With designer coach shoulder bags, you will always attract people's attention. The bags will emphasize your personal style and taste.<br/>
xujie333
2012-5-21 15:20
coach outlet online Store guarantee that all the coach handbags offered are own high quality. In addition , all of them are sold at an unexpected low price.If you want to purchase, just visit their website.coach factory outlet is really sizzling kinds of shopping way for you. With the usage of the replica designer coach bags, you can surely be able to change your individual looks in a stunning manner.Coach bags on sale from the coach outlet are cheap or discount prices that you certainly will stand out from the crowd on your next camping trip!<br/>
GAWERW
2013-3-7 14:34
买轴承就到:www.jkzhoucheng.cn
phpman Homepage
2013-3-30 15:14
http://phpman.cn/so 中文分词
与飞
2013-4-8 13:02
安装成功,phpinfo打印的信息正确,phpcws_split()返回空,请帮忙看看是什么问题?phpcws-1.0.1 tokyocabinet-1.4.48centos 5.5
45
2013-4-8 17:46
,.<>
萧雨 Email
2013-4-24 15:21
我已经安装了,查看phpinfo已经有你截图显示信息,但是我复制你的代码测试,输出的为空?不知道是什么原因,他还需要别的支持吗?我的系统是centos5.5  使用环境是lnmp
磨延城 Email Homepage
2013-10-21 21:06
磨途歌学习了
玩家 Email
2014-3-1 11:32
为什么我的分词只输出单个字
下面是我的代码
$test="你好哲理是世界国家地区的方向,路人甲路人乙还有喜好小孩";
    
    require dirname(__FILE__).'/pscws4/pscws4.class.php';
    $pscws=new PSCWS4();
    
    $pscws->set_dict('/var/www/dict.xdb');
    
    $pscws->set_rule(dirname(__FILE__).'/pscws4/etc/rules.utf8.ini');
    $pscws->set_charset('utf8');
   // $pscws->set_ignore(true);
    $pscws->send_text($test);
  
    while($some=$pscws->get_result())
    {
         foreach($some as $word)
         {
         echo "<pre>";
         print_r($word);
         echo "</pre>";
         }
        
    }
    
    $pscws->close();
过客 Email
2014-11-27 23:58
root@ydcc:/usr/local/etc # cd httpcws-1.0.0-i386-bin/root@ydcc:/usr/local/etc/httpcws-1.0.0-i386-bin # /usr/local/etc/httpcws-1.0.0-i386-bin/httpcws -d -x /usr/local/etc/httpcws-1.0.0-i386-bin/dict/ELF binary type "0" not known./usr/local/etc/httpcws-1.0.0-i386-bin/httpcws: Exec format error. Binary file not executable.root@ydcc:/usr/local/etc/httpcws-1.0.0-i386-bin #
Steven Email Homepage
2015-3-19 02:57
Thank you for this post Weight Loss Tea
pikachu chu Email
2018-12-4 18:13
The tutorial you shared is great and useful to me. I need such information.  happy wheelshappy wheels
Entrepreneurship Essay Writing Services Email Homepage
2020-9-21 19:10
Entrepreneurship research writing services are essential for entrepreneurship coursework writing service students and Entrepreneurship Essay Writing Services seekers.
토토사이트 Email Homepage
2022-12-5 15:29
I came here because my friends recommended me how good this article is. There is priceless information that I have found. I will also recommend 토토사이트추천  to my relationship.
먹튀신고 Email Homepage
2023-11-16 08:13
We really appreciate your blog post. After visiting your post, you will find many methods. I'm looking for it. Thanks for posts like this, please keep it up. Well done.먹튀신고
分页: 6/6 第一页 上页 1 2 3 4 5 6 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]