php mmseg,xsplit A PHP extension for Chinese segmentation using MMSEG algorithm[zz]
这个project大约是09年初开始做的,很快就形成了现在版本的样子,后陆续做了一些小修改和修复了一些小bug,现整理了一下决定发布。把它open source的主要原因,就是希望这个小工具能对大家有用,并且能够参与进来一起把它做的更好
20101025
Change Log
20130509 0.0.9 released fixed an issue when compiler uses strict type checking
20101108 0.0.8 beta,加入xs_simhash和xs_hdist函数,分别计算simhash和汉明距离。
关于xsplit的交流,请到xsplit贴吧: http://tieba.baidu.com/f?kw=xsplit 参与讨论。
xsplit是一个PHP扩展,提供基于MMSEG算法的分词功能。目前只在linux下测试并部署过,希望有朋友可以帮忙编译提供windows下的dll。
xsplit只处理UTF8编码格式,如果是其他编码格式,请在使用前自行转换
xsplit主要有以下几个函数:
bool xs_build ( array $words, string $dict_file )
resource xs_open (string $dict_file [, bool $persistent])
array xs_split ( string $text [, int $split_method = 1 [, resource $dictionary_identifier ] ] )
mixed xs_search ( string $text [, int $search_method [, resource $dictionary_identifer ] ] )
string xs_simhash( string $text [, bool $isBinary] )
int xs_hdist( string $simhash1, string $simhash2 )
安装过程与一般的PHP扩展安装一样
$phpize
$./configure --with-php-config=/path/to/php-config
$make
$make install
php.ini中可以设置以下参数:
xsplit.allow_persisten = On
xsplit.max_dicts = 5
xsplit.max_persistent = 3
xsplit.default_dict_file = /home/xdict
xsplit.allow_persistent 是否允许加载持久词典
xsplit.max_dicts 允许同时打开的最大词典数目
xsplit.max_persistent 允许同时打开的最大持久词典数目
xsplit.default_dict_file 默认的词典,没有指定词典时会调用此词典
源码中有一个utils目录,包含
make_dict.php 提供命令行方式创建词典
xsplit.php 一个简单的示例文件
xdict_example.txt 一个文本词库的格式示例
make_dict.php的使用例子如下:
$php make_dict.php ./xdict_example.txt ./xdict.db
文本词库的格式请参考xdict_example.txt
bool xs_build (array $words, string $dict_file)
从$words数组建立名称为$dict_file的词典,若成功则返回true。$words数组的格式请参考示例,key为词语,value为词频。
例子如下:
$dict_file='dict.db';
$dwords['美丽']=100;
$dwords['蝴蝶']=100;
$dwords['永远']=100;
$dwords['心中']=100;
$dwords['翩翩']=100;
$dwords['飞舞']=100;
$dwords['翩翩飞舞']=10;
if(!xs_build($dwords, $dict_file)) {
die('建立词典失败!');
}
resource xs_open (string $dict_file [, bool $persistent])
打开一个词典文件,并返回一个resource类型的identifier。$persistent可以指定是否是持久化词典,持久化词典在这里可以理解为词典资源生命周期的不同,一般情况下$persistent=true或者默认缺省即可。在进行分词的时候,可以指定不同的词典。
$dict_file_1 = 'xdcit.db';
$dict_file_2 = 'mydict.db';
$dict1 = xs_open($dict_file);
xs_open($dict_file);
array xs_split ( string $text [, int $split_method = 1 [, resource $dictionary_identifier ] ] )
对文本进行分词,可以指定分词方法和词典。分词方法目前有两种,一个是MMSEG算法(默认),一个是正向最大匹配,分别用常量XS_SPLIT_MMSEG和XS_SPLIT_MMFWD表示。返回值是一个数组,包含所有切分好的词语。如果不指定词典,最后一次打开的词典将被使用。
$text="那只美丽的蝴蝶永远在我心中翩翩飞舞着。";
$dict_file = 'xdict.db';
$dict_res = xs_open($dict_file);
$words = xs_split($text); /* 此处没有指定词典资源,默认使用最后一次打开的词典 */
$words1 = xs_split($text, XS_SPLIT_MMSEG, $dict_res);
mixed xs_search ( string $text [, int $search_method [, $dictionary_identifer ] ] ) 基于双数组trie树提供的一些功能,$search_method有四个常量表示:
XS_SEARCH_CP : darts的commonPrefixSearch封装,如果没有找到,返回false。
XS_SEARCH_EM : darts的exactMatchSearch封装,如果没有找到,返回false。
XS_SEARCH_ALL_SIMPLE : 按照词典返回所有词语词频总和,一个INT型数值。
XS_SEARCH_ALL_DETAIL : 按照词典返回所有词典的词频,并以数组形式返回每一个词语的详细统计。
如果不指定词典,最后一次打开的词典将被使用。
xs_open($dict_file);
$text="那只美丽的蝴蝶永远在我心中翩翩飞舞着。";
$word='翩翩飞舞';
$result=xs_search($word, XS_SEARCH_CP); /* common prefix search */
var_dump($result);
$result=xs_search($word, XS_SEARCH_EM); /* exact match search */
var_dump($result);
$result=xs_search($text, XS_SEARCH_ALL_SIMPLE);
var_dump($result);
$result=xs_search($text, XS_SEARCH_ALL_DETAIL);
var_dump($result);
string xs_simhash( array $tokens [, bool $rawoutput] )
计算simhash。这里所有token权重都是1,$tokens的例子如array('在', '这个', '世界')。$rawoput默认为0,即返回simhash的hex string形式,如md5, sha1函数一样;如过$rawoput为真,返回一个8字节的字符串,这个字符串实际上是一个64 bits的整型数,uint64_t,在一些特殊情况下可以用到。
int xs_hdist( string $simhash1, $string $simhash2)
计算汉明距离。
xs_open('xdict');
$text1="那只美丽的蝴蝶永远在我心中翩翩飞舞着。";
$text2="那只美丽的蝴蝶永远在我心中翩翩飞舞。";
$tokens1=xs_search($text1, XS_SEARCH_ALL_INDICT); /* 去掉标点等特殊符号,经过实验,计算simhash时,一些标点、换行、特殊符号等对效果影响较大 */
$tokens2=xs_search($text2, XS_SEARCH_ALL_INDICT);
$simhash1=xs_simhash($tokens1);
$simhash2=xs_simhash($tokens2);
echo "simhash1 is {$simhash1}\n";
echo "simhash2 is {$simhash2}\n";
$hamming_dist=xs_hdist($simhash1, $simhash2);
echo "bit-wise format:\n";
echo decbin(hexdec($simhash1)), "\n";
echo decbin(hexdec($simhash2)), "\n";
echo "hamming distance is {$hamming_dist}\n";
Terms - Privacy
php mmseg,xsplit A PHP extension for Chinese segmentation using MMSEG algorithm[zz]相关推荐
- Chinese Segmentation Introduction
1. Chinese Segmentation Introduction 最近两天系统的研究了一下中文分词算法,在这里做个简单的总结. 中文分词可以分为(1)基于词典的分词 和(2)非基于词典的分词. ...
- 【技术类】【ArcGIS对国产卫星的支持2:高分一号卫星】篇1、ArcGIS RasterType Extension for Chinese Satellites 2.0插件介绍
为了更好的服务于国产卫星用户,中国卫星插件推出2.0版本,增加了对高分一号卫星的支持. 1.插件版本说明及获取方式: 适用于ArcGIS 10.2或更高版本. 如需插件,请通过邮箱获取:may@esr ...
- 中文维基百科文本数据获取与预处理
照例,先讲下环境,Mac OSX 10.11.2 ,Python 3.4.3. 下载数据 方法1:使用官方dump的xml数据 最新打包的中文文档下载地址是:https://dumps.wikimed ...
- xsplit PHP扩展
xsplit是一个PHP扩展,提供基于MMSEG算法的分词功能.目前只在linux下测试并部署过,希望有朋友可以帮忙编译提供windows下的dll. xsplit只处理UTF8编码格式,如果是其他编 ...
- 【mmdeploy】mmseg转ONNX/TensorRT,附推理代码
目录 1.关于mmdeploy 2.环境安装 2.1预编译安装(Linux-x86_64, CUDA 11.x, TensorRT 8.2.3.0): 2.2预编译安装(Linux-x86_64, C ...
- mmseg底层代码分析及修改
本人采用voc格式数据集对mmseg底层代码进行分析,本文适合对mmseg使用流程比较熟悉的同学食用 数据增广部分代码分析 1.数据增广使用: mmsegmentation/tools/convert ...
- Mmseg分词算法及实现
一.分词方法 关于中文分词 参考之前写的jieba分词源码分析 jieba中文分词. 中文分词算法大概分为两大类: 一是基于字符串匹配,即扫描字符串,使用如正向/逆向最大匹配,最小切分等策略(俗称基于 ...
- 吉林大学计算机学院刘衍衍教授,刘磊-吉林大学计算机科学与技术学院
学术论文: 在<Journal of Systems and Software>.<Pattern Recognition>.<计算机学报>.<软件学报> ...
- 吕帅 计算机学院,刘磊-吉林大学计算机科学与技术学院
在<Journal of Systems and Software>.<Pattern Recognition>.<计算机学报>.<软件学报>等国内外期 ...
最新文章
- Python中numpy数组的拼接、合并
- CNN tflearn处理mnist图像识别代码解说——conv_2d参数解释,整个网络的训练,主要就是为了学那个卷积核啊。...
- Tomcat启动报错 Could not reserve enough space for object heap
- linux磁盘格式化
- gRPC入门教程汇总
- SQLite基础知识学习
- JAVA day27,28 线程池
- bzoj 1642: [Usaco2007 Nov]Milking Time 挤奶时间(DP)
- MATLAB卷积conv、conv2、convn详解
- 100个最常用的PHP函数(建议收藏)
- java计算机毕业设计小学教师课程管理系统源码+系统+数据库+lw文档
- @Resource注解用法
- EpiQuik病毒RNA提取试剂盒,快速、简单、经济有效
- Dear小弟×××,给你们的一封信「社区运营入门系列 序」
- 沈阳大学博客网站的设计与实现
- 图片去除下间隙(图片3像素)的方法
- 笔记本计算机内部部件图片,笔记本内部硬件构造图
- 【正则】1302- 快速上手常用正则表达式
- Wavelets: Seeing the forest and the trees
- System.Web.Services.Protocols.SoapException: 服务器无法处理请求
热门文章
- 【CCF】——中间数(详细分析)
- java 拆箱与装箱原理
- 微信内置浏览器缓存清理及关闭
- SharePreference保存list
- 【Microsoft Azure 的1024种玩法】二十七. Azure Virtual Desktop虚拟桌面之快速创建配置(一)
- 施工员证需要什么条件才能考?施工员证报名条件及费用?
- 关于富顺豆花及其经营的调查报告
- 量化交易 米筐 单因子回测框架(因子方向性确定)
- posix_qui-master 服务器端QuicAccept()函数
- SSH企业案例_CRM客户管理系统(一):SVNCRM注册页面