ReMe/reme/components/tokenizer/jieba_tokenizer.py
jinliyl 54fa3381d7
Some checks failed
CI and Release / Docker / Build and test / amd64 (push) Has been cancelled
CI and Release / Docker / Build and test / arm64 (push) Has been cancelled
Security / CodeQL / Analyze python (push) Has been cancelled
CI / Python quality / Pre-commit (push) Has been cancelled
CI / Python tests / Unit Tests - py3.12 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.13 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.14 (push) Has been cancelled
CI / Python packages / Build and verify distributions (push) Has been cancelled
CI / Python quality / GitHub Actions (push) Has been cancelled
CI / Python tests / Hermes embedded - ubuntu-latest (push) Has been cancelled
CI / Python tests / Hermes embedded - windows-latest (push) Has been cancelled
CI / Python tests / Unit Tests - py3.11 (push) Has been cancelled
CI / ReMe Studio / Studio checks (push) Has been cancelled
Deploy / Documentation / Build documentation (push) Has been cancelled
CI / Windows / CLI smoke - py3.11 (push) Has been cancelled
Security / CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI and Release / Docker / Publish multi-platform tags (push) Has been cancelled
Deploy / Documentation / deploy (push) Has been cancelled
refactor(core): reorganize the ReMe runtime and shared helpers (#769)
* refactor(core): reorganize the ReMe runtime and shared helpers

* test(status): import the base component from its canonical module

* test: remove redundant unit checks

* refactor(core): use canonical runtime imports and shared CLI discovery

Restore evolve behavior to the base branch and update its imports. Use Application as the runtime entry point and encode SSE in the HTTP service.

* fix(cli): honor caller config when service replay is incomplete

Use partial startup overrides for port probes and resolve named configs before requesting the service process working directory.
2026-11-20 22:06:46 +08:00

45 lines
1.5 KiB
Python

"""Jieba tokenizer for Chinese text segmentation."""
from typing import Callable
from .base_tokenizer import BaseTokenizer
from ...core.registry import R
@R.register("jieba")
class JiebaTokenizer(BaseTokenizer):
"""Tokenizer backed by jieba for Chinese word segmentation.
`backend` selects the underlying implementation:
- "rjieba": Rust binding of jieba-rs, ~10-30x faster than pure Python (default).
- "jieba": Original pure-Python jieba, slowest but the reference.
"""
SUPPORTED_BACKENDS = ("rjieba", "jieba")
def __init__(self, backend: str = "rjieba", **kwargs):
super().__init__(**kwargs)
if backend not in self.SUPPORTED_BACKENDS:
raise ValueError(
f"Unknown jieba backend {backend!r}; expected one of {self.SUPPORTED_BACKENDS}",
)
self.backend = backend
self._cut: Callable[[str], list[str]] | None = None
async def _start(self) -> None:
await super()._start()
# Resolve the backend once at startup so per-call overhead is just one attribute lookup.
if self.backend == "rjieba":
import rjieba
self._cut = rjieba.cut
else:
import jieba
self._cut = jieba.cut
self.logger.info(f"JiebaTokenizer using backend: {self.backend}")
def _tokenize_one(self, text: str, **kwargs) -> list[str]:
if self._cut is None:
raise RuntimeError("Tokenizer not initialized. Call start() first.")
return list(self._cut(text))