Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
J

jieba-php

> 编程语言
Open source

"結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件。 / "Jieba" (Chinese for "to stutter") Chinese text segmentation: built to be the best PHP Chinese word segmentation module.

1.4K stars0 likes0 views
WebsiteGitHub

About

"結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件。 / "Jieba" (Chinese for "to stutter") Chinese text segmentation: built to be the best PHP Chinese word segmentation module.

jieba-php

"結巴"中文分詞:做最好的 PHP 中文分詞、中文斷詞組件,原始版本翻譯自 fxsjy/jieba,目前已經為一個獨立分支,請有興趣的開發者一起加入開發!若想使用 Python 版本請前往 fxsjy/jieba

現在已經可以支援繁體中文!只要將字典切換為 big 模式即可!

中文斷詞目前使用 LLM 大語言模型會得到更好的斷詞結果,但如果要快速、便宜,這個套件仍然有其用處。

"Jieba" (Chinese for "to stutter") Chinese text segmentation: built to be the best PHP Chinese word segmentation module.

Scroll down for English documentation.

線上展示

  • 網站原始碼:https://github.com/fukuball/jieba-php.fukuball.com

Feature

  • 支持三種分詞模式:

  • 1)默認精確模式,試圖將句子最精確地切開,適合文本分析;

  • 2)全模式,把句子中所有的可以成詞的詞語都掃描出來,但是不能解決歧義。(需要充足的字典)

    1. 搜尋引擎模式,在精確模式的基礎上,對長詞再次切分,提高召回率,適合用於搜尋引擎分詞。
  • 支持繁體斷詞

  • 支持自定義詞典

  • 支持多語言 CJK 文本處理(中文、日文、韓文)

  • 支持 TF-IDF 分詞整合和詞性標註

  • 支持記憶體管理和快取優化

  • 支持自定義詞性標籤

Usage

  • 自動安裝:使用 composer 安裝後,透過 autoload 引用

代碼示例

php
composer require fukuball/jieba-php

代碼示例

php
require_once "/path/to/your/vendor/autoload.php";
  • 手動安裝:將 jieba-php 放置適當目錄後,透過 require_once 引用

代碼示例

php
require_once "/path/to/your/vendor/multi-array/MultiArray.php";
require_once "/path/to/your/vendor/multi-array/Factory/MultiArrayFactory.php";
require_once "/path/to/your/class/Jieba.php";
require_once "/path/to/your/class/Finalseg.php";

Algorithm

  • 基於 Trie 樹結構實現高效的詞圖掃描,生成句子中漢字所有可能成詞情況所構成的有向無環圖(DAG)
  • 採用了動態規劃查找最大概率路徑, 找出基於詞頻的最大切分組合
  • 對於未登錄詞,採用了基於漢字成詞能力的 HMM 模型,使用了 Viterbi 算法
  • BEMS 的解釋 https://github.com/fxsjy/jieba/issues/7

Interface

  • 組件只提供 jieba.cut 方法用於分詞
  • cut 方法接受兩個輸入參數: 1) 第一個參數為需要分詞的字符串 2)cut_all 參數用來控制分詞模式
  • 待分詞的字符串可以是 utf-8 字符串
  • jieba.cut 返回的結構是一個可迭代的 array

功能 1):分词

  • cut 方法接受想個輸入參數: 1) 第一個參數為需要分詞的字符串 2)cut_all 參數用來控制分詞模式
  • cutForSearch 方法接受一個參數:需要分詞的字符串,該方法適合用於搜索引擎構建倒排索引的分詞,粒度比較細
  • 注意:待分詞的字符串是 utf-8 字符串
  • cut 以及 cutForSearch 返回的結構是一個可迭代的 array

代碼示例 (Tutorial)

…

Output:

…

功能 2):添加自定義詞典

  • 開發者可以指定自己自定義的詞典,以便包含 jieba 詞庫裡沒有的詞。雖然 jieba 有新詞識別能力,但是自行添加新詞可以保證更高的正確率

  • 用法: Jieba::loadUserDict(file_name) # file_name 為自定義詞典的絕對路徑

  • 詞典格式和 dict.txt 一樣,一個詞佔一行;每一行分為三部分,一部分為詞語,一部分為詞頻,一部分為詞性,用空格隔開

  • 範例:

    云计算 5 n 李小福 2 n 创新办 3 n

    之前: 李小福 / 是 / 创新 / 办 / 主任 / 也 / 是 / 云 / 计算 / 方面 / 的 / 专家 / 加載自定義詞庫後: 李小福 / 是 / 创新办 / 主任 / 也 / 是 / 云计算 / 方面 / 的 / 专家 /

