源码地址:http://scws.tguanlim.com/
<?php /*-----------------------------------------------------------------------*/ PHP-简易中文分词(SCWS)ver3.1/2.1(实例演示)
(*)基于词频词典逐点搜索最长词,双向根据词频取较高之分法
----------------------------------------------------------------------- 作者:马明练(hightman)(MSN:MingL_Mar@msn.com)(php-QQ群:17708754) 网站:http://php.twomice.net 时间:2006/03/05 目的:学习研究交流用,希望有好的建议及用途希望能进一步交流. ----------------------------------------------------------------------- 运行环境:PHP4.x.*(x>0)(编译需要--enable-dba--with-[cdb|gdbm]) /*-----------------------------------------------------------------------*/
/** *查看源码的参数<*.php?source> */ if(isset($_SERVER['QUERY_STRING']) &&!strcasecmp($_SERVER['QUERY_STRING'],'source')) { highlight_file(__FILE__); exit(0); }
/** *实例进行 */
//尝试计算实列运算时间 functionget_microtime() { list($usec,$sec)=explode('',microtime()); return((float)$usec+(float)$sec); } $time_start=get_microtime();
//分词结果之回调函数(param:分好的词组成的数组) functionwords_cb($ar) { echoimplode('',$ar); flush(); }
//实例化前的参数指定与读取 $dict='dict/dict.cdb'; $mydata=NULL;//待切数据 $version=3;//采用版本 $autodis=false;//是否识别名字 $ignore=false;//是否忽略标点 $debug=false;//是否为除错模式 $is_cli=(php_sapi_name()=='cli');//是否为cli运行环境
//根据不同版本的环境读取参数设置 if($is_cli) { $argc=$_SERVER['argc']; for($i=1;$i<$argc;$i++) { $optarg=$_SERVER['argv'][$i]; if(!strncmp($optarg,"--",2)) { $cmp=substr($optarg,2); if(!strcasecmp($cmp,"help")) { $mydata=NULL; break; } elseif(!strcasecmp($cmp,"autodis")) $autodis=true; elseif(!strcasecmp($cmp,"ignore")) $ignore=true; elseif(!strcasecmp($cmp,"v2")) $version=2; elseif(!strcasecmp($cmp,"debug")) $debug=true; elseif(!strcasecmp($cmp,"dict")) { $i++; $dict=$_SERVER['argv'][$i]; } } elseif(is_null($mydata)) $mydata=trim($optarg);
if(!is_null($mydata)&&is_file($mydata)) $mydata=@file_get_contents($mydata); } } else { //部分参数选项 $checked_ignore=$checked_autodis=$checked_v2='';
//是否指定有第2版 if(isset($_REQUEST['version'])&&$_REQUEST['version']==2) { $version=2; $checked_v2='selected'; }
//是否指定一个词典格式 $selected_gdbm=$selected_text=$selected_sqlite=''; if(isset($_REQUEST['dict'])) { if($_REQUEST['dict']=='gdbm') { $dict='dict/dict.gdbm'; $selected_gdbm='selected'; } elseif($_REQUEST['dict']=='text') { $dict='dict/cwords.txt'; $selected_text='selected'; } elseif($_REQUEST['dict']=='sqlite') { $dict='dict/dict.sqlite'; $selected_sqlite='selected'; } else { $_REQUEST['dict']='cdb'; } }
//是否开启人名识别(缺省关闭) if(isset($_REQUEST['autodis'])&&!strcmp($_REQUEST['autodis'],'yes')) { $autodis=true; $checked_autodis='checked'; }
//是否清除标点符号 if(isset($_REQUEST['ignore'])&&!strcmp($_REQUEST['ignore'],'yes')) { $ignore=true; $checked_ignore='checked'; }
//是否开启debug if(isset($_REQUEST['debug'])&&!strcmp($_REQUEST['debug'],'yes')) { $debug=true; $checked_debug='checked'; }
//切分数据 if(!isset($_REQUEST['mydata'])||empty($_REQUEST['mydata'])) { $mydata=@file_get_contents('sample.txt'); } else { $mydata=&$_REQUEST['mydata']; if(get_magic_quotes_gpc()) $mydata=stripslashes($mydata); } }
//清除最后的/r/n/t if(!is_null($mydata)) $mydata=trim($mydata);
//实例化分词对像(mydata非空) $object='my_SimpledCWS'.$version; require($object.'.class.php');
$cws=new$object($dict); $cws->set_ignore_mark($ignore); $cws->set_autodis($autodis); $cws->set_debug($debug);
?> <?phpif(!$is_cli){?> <html> <head> <title>PHP简易中文分词(SCWS)第<?phpecho$version;?>版在线演示(byhightman)</title> <metahttp-equiv="Content-type"content="text/html;charset=gbk"> <styletype="text/css"> <!-- td,body{background-color:#efefef;font-family:tahoma;font-size:14px;} .demotx{font-size:12px;width:100%;height:140px;} small{font-size:12px;} //--> </style> </head> <body> <h3> <fontcolor=red>PHP简易中文分词(SCWS)</font> <fontcolor=blue>第<?phpecho$version;?>版</font>-在线演示(byhightman) </h3> 基本功能:根据词频词典进行机械分词、中外人名智能辨认(词典格式:gdbm/cdb等) <hr/>
<tablewidth=100%border=0> <tr> <formmethod=post> <tdwidth=100%> <strong>请输入文字点击提交尝试分词:</strong><br/> <textareaname=mydatacols=60rows=8class=demotx><?phpecho$mydata;?></textarea> <small> <inputtype=checkboxname=autodisvalue="yes"<?phpecho$checked_autodis;?>>智能识别人名 <inputtype=checkboxname=ignorevalue="yes"<?phpecho$checked_ignore;?>>清除标点符号 <inputtype=checkboxname=debugvalue="yes"<?phpecho$checked_debug;?>>debug(v2) <br/> 词典格式: <selectname=dictsize=1> <optionvalue=cdb>CDB</option> <optionvalue=gdbm<?phpecho$selected_gdbm;?>>GDBM</option> <optionvalue=text<?phpecho$selected_text;?>>Text</option> <optionvalue=sqlite<?phpecho$selected_sqlite;?>>SQLite2.x</option> </select> 尝试采用第 <selectname=versionsize=1style="color:red;font-weight:bold;"> <optionvalue=3>3</option> <optionvalue=2<?phpecho$checked_v2;?>>2</option> </select> 版分词算法 </small> <inputtype=submit> </td> </form> </tr> <tr> <td><hr/></td> </tr> <tr> <tdwidth=100%> <strong>分词结果(原文总长度<?phpechostrlen($mydata);?>字符)</strong> <br/> <textareacols=60rows=8class=demotxreadonly> <?php}else{?> _____________________________________________________________________
PHP简易中文分词程序(SCWS)-第<?phpecho$version;?>版-byhightman _____________________________________________________________________ 1.基于词频词典逐点搜索最长词,双向根据词频取较高之分法 2.用法:<?phpecho$_SERVER['argv'][0];?>[选项]<string|file> 3.选项:--autodis打开人名识别 --ignore清除结果中的标点符号 --v2使用第2版分词算法(缺省第3版) --dict<file>直接指定词典文件,后缀(.cdb|.gdbm|.txt|.sqlite) --help显示本页帮助文件 4.结果:直接输出分词结果,词之间以空格分隔 _____________________________________________________________________
<?php}?> <?php //执行切分,分词结果数组执行words_cb() $cws->segment($mydata,'words_cb');
//切分时间统计 $time_end=get_microtime(); $time=$time_end-$time_start;
//以下显示结果 ?> <?phpif(!$is_cli){?> </textarea> <small> 分词耗时:<?phpecho$time;?>秒, 词典查询次数:<?phpecho$cws->_dict->query_times;?>次 </small> </td> </tr> </table> <hr/> <small> 注:本程序代码及相关词典免费开放下载,供研究学习交流. 参见网页<ahref=http://php.twomice.nettarget=_blank>http://php.twomice.net</a> 或直接<ahref="?source"target="_blank">查看源码</a> </small> </body> </html> <?php}elseif(!empty($mydata)){?>
_____________________________________________________________________ 总长:<?phpechostrlen($mydata);?>字符,耗时:<?phpecho$time;?>秒,查词次数:<?phpecho$cws->_dict->query_times;?>次 <?php}?> (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|