PHPackages                             zonuexe/budoux - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. zonuexe/budoux

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

zonuexe/budoux
==============

A small, standalone, and language-neutral line break organizer.

v0.6.3.2(1y ago)23.0kApache-2.0PHPPHP ^8.0

Since Sep 25Pushed 1y agoCompare

[ Source](https://github.com/zonuexe/budoux-php)[ Packagist](https://packagist.org/packages/zonuexe/budoux)[ RSS](/packages/zonuexe-budoux/feed)WikiDiscussions php Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (6)Used By (0)

BudouX for PHP
==============

[](#budoux-for-php)

[![Package version](https://camo.githubusercontent.com/3eaf3d9fc4bec38e38ab508ae66b850e158eb78701965ce6656a7f60b2c71eac/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7a6f6e756578652f6275646f75782e7376673f7374796c653d666c6174)](https://packagist.org/packages/zonuexe/budoux)

Standalone. Small. Language-neutral.

BudouX is the successor to [Budou](https://github.com/google/budou), the machine learning powered line break organizer tool.

[![Example](https://raw.githubusercontent.com/google/budoux/main/example.png)](https://raw.githubusercontent.com/google/budoux/main/example.png)

It is **standalone**. It works with no dependency on third-party word segmenters such as Google cloud natural language API.

It is **small**. It takes only around 15 KB including its machine learning model. It's reasonable to use it even on the client-side.

It is **language-neutral**. You can train a model for any language by feeding a dataset to BudouX’s training script.

Last but not least, BudouX supports HTML inputs.

Demo
----

[](#demo)

Natural languages supported by pretrained models
------------------------------------------------

[](#natural-languages-supported-by-pretrained-models)

- Japanese
- Simplified Chinese
- Traditional Chinese
- Thai

### Korean support?

[](#korean-support)

Korean uses spaces between words, so you can generally prevent words from being split across lines by applying the CSS property `word-break: keep-all` to the paragraph, which should be much more performant than installing BudouX. That said, we're happy to explore dedicated Korean language support if the above solution proves insufficient.

Supported Programming languages
-------------------------------

[](#supported-programming-languages)

- PHP (Packagist: [zonuexe/budoux](https://packagist.org/packages/zonuexe/budoux))

The following languages are provided by the [original BudouX](https://github.com/google/budoux).

- Python
- [JavaScript](https://github.com/google/budoux/tree/main/javascript/)
- [Java](https://github.com/google/budoux/tree/main/java/)

### Usage

[](#usage)

#### Library

[](#library)

You can get a list of phrases by feeding a sentence to the parser. The easiest way is to get a parser is loading the default parser for each language.

**Japanese:**

```
use Budoux;

$parser = Budoux\Parser::loadDefaultJapaneseParser();
var_dump($parser->parse('今日は天気です。'));
# ['今日は', '天気です。']
```

**Simplified Chinese:**

```
use Budoux;

$parser = Budoux\Parser::loadDefaultSimplifiedChineseParser();
var_dump($parser->parse('今天是晴天。'));
# ['今天', '是', '晴天。']
```

**Traditional Chinese:**

```
use Budoux;

$parser = Budoux\Parser::loadDefaultTraditionalChineseParser();
var_dump($parser->parse('今天是晴天。'));
# ['今天', '是', '晴天。']
```

**Thai:**

```
use Budoux;

$parser = Budoux\Parser::loadDefaultThaiParser();
var_dump($parser->parse('วันนี้อากาศดี'));
# ['วัน', 'นี้', 'อากาศ', 'ดี']
```

You can also translate an HTML string to wrap phrases with non-breaking markup. The default parser uses zero-width space (U+200B) to separate phrases.

```
var_dump($parser->translate_html_string('今日はとても天気です。'));
# 今日は\u200bとても\u200b天気です。
```

Caution

The PHP port does ***not yet*** support HTML.

Please note that separators are denoted as `\u200b` in the example above for illustrative purposes, but the actual output is an invisible string as it's a zero-width space.

If you have a custom model, you can use it as follows.

```
$model = json_decode(file_get_contents('/path/to/your/model.json'), true);
$parser = Budoux\Parser::Parser(model)
```

A model file for BudouX is a JSON file that contains pairs of a feature and its score extracted by machine learning training. Each score represents the significance of the feature in determining whether to break the sentence at a specific point.

For more details of the JavaScript model, please refer to [JavaScript module README](https://github.com/google/budoux/tree/main/javascript/README.md).

#### CLI

[](#cli)

You can also format inputs on your terminal with `budoux` command.

```
$ budoux 本日は晴天です。 # default: japanese
本日は
晴天です。

$ budoux -l ja 本日は晴天です。
本日は
晴天です。

$ budoux -l zh-hans 今天天气晴朗。
今天
天气
晴朗。

$ budoux -l zh-hant 今天天氣晴朗。
今天
天氣
晴朗。

$ budoux -l th วันนี้อากาศดี
วัน
นี้
อากาศ
ดี
```

```
$ echo $'本日は晴天です。\n明日は曇りでしょう。' | budoux
本日は
晴天です。
---
明日は
曇りでしょう。
```

```
$ budoux 本日は晴天です。 -H
本日は\u200b晴天です。
```

Please note that separators are denoted as `\u200b` in the example above for illustrative purposes, but the actual output is an invisible string as it's a zero-width space.

If you want to see help, run `budoux -h`.

```
$ budoux -h
usage: budoux [-h] [-H] [-m JSON | -l LANG] [-d STR] [-V] [TXT]

BudouX is the successor to Budou,
the machine learning powered line break organizer tool.

positional arguments:
  TXT                    text (default: None)

optional arguments:
  -h, --help             show this help message and exit
  -H, --html             HTML mode (default: False)
  -m JSON, --model JSON  custom model file path (default: /path/to/budoux/models/ja.json)
  -l LANG, --lang LANG   language of custom model (default: None)
  -d STR, --delim STR    output delimiter in TEXT mode (default: ---)
  -V, --version          show program's version number and exit

supported languages of `-l`, `--lang`:
- ja
- zh-hans
- zh-hant
- th
```

Caveat
------

[](#caveat)

BudouX supports HTML inputs and outputs HTML strings with markup that wraps phrases, but it's not meant to be used as an HTML sanitizer. **BudouX doesn't sanitize any inputs.** Malicious HTML inputs yield malicious HTML outputs. Please use it with an appropriate sanitizer library if you don't trust the input.

Background
----------

[](#background)

English text has many clues, like spacing and hyphenation, that enable beautiful and readable line breaks. However, some CJK languages lack these clues, and so are notoriously more difficult to process. Line breaks can occur randomly and usually in the middle of a word or a phrase without a more careful approach. This is a long-standing issue in typography on the Web, which results in a degradation of readability.

Budou was proposed as a solution to this problem in 2016. It automatically translates CJK sentences into HTML with lexical phrases wrapped in non-breaking markup, so as to semantically control line breaks. Budou has solved this problem to some extent, but it still has some problems integrating with modern web production workflow.

The biggest barrier in applying Budou to a website is that it has dependency on third-party word segmenters. Usually a word segmenter is a large program that is infeasible to download for every web page request. It would also be an undesirable option making a request to a cloud-based word segmentation service for every sentence, considering the speed and cost. That’s why we need a standalone line break organizer tool equipped with its own segmentation engine small enough to be bundled in a client-side JavaScript code.

Budou*X* is the successor to Budou, designed to be integrated with your website with no hassle.

How it works
------------

[](#how-it-works)

BudouX uses the [AdaBoost algorithm](https://en.wikipedia.org/wiki/AdaBoost) to segment a sentence into phrases by considering the task as a binary classification problem to predict whether to break or not between all characters. It uses features such as the characters around the break point, their Unicode blocks, and combinations of them to make a prediction. The output machine learning model, which is encoded as a JSON file, stores pairs of the feature and its significance score. The BudouX parser takes a model file to construct a segmenter and translates input sentences into a list of phrases.

Building a custom model
-----------------------

[](#building-a-custom-model)

You can build your own custom model for any language by preparing training data in the target language. A training dataset is a large text file that consists of sentences separated by phrases with the separator symbol "▁" (U+2581) like below.

```
私は▁遅刻魔で、▁待ち合わせに▁いつも▁遅刻してしまいます。
メールで▁待ち合わせ▁相手に▁一言、▁「ごめんね」と▁謝れば▁どうにか▁なると▁思っていました。
海外では▁ケータイを▁持っていない。

```

Assuming the text file is saved as `mysource.txt`, you can build your own custom model by running the following commands.

```
$ pip install .[dev]
$ python scripts/encode_data.py mysource.txt -o encoded_data.txt
$ python scripts/train.py encoded_data.txt -o weights.txt
$ python scripts/build_model.py weights.txt -o mymodel.json
```

Please note that `train.py` takes time to complete depending on your computer resources. Good news is that the training algorithm is an [anytime algorithm](https://en.wikipedia.org/wiki/Anytime_algorithm), so you can get a weights file even if you interrupt the execution. You can build a valid model file by passing that weights file to `build_model.py` even in such a case.

Constructing a training dataset from the KNBC corpus for Japanese
-----------------------------------------------------------------

[](#constructing-a-training-dataset-from-the-knbc-corpus-for-japanese)

The default model for Japanese (`budoux/models/ja.json`) is built using the [KNBC corpus](https://nlp.ist.i.kyoto-u.ac.jp/kuntt/). You can create a training dataset, which we name `source_knbc.txt` below for example, from the corpus by running the following commands:

```
$ curl -o knbc.tar.bz2 https://nlp.ist.i.kyoto-u.ac.jp/kuntt/KNBC_v1.0_090925_utf8.tar.bz2
$ tar -xf knbc.tar.bz2  # outputs KNBC_v1.0_090925_utf8 directory
$ python scripts/prepare_knbc.py KNBC_v1.0_090925_utf8 -o source_knbc.txt
```

Author
------

[](#author)

[Shuhei Iitsuka](https://tushuhei.com)

Disclaimer
----------

[](#disclaimer)

This is not an officially supported Google product.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance41

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 51.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~116 days

Total

5

Last Release

492d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e1cc4bc1729b858086681a12494fda6cced8f26db3d0928ae4d81c68ddf8dc06?d=identicon)[zonuexe](/maintainers/zonuexe)

---

Top Contributors

[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (238 commits)")[![tushuhei](https://avatars.githubusercontent.com/u/734905?v=4)](https://github.com/tushuhei "tushuhei (167 commits)")[![zonuexe](https://avatars.githubusercontent.com/u/822086?v=4)](https://github.com/zonuexe "zonuexe (18 commits)")[![eggplants](https://avatars.githubusercontent.com/u/42153744?v=4)](https://github.com/eggplants "eggplants (15 commits)")[![kojiishi](https://avatars.githubusercontent.com/u/1274377?v=4)](https://github.com/kojiishi "kojiishi (14 commits)")[![tamanyan](https://avatars.githubusercontent.com/u/2387508?v=4)](https://github.com/tamanyan "tamanyan (2 commits)")[![junseinagao](https://avatars.githubusercontent.com/u/54606780?v=4)](https://github.com/junseinagao "junseinagao (2 commits)")[![ryu22e](https://avatars.githubusercontent.com/u/301892?v=4)](https://github.com/ryu22e "ryu22e (1 commits)")[![sassy](https://avatars.githubusercontent.com/u/459739?v=4)](https://github.com/sassy "sassy (1 commits)")[![step-security-bot](https://avatars.githubusercontent.com/u/89328645?v=4)](https://github.com/step-security-bot "step-security-bot (1 commits)")[![hiro0218](https://avatars.githubusercontent.com/u/3617124?v=4)](https://github.com/hiro0218 "hiro0218 (1 commits)")[![Harukaichii](https://avatars.githubusercontent.com/u/34189742?v=4)](https://github.com/Harukaichii "Harukaichii (1 commits)")[![amitmarkel](https://avatars.githubusercontent.com/u/1833277?v=4)](https://github.com/amitmarkel "amitmarkel (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/zonuexe-budoux/health.svg)

```
[![Health](https://phpackages.com/badges/zonuexe-budoux/health.svg)](https://phpackages.com/packages/zonuexe-budoux)
```

###  Alternatives

[zservices/query

Pacote para consultas em serviços do governo.

131.1k](/packages/zservices-query)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
