avatar

麦兜的小站

MDO.INK

  • 首页
  • 随笔
  • 知识库
  • 归档
  • 动态
  • 标签
  • 关于
Home Laravel 中使用 PHP 分词库 (jieba) 和 (scws)
文章

Laravel 中使用 PHP 分词库 (jieba) 和 (scws)

Posted 2025-01-27 Updated 2025-01- 27
By power 已删除用户
12~16 min read

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:

  1. Jieba 分词库可以添加关键字,就是自定义词汇来作分词,有额外需求的可以看 GitHub
  2. 词汇的词性是在'src/dict/pos_tag_readable.txt'

2. SCWS 分词#

官方演示网站,scws4;#

这个分词库,个人感觉很快,而且不需要像 Jieba 那样需要内存那么,当时使用完,感觉还不错,我选择的是 PSCWS4,就是以 PHP 环境的,而没用 PHP 扩展,不支持 composer;#

1. 下载安装:

  1. pscws4
  2. 词典 (简体中文 - utf8)
  3. 将 pscws4 解压后方到 http/Help/scws 目录下 (新建)
  4. 将词典文件放到 public 目录下

2. 准备:

  1. 修改解压后的 pscws4 的 pscws4.class.php 文件名为 PSCWS4.php,把 require 文件改为 use AppHelpscwsXDB_R;
  2. 修改解压后的 pscws4 的 xdb_r.class.php 文件名为 XDB_R.php
  3. 给两个类文件添加命名空间 namespace AppHelpscws;

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 AppHttpControllers;

use AppHelpscwsPSCWS4;
use FukuballJiebaFinalseg;
use FukuballJiebaJieba;
use IlluminateHttpRequest;

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);
    }
} 
知识库
License:  CC BY 4.0
Share

Further Reading

Jul 31, 2025

如何实现接口幂等性

通俗的说,用户在系统中有操作,不管重复多少次,都应该产生一样的效果或返回一样的结果的。 幂等性的概念 幂等(Idempotent)是一个数学与计算机学的概念,常见于抽象代数中。 f(n)=1^n//无...

Jul 19, 2025

10个npm工具包

有了npm之后,前端人员真的是过上好日子了。我们可以直接把别人写好的工具包拿来用,非常的方便。 1.day.js-轻量日期处理 npminstalldayjs importdayjsfrom'd...

Jul 17, 2025

How to set up PHP7.4 on MacOS.

Thisisallverywellandgood.Apartfromonesmallinsignificantthing… TheversionofPHPinuseiscurrently7.4. Th...

OLDER

上传选择、拖拽、剪裁

NEWER

快让Appium自动化测试你的App吧

Recently Updated

  • 如何实现接口幂等性
  • 10个npm工具包
  • How to set up PHP7.4 on MacOS.
  • Automa:一键自动化,网页数据采集与工作流程优化专家Automa:解锁自动化
  • Mac 下用 brew 搭建 LNMP

Trending Tags

thinkphp clippings

Contents

©2025 麦兜的小站. Some rights reserved.

Using the Halo theme Chirpy