說明:"通过用户自定义词典来增强歧义纠错能力" --- https://github.com/fxsjy/jieba/issues/14

  • 自定義詞典:https://github.com/fukuball/jieba-php/blob/master/src/dict/user_dict.txt

功能 3):關鍵詞提取

  • JiebaAnalyse::extractTags($content, $top_k)
  • content 為待提取的文本
  • top_k 為返回幾個 TF/IDF 權重最大的關鍵詞,默認值為 20
  • 可使用 setStopWords 增加自定義 stop words

代碼示例 (關鍵詞提取)

…

Output:

…

功能 4):詞性分詞

  • 詞性說明:https://gist.github.com/luw2007/6016931

代碼示例 (Tutorial)

…

新功能:支持 TF-IDF 分數的詞性分詞

…

Output:

…

功能 5):切換成繁體字典

代碼示例 (Tutorial)

php
ini_set('memory_limit', '1024M');

require_once dirname(dirname(__FILE__))."/vendor/multi-array/MultiArray.php";
require_once dirname(dirname(__FILE__))."/vendor/multi-array/Factory/MultiArrayFactory.php";
require_once dirname(dirname(__FILE__))."/class/Jieba.php";
require_once dirname(dirname(__FILE__))."/class/Finalseg.php";
use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\Finalseg;
Jieba::init(array('mode'=>'default','dict'=>'big'));
Finalseg::init();

$seg_list = Jieba::cut("怜香惜玉也得要看对象啊!");
var_dump($seg_list);

$seg_list = Jieba::cut("憐香惜玉也得要看對象啊!");
var_dump($seg_list);

Output:

php
array(7) {
  [0]=>
  string(12) "怜香惜玉"
  [1]=>
  string(3) "也"
  [2]=>
  string(3) "得"
  [3]=>
  string(3) "要"
  [4]=>
  string(3) "看"
  [5]=>
  string(6) "对象"
  [6]=>
  string(3) "啊"
}
array(7) {
  [0]=>
  string(12) "憐香惜玉"
  [1]=>
  string(3) "也"
  [2]=>
  string(3) "得"
  [3]=>
  string(3) "要"
  [4]=>
  string(3) "看"
  [5]=>
  string(6) "對象"
  [6]=>
  string(3) "啊"
}

功能 5):切換成繁體字典

代碼示例 (Tutorial)

php
ini_set('memory_limit', '1024M');

require_once dirname(dirname(__FILE__))."/vendor/multi-array/MultiArray.php";
require_once dirname(dirname(__FILE__))."/vendor/multi-array/Factory/MultiArrayFactory.php";
require_once dirname(dirname(__FILE__))."/class/Jieba.php";
require_once dirname(dirname(__FILE__))."/class/Finalseg.php";
use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\Finalseg;
Jieba::init(array('mode'=>'default','dict'=>'big'));
Finalseg::init();

$seg_list = Jieba::cut("怜香惜玉也得要看对象啊!");
var_dump($seg_list);

$seg_list = Jieba::cut("憐香惜玉也得要看對象啊!");
var_dump($seg_list);

Output:

php
array(7) {
  [0]=>
  string(12) "怜香惜玉"
  [1]=>
  string(3) "也"
  [2]=>
  string(3) "得"
  [3]=>
  string(3) "要"
  [4]=>
  string(3) "看"
  [5]=>
  string(6) "对象"
  [6]=>
  string(3) "啊"
}
array(7) {
  [0]=>
  string(12) "憐香惜玉"
  [1]=>
  string(3) "也"
  [2]=>
  string(3) "得"
  [3]=>
  string(3) "要"
  [4]=>
  string(3) "看"
  [5]=>
  string(6) "對象"
  [6]=>
  string(3) "啊"
}

功能 6):多語言 CJK 文本處理

jieba-php 現在支援更好的多語言 CJK(中文、日文、韓文)文本處理,包括混合語言文本的處理。

