Laravel 中使用 PHP 分词库 (jieba) 和 (scws)
laravel 中使用 php 分词库 (jieba) 和 (scws)#
坚持开源,坚持分享
这篇文章旨在介绍我用过的两个 PHP 分词库以及他们的简单使用
- 目的:完成一段段落的分词
1.Jieba 分词库#
Jieba 分词库,GitHub 地址
安装:
composer require fukuball/jieba-php:dev-master
主要代码:
//这边要给内存,不然会炸
ini_set('memory_limit', '1024M');
//初始化
$this->jieba = new Jieba();
$this->finalseg = new Finalseg();
$this->jieba->init();
$this->finalseg->init();
//使用
$cut_array = $this->jieba->cut('分词字符串',false);
//分词后的结果是数组
notice:
- Jieba 分词库可以添加关键字,就是自定义词汇来作分词,有额外需求的可以看 GitHub
- 词汇的词性是在'src/dict/pos_tag_readable.txt'
2. SCWS 分词#
官方演示网站,scws4;#
这个分词库,个人感觉很快,而且不需要像 Jieba 那样需要内存那么,当时使用完,感觉还不错,我选择的是 PSCWS4,就是以 PHP 环境的,而没用 PHP 扩展,不支持 composer;#
1. 下载安装:
- pscws4
- 词典 (简体中文 - utf8)
- 将 pscws4 解压后方到 http/Help/scws 目录下 (新建)
- 将词典文件放到 public 目录下
2. 准备:
- 修改解压后的 pscws4 的 pscws4.class.php 文件名为 PSCWS4.php,把 require 文件改为 use App\Help\scws\XDB_R;
- 修改解压后的 pscws4 的 xdb_r.class.php 文件名为 XDB_R.php
- 给两个类文件添加命名空间 namespace App\Help\scws;
3. 编码测试
简要实现代码 (附录有完整代码)
//初始化 并设置utf8,设置词典路径和规则路径
$this->pscws = new PSCWS4('utf8');
$this->pscws->set_charset('utf-8');
$this->pscws->set_dict(public_path().'/dict.utf8.xdb');
$this->pscws->set_rule(public_path().'/rules.ini');
//使用:
$this->pscws->send_text("分词的字符串。。。");
while ($some = $this->pscws->get_result())
{
foreach ($some as $word)
{
$article[] = $word['word'];
}
}
4. 效果图
jieba 效果图:
pscws4 效果图
以上可以看出,jieba 对于一些英文标点符号没有很好的切割,例如 42 的 country;而 scws 对于每个标点符号都作了切割;对于我的需求来说,scws 是比较适合我的,如何选择看个人需求。
jieba
- 优点:能添加关键字;自定义词典
- 缺点:需要内存大,对于英文分词和标点符号支持不是很好
scws:
- 优点:词汇字典很大,有 28w,可以精细切割每个字符
- 缺点:无法自己扩展,貌似要钱
附录代码#
路由:web.php
Route::get('/scws', 'WordCutController@scwsCut');
Route::get('/jieba', 'WordCutController@jieBaCut');
控制器:WordCutController
<?php
namespace App\Http\Controllers;
use App\Help\scws\PSCWS4;
use Fukuball\Jieba\Finalseg;
use Fukuball\Jieba\Jieba;
use Illuminate\Http\Request;
class WordCutController extends Controller
{
public $pscws;
public $jieba;
public $finalseg;
/*
* pscws4分词 实例
*/
public function scwsCut(){
$this->pscws = new PSCWS4('utf8');
$this->pscws->set_charset('utf-8');
$this->pscws->set_dict(public_path().'/dict.utf8.xdb');
$this->pscws->set_rule(public_path().'/rules.ini');
//使用:
$this->pscws->send_text("Dragon Boat Festival is one the very classic traditional festivals, which has been celebrated since the old China. Firstly, it is to in honor of the great poet Qu Yuan, who jumped into the water and ended his life for loving the country. Nowadays, different places have different ways to celebrate.
端午节是一个非常经典的传统节日,自古以来就一直被人们所庆祝。首先,是为了纪念伟大的诗人屈原,屈原跳入水自杀,以此来表达了对这个国家的爱。如今,不同的地方有不同的庆祝方式。");
while ($some = $this->pscws->get_result())
{
foreach ($some as $word)
{
$article[] = $word['word'];
}
}
dd($article);
}
/*
* jieba分词 实例
*/
public function jieBaCut(){
ini_set('memory_limit', '1024M');
//初始化
$this->jieba = new Jieba();
$this->finalseg = new Finalseg();
$this->jieba->init();
$this->finalseg->init();
//使用
$cut_array = $this->jieba->cut('Dragon Boat Festival is one the very classic traditional festivals, which has been celebrated since the old China. Firstly, it is to in honor of the great poet Qu Yuan, who jumped into the water and ended his life for loving the country. Nowadays, different places have different ways to celebrate.
端午节是一个非常经典的传统节日,自古以来就一直被人们所庆祝。首先,是为了纪念伟大的诗人屈原,屈原跳入水自杀,以此来表达了对这个国家的爱。如今,不同的地方有不同的庆祝方式。',false);
dd($cut_array);
}
}
如果有不对或不足的,请大佬们指出来,毕竟我只是满足需求,并没有深入研究,谢谢各位大佬哦
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由系统于 6年前 自动加精
推荐文章: