litsea-python
Python binding for Litsea, a compact word segmentation and POS (Part-of-Speech) tagging library for Japanese, Chinese, Korean, and English.
Installation
pip install litsea
Wheels are built with the stable ABI (abi3) and work on CPython 3.10 and later.
Models are not bundled
The package ships code only. Download a pre-trained model from the Litsea repository and point the segmenter at it:
| Model | Purpose | Size |
|---|---|---|
japanese.model, chinese.model, korean.model, english.model |
Segmentation | 84 KB – 2.0 MB |
japanese_pos.model, chinese_pos.model, korean_pos.model, english_pos.model |
Segmentation + POS | 3.0 – 8.0 MB |
You never have to say which kind you have: the model file identifies itself, and has_pos reports what the loaded model can do.
Usage
Segmentation
from litsea import Language, Segmenter
seg = Segmenter.open(Language.JAPANESE, "models/japanese.model")
seg.segment("これはテストです。")
# ['これ', 'は', 'テスト', 'です', '。']
seg.segment_batch(["これはテストです。", "東京都から神奈川県へ引っ越した"])
# [['これ', 'は', 'テスト', 'です', '。'],
# ['東京', '都', 'から', '神奈川', '県', 'へ', '引っ越し', 'た']]
For space-delimited languages the whitespace comes back as its own token, so the tokens always reconstruct the input:
Segmenter.open("ko", "models/korean.model").segment("안녕하세요 반갑습니다")
# ['안녕하세요', ' ', '반갑습니다']
Language names work anywhere a Language does:
Segmenter.open("ja", "models/japanese.model")
Segmenter.open("japanese", "models/japanese.model")
POS tagging
from litsea import Language, Segmenter
seg = Segmenter.open(Language.JAPANESE, "models/japanese_pos.model")
seg.has_pos
# True
for token in seg.segment_with_pos("これはテストです。"):
print(token.surface, token.pos.name, token.start, token.end)
# これ PRON 0 6
# は ADP 6 9
# テスト NOUN 9 18
# です AUX 18 24
# 。 PUNCT 24 27
start and end are byte offsets into the input, so text.encode()[token.start:token.end].decode() gives the surface back. They are exact for both segmentation and POS output, including for space-preserving languages such as Korean and English.
Calling segment_with_pos on a segmentation-only model raises PosUnavailableError.
Other model sources
Segmenter.from_bytes(Language.KOREAN, open("korean.model", "rb").read())
Segmenter.from_uri(Language.CHINESE, "https://example.com/chinese.model")
Training
from litsea import CancelToken, Extractor, Language, Trainer
Extractor(Language.JAPANESE).extract("corpus.txt", "features.txt")
metrics = Trainer(0.01, 10_000, "features.txt").train("japanese.model")
print(f"accuracy: {metrics.accuracy:.2f}%")
Two-stage (segmentation + POS) training:
from litsea import Extractor, Language, TwoStageTrainer
Extractor(Language.JAPANESE).extract_two_stage("corpus_pos.txt", "features", feature_set="fast")
metrics = TwoStageTrainer(10, "features").train("japanese_pos.model")
print(metrics.stage1.accuracy, metrics.stage2.accuracy)
A TwoStageTrainer can only be used once — training collapses stage 1 into an AdaBoost model, which consumes the trainer. available reports whether it can still run.
Cancelling a training run
Training releases the GIL, so another thread can stop it:
import threading
from litsea import CancelToken, Trainer
cancel = CancelToken()
trainer = Trainer(0.01, 100_000, "features.txt")
threading.Timer(60.0, cancel.cancel).start()
metrics = trainer.train("japanese.model", cancel=cancel)
Cancelling is not an error: training stops at its next check point, still writes the partially trained model, and returns its metrics.
The binding never installs a signal handler, so Ctrl-C handling stays yours.
Errors
Every exception derives from LitseaError, so one except clause catches them all.
| Exception | Raised when |
|---|---|
InvalidArgumentError |
Unknown language name, unknown feature set, reused trainer |
ModelError |
Download failed, or the file is a legacy joint POS model |
IoError |
A file could not be read or written |
ParseError |
The model or training data is malformed |
UnsupportedError |
The scheme or operation is unavailable in this build |
PosUnavailableError |
POS tagging requested from a segmentation-only model |
Threading
A Segmenter is immutable and safe to share between threads. segment_batch, segment_with_pos_batch, extract, and every train release the GIL. Single-sentence segment and segment_with_pos keep it: releasing would require copying the input string, which costs more than segmenting one sentence.
Development
make setup-venv # create the venv and install the dev tools
make test-litsea-python # cargo test + maturin develop + pytest
make build-litsea-python # build a release wheel
License
MIT. See LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file litsea-0.13.0-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: litsea-0.13.0-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 2.2 MB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f8dd1912096a230f434c073cc6f24fe68186df6c915908242f9f007a97f7241d
|
|
| MD5 |
d4b84652ecbddd1f942c01eddeca70b8
|
|
| BLAKE2b-256 |
f1729097e9ed0f074884864d7148799d1ffc5efa1fee00684882158dd7dd8b63
|
File details
Details for the file litsea-0.13.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: litsea-0.13.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 2.7 MB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d6aa70d799edf3ee04aca53396d4974a0d853c4c6f813b9922c748650d1180e
|
|
| MD5 |
f5434561520b0c38786e26bf61fc7ff6
|
|
| BLAKE2b-256 |
d490dbe289d31a3fe563b09cc427be9dab8f49e3f1baff4409e2546faf2fad97
|
File details
Details for the file litsea-0.13.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: litsea-0.13.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 2.6 MB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec22fa1de34efa19b3b612cb8342c4f911dc0689bc7240f9334839faa38986f4
|
|
| MD5 |
5ce199993fea74352e726e2be732ebed
|
|
| BLAKE2b-256 |
25b46bcc4914d3cc06f896a13f23e6bc5e367ce51af06e000cb405650d66f51e
|
File details
Details for the file litsea-0.13.0-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: litsea-0.13.0-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 2.5 MB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c0452c624cba31de1807ed1f9e6e7cbd8e7f6f2ff216fc805a730826fab2f23
|
|
| MD5 |
30ccf41cacd3639d12e29c1efdc23428
|
|
| BLAKE2b-256 |
c9fcaae074732a138b317762b9ee58a23f5cef85e231975baffad0ba2eb6663c
|
File details
Details for the file litsea-0.13.0-cp310-abi3-macosx_10_12_x86_64.whl.
File metadata
- Download URL: litsea-0.13.0-cp310-abi3-macosx_10_12_x86_64.whl
- Upload date:
- Size: 2.6 MB
- Tags: CPython 3.10+, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via:
maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
647d837dc61d355179654090b84e762b37da7902151a3a3c1cdfc043c0d8c414
|
|
| MD5 |
acb8ceebb245aad29d15d27e752c6c9e
|
|
| BLAKE2b-256 |
9b6d7a139a6f8a093743fa6d51d004c6d24afcca92a80abc5c5591459805455e
|