Build and Use jieba-php: Chinese Word Segmentation in PHP via Rust

This guide explains how to install the jieba-php extension— a Rust‑based Chinese word segmentation library for PHP—by listing required dependencies, showing the cargo build steps, demonstrating runtime commands, describing the provided API, and offering a complete usage example.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Build and Use jieba-php: Chinese Word Segmentation in PHP via Rust

Overview

jieba-php provides Chinese word segmentation for PHP by wrapping the Rust implementation of the Jieba library.

Dependencies

rust

php-dev

Build

# Optional, specify if php isn’t installed globally.
# export PHP_CONFIG=<Your path of php-config>
# Build libjieba.so.
cargo build --release

Run

php -d "extension=target/release/libjieba.so" --ri jieba

php -d "extension=target/release/libjieba.so" -r "print_r((new Jieba())->cut('我们中出了一个叛徒'));"

API

class Jieba {
    public function __construct();
    public function cut(string $sentence, bool $hmm): array;
    public function cutForSearch(string $sentence, bool $hmm): array;
    public function cutAll(string $sentence): array;
}

Example

<?php

$jieba = new Jieba();

$words = $jieba->cut("我们中出了一个叛徒", true);
print_r($words);

$words = $jieba->cutAll("我们中出了一个叛徒");
print_r($words);

$words = $jieba->cutForSearch("我们中出了一个叛徒");
print_r($words);
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

jiebachinese segmentation
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.