代碼示例 (Tutorial)

…

示範腳本

  • 基本多語言處理:php src/cmd/demo_mixed_cjk.php
  • TF-IDF 和詞性標註整合:php src/cmd/demo_tf_idf_pos.php

Output:

…

功能 7):返回詞語在原文的起止位置

代碼示例 (Tutorial)

php
ini_set('memory_limit', '1024M');

require_once dirname(dirname(__FILE__))."/vendor/multi-array/MultiArray.php";
require_once dirname(dirname(__FILE__))."/vendor/multi-array/Factory/MultiArrayFactory.php";
require_once dirname(dirname(__FILE__))."/class/Jieba.php";
require_once dirname(dirname(__FILE__))."/class/Finalseg.php";
use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\Finalseg;
Jieba::init(array('mode'=>'test','dict'=>'big'));
Finalseg::init();

$seg_list = Jieba::tokenize("永和服装饰品有限公司");
var_dump($seg_list);

Output:

php
array(4) {
  [0] =>
  array(3) {
    'word' =>
    string(6) "永和"
    'start' =>
    int(0)
    'end' =>
    int(2)
  }
  [1] =>
  array(3) {
    'word' =>
    string(6) "服装"
    'start' =>
    int(2)
    'end' =>
    int(4)
  }
  [2] =>
  array(3) {
    'word' =>
    string(6) "饰品"
    'start' =>
    int(4)
    'end' =>
    int(6)
  }
  [3] =>
  array(3) {
    'word' =>
    string(12) "有限公司"
    'start' =>
    int(6)
    'end' =>
    int(10)
  }
}

其他詞典

  1. 佔用內容較小的詞典 https://github.com/fukuball/jieba-php/blob/master/src/dict/dict.small.txt

  2. 支持繁體斷詞的詞典 https://github.com/fukuball/jieba-php/blob/master/src/dict/dict.big.txt

常見問題

  1. 模型的數據是如何生成的? https://github.com/fxsjy/jieba/issues/7
  2. 這個庫的授權是? https://github.com/fxsjy/jieba/issues/2

jieba-php English Document

Online Demo

  • Demo Site Repo:https://github.com/fukuball/jieba-php.fukuball.com

Feature

  • Support three types of segmentation mode:

    1. Accurate Mode, attempt to cut the sentence into the most accurate segmentation, which is suitable for text analysis;
    1. Full Mode, break the words of the sentence into words scanned
    1. Search Engine Mode, based on the Accurate Mode, with an attempt to cut the long words into several short words, which can enhance the recall rate
  • Support Traditional Chinese word segmentation

  • Support custom dictionaries

  • Support multi-language CJK text processing (Chinese, Japanese, Korean)

  • Support TF-IDF integration and POS tagging

  • Support memory management and cache optimization

  • Support custom POS tags

Usage

  • Installation: Use composer to install jieba-php, then require the autoload file to use jieba-php.

Algorithm

  • Based on the Trie tree structure to achieve efficient word graph scanning; sentences using Chinese characters constitute a directed acyclic graph (DAG).
  • Employs memory search to calculate the maximum probability path, in order to identify the maximum tangential points based on word frequency combination.
  • For unknown words, the character position HMM-based model is used, using the Viterbi algorithm.
  • The meaning of BEMS https://github.com/fxsjy/jieba/issues/7.

Interface

  • The cut method accepts two parameters: 1) first parameter is the string to segmentation 2)the second parameter cut_all to control segmentation mode.
  • The string to segmentation may use utf-8 string.
  • cutForSearch accpets only on parameter: the string that requires segmentation, and it will cut the sentence into short words
  • cut and cutForSearch return an segmented array.

Function 1) Segmentation

Example (Tutorial)

…

Output:

…

Function 2) Add a custom dictionary

  • Developers can specify their own custom dictionary to include in the jieba thesaurus. jieba has the ability to identify new words, but adding your own new words can ensure a higher rate of correct segmentation.

  • Usage: Jieba::loadUserDict(file_name) # file_name is a custom dictionary path.

  • The dictionary format is the same as that of dict.txt: one word per line; each line is divided into two parts, the first is the word itself, the other is the word frequency, separated by a space.

  • Example:

    云计算 5 李小福 2 创新办 3

    之前: 李小福 / 是 / 创新 / 办 / 主任 / 也 / 是 / 云 / 计算 / 方面 / 的 / 专家 / 加載自定義詞庫後: 李小福 / 是 / 创新办 / 主任 / 也 / 是 / 云计算 / 方面 / 的 / 专家 /

