xsplit PHP扩展

本文深入探讨了xsplit PHP扩展的功能、使用方法和安装步骤,包括分词、词典管理、文本分析等功能。提供了实用的示例代码和参数配置说明,适合开发者在项目中实现基于MMSEG算法的文本处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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为词频。
例子如下:
<?php
$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表示。返回值是一个数组,包含所有切分好的词语。如果不指定词典,最后一次打开的词典将被使用。
<?php
$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 : 按照词典返回所有词典的词频,并以数组形式返回每一个词语的详细统计。
如果不指定词典,最后一次打开的词典将被使用。
<?php
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)
计算汉明距离。
<?php
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";
资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值