ranvis/mecab 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ranvis/mecab

Composer 安装命令:

composer require ranvis/mecab

包简介

MeCab binding using FFI

关键字:

README 文档

README

MeCab binding using FFI.

License

BSD 2-Clause License

This library contains a part of MeCab interface definitions, which is licensed under BSD 3-Clause License as stated there.

Installation

First make sure you have enabled FFI extension, which is bundled with PHP. Then execute the Composer command:

composer require "ranvis/mecab:^0.3"

Make sure you have installed MeCab 0.996 (or later compatible version) on your system along with the dictionary. On Windows and some Linux distros, there should be a pre-built package.

Last but not least, if you are going to use the library from a non-CLI environment such as a web server, ffi.enable=true instead of the restricted default ffi.enable=preload must be set in the system INI configuration used by the SAPI.

Example Usage

use Ranvis\MeCab;

require_once __DIR__ . '/vendor/autoload.php';

$mecab = new MeCab\Env(); // libmecab.{so,dll} should be in the PATH directory
//$mecab = new MeCab\Env('libmecab.so.2'); // or libmecab.so.2 in it
//$mecab = new MeCab\Env('/usr/lib64/libmecab.so.2'); // or specify explicitly
var_dump($mecab->getVersion());

$tagger = $mecab->tagger();
//$tagger = $mecab->tagger(['--rcfile', '/path/to/mecabrc']);

foreach ($tagger->getDictionaryInfo() as $info) {
    $name = $info->getFileName();
    $name = substr($name, strlen(dirname($name, 2)));
    printf("Dictionary: %s, Version: %d, Encoding: %s\n", $name, $info->getVersion(), $info->getCharset());
}

$headNode = $tagger->parseToNode("メカブはおやつに入りますか?");
foreach ($headNode as $node) {
    echo $node->surface() . ": " . $node->feature() . "\n";
}

Preloading Dynamic Library

Using scripts and FFI to load libraries incurs a small overhead compared to native PHP extensions. This can be mitigated by using FFI's preload feature. With preloading, daemon-like SAPI such as FPM can preprocess initialization of the library and reuse it afterwards.

Preload using ffi.preload

To make use of this, we need to generate a header file for MeCab once. Run bundled gen_mecab_preloader command with the destination (and MeCab library name/path).

$ mkdir ffi_preload.d
$ vendor/bin/gen_mecab_preloader ffi_preload.d/mecab.h /path/to/libmecab.so
Generated: ffi_preload.d/mecab.h

Then set ffi.preload ini value to point to the file. (The header file may have to be regenerated in case this library is largely updated.)

Now to see if it works, we use CLI to run the following script. Notice that MeCab\Env is now instantiated with the MeCab\Env::fromScope() static method instead of a new operator, to take advantage of preloading.

$ cat <<'END' > preload_test.php
<?php

require_once __DIR__ . '/vendor/autoload.php';

use Ranvis\MeCab;

$mecab = MeCab\Env::fromScope();
var_dump($mecab->getVersion());
END
$ php -d ffi.preload=ffi_preload.d/*.h preload_test.php
string(5) "0.996"

If your PHP is running in a daemon-like style like FPM, restart the daemon process to take effect.

Preload using opcache.preload

Note: OPcache's preloading is not supported in Windows. (gh#4999)

Another way of preloading is to use OPcache's one. OPcache also has a feature to preload classes that you use often. FFI can be initialized during this step as well, provided that opcache.preload_user is not set, or set as the current system user on PHP 8.3 and later. (Usually it is not set.)

In the PHP script specified in opcache.preload ini value, call MeCab\Env::preload() as follows:

<?php // preloader.php

require_once __DIR__ . '/vendor/autoload.php';

\Ranvis\MeCab\Env::preload('/path/to/libmecab.so');

And then in the actual script, call MeCab\Env::fromScope() to instantiate like the former example.

$ php -d opcache.preload=preloader.php preload_test.php

While this looks simpler than the former way, a header file will be silently created on the system's temporary directory everytime OPcache's preloading triggers; since FFI doesn't allow in-memory interface definitions for preloading as of PHP 8.3.

ranvis/mecab 适用场景与选型建议

ranvis/mecab 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 804.97k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「text」 「mecab」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ranvis/mecab 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ranvis/mecab 我们能提供哪些服务?
定制开发 / 二次开发

基于 ranvis/mecab 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 804.97k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2020-12-17