Function 3) Keyword Extraction

  • JiebaAnalyse::extractTags($content, $top_k)
  • content: the text to be extracted
  • top_k: to return several TF/IDF weights for the biggest keywords, the default value is 20

Example (keyword extraction)

…

Output:

php
array(10) {
  ["是否"]=>
  float(1.2196321889395)
  ["一般"]=>
  float(1.0032459890209)
  ["肌迫"]=>
  float(0.64654314660465)
  ["怯懦"]=>
  float(0.44762844339349)
  ["藉口"]=>
  float(0.32327157330233)
  ["逼不得已"]=>
  float(0.32327157330233)
  ["不安全感"]=>
  float(0.26548304656279)
  ["同感"]=>
  float(0.23929673812326)
  ["有把握"]=>
  float(0.21043366018744)
  ["空洞"]=>
  float(0.20598261709442)
}

Function 4) Word Segmentation and Tagging

  • Word Tagging Meaning:https://gist.github.com/luw2007/6016931

Example (word tagging)

…

Output:

…

Function 5):Use Traditional Chinese

Example (Tutorial)

php
ini_set('memory_limit', '1024M');

require_once dirname(dirname(__FILE__))."/vendor/multi-array/MultiArray.php";
require_once dirname(dirname(__FILE__))."/vendor/multi-array/Factory/MultiArrayFactory.php";
require_once dirname(dirname(__FILE__))."/class/Jieba.php";
require_once dirname(dirname(__FILE__))."/class/Finalseg.php";
use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\Finalseg;
Jieba::init(array('mode'=>'default','dict'=>'big'));
Finalseg::init();

$seg_list = Jieba::cut("怜香惜玉也得要看对象啊!");
var_dump($seg_list);

$seg_list = Jieba::cut("憐香惜玉也得要看對象啊!");
var_dump($seg_list);

Output:

php
array(7) {
  [0]=>
  string(12) "怜香惜玉"
  [1]=>
  string(3) "也"
  [2]=>
  string(3) "得"
  [3]=>
  string(3) "要"
  [4]=>
  string(3) "看"
  [5]=>
  string(6) "对象"
  [6]=>
  string(3) "啊"
}
array(7) {
  [0]=>
  string(12) "憐香惜玉"
  [1]=>
  string(3) "也"
  [2]=>
  string(3) "得"
  [3]=>
  string(3) "要"
  [4]=>
  string(3) "看"
  [5]=>
  string(6) "對象"
  [6]=>
  string(3) "啊"
}

Function 6):Keeping Japanese or Korean original text

Example (Tutorial)

…

Output:

…

詞性說明

…

功能 8):記憶體管理

為了處理大量文本時的記憶體使用問題,jieba-php 提供了新的 JiebaMemory 類別來統一管理所有類別的記憶體使用。

JiebaMemory - 統一記憶體管理

php
ini_set('memory_limit', '1024M');

use Fukuball\Jieba\JiebaMemory;

// 初始化所有類別
JiebaMemory::initAll();

// 檢查初始化狀態
$status = JiebaMemory::getInitializationStatus();
var_dump($status);

// 獲取記憶體使用統計
$stats = JiebaMemory::getMemoryStats();
echo "當前記憶體使用:" . $stats['current_memory_usage_formatted'] . "\n";
echo "峰值記憶體使用:" . $stats['peak_memory_usage_formatted'] . "\n";

// 清除所有快取但保持類別初始化
JiebaMemory::clearAllCaches();

// 銷毀所有類別釋放記憶體
JiebaMemory::destroyAll();

詳細快取統計

php
// 獲取所有類別的快取統計
$cacheStats = JiebaMemory::getAllCacheStats();
echo "Jieba DAG 快取大小:" . $cacheStats['jieba']['dag_cache_size'] . "\n";
echo "Posseg 詞性標籤數量:" . $cacheStats['posseg']['word_tag_size'] . "\n";
echo "JiebaAnalyse IDF 頻率數量

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPchinese-text-segmentationmachine-learningnatural-language-processingnlp

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言