|
| 1 | +"""jieba 兼容入口。""" |
| 2 | + |
| 3 | +from collections.abc import Iterator |
| 4 | +from typing import Any |
| 5 | + |
| 6 | +import jieba_next as _jieba_next |
| 7 | +from jieba_next import cut_for_search as _cut_for_search |
| 8 | +from jieba_next import lcut as _lcut |
| 9 | +from jieba_next import lcut_for_search as _lcut_for_search |
| 10 | + |
| 11 | + |
| 12 | +def cut(sentence: str, cut_all: bool = False, HMM: bool = True, use_paddle: bool = False) -> Iterator[str]: |
| 13 | + """ |
| 14 | + 兼容旧 jieba.cut 入口,底层委托给 jieba-next 的 Rust 加速实现。 |
| 15 | + """ |
| 16 | + return _jieba_next.cut(sentence, cut_all=cut_all, HMM=HMM) |
| 17 | + |
| 18 | + |
| 19 | +def lcut(sentence: str, cut_all: bool = False, HMM: bool = True, use_paddle: bool = False) -> list[str]: |
| 20 | + """ |
| 21 | + 兼容旧 jieba.lcut 入口,保持返回列表的调用习惯。 |
| 22 | + """ |
| 23 | + return _lcut(sentence, cut_all=cut_all, HMM=HMM) |
| 24 | + |
| 25 | + |
| 26 | +def cut_for_search(sentence: str, HMM: bool = True) -> Iterator[str]: |
| 27 | + """ |
| 28 | + 兼容旧 jieba.cut_for_search 入口,用于搜索模式分词。 |
| 29 | + """ |
| 30 | + return _cut_for_search(sentence, HMM=HMM) |
| 31 | + |
| 32 | + |
| 33 | +def lcut_for_search(sentence: str, HMM: bool = True) -> list[str]: |
| 34 | + """ |
| 35 | + 兼容旧 jieba.lcut_for_search 入口,用于搜索模式分词列表。 |
| 36 | + """ |
| 37 | + return _lcut_for_search(sentence, HMM=HMM) |
| 38 | + |
| 39 | + |
| 40 | +def __getattr__(name: str) -> Any: |
| 41 | + """ |
| 42 | + 将未显式封装的 jieba 属性回退到 jieba-next,减少旧调用面的迁移成本。 |
| 43 | + """ |
| 44 | + return getattr(_jieba_next, name) |
0 commit comments