From 134c441754cb69e6039d55d8c11bb00c06c7fb0d Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 23 May 2026 12:59:33 +0800 Subject: [PATCH] fix: replace fast jieba dependency --- .../tools/impl/query_transfer_history.py | 2 +- app/utils/jieba.py | 6 +-- jieba/__init__.py | 44 +++++++++++++++++++ requirements.in | 2 +- tests/test_fast_jieba_utils.py | 9 ---- tests/test_jieba_utils.py | 19 ++++++++ 6 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 jieba/__init__.py delete mode 100644 tests/test_fast_jieba_utils.py create mode 100644 tests/test_jieba_utils.py diff --git a/app/agent/tools/impl/query_transfer_history.py b/app/agent/tools/impl/query_transfer_history.py index 8ec5c3a17..c257f1294 100644 --- a/app/agent/tools/impl/query_transfer_history.py +++ b/app/agent/tools/impl/query_transfer_history.py @@ -69,7 +69,7 @@ class QueryTransferHistoryTool(MoviePilotTool): async with AsyncSessionFactory() as db: # 处理标题搜索 if title: - # 使用 fast-jieba 分词处理标题。 + # 使用统一分词封装处理标题,便于替换底层实现。 words = jieba_cut(title, HMM=False) title_search = "%".join(words) # 查询记录 diff --git a/app/utils/jieba.py b/app/utils/jieba.py index 4a834042f..d2ce880cc 100644 --- a/app/utils/jieba.py +++ b/app/utils/jieba.py @@ -1,10 +1,10 @@ """中文分词工具。""" -from fast_jieba import cut as fast_jieba_cut +from jieba_next import cut as jieba_next_cut def cut(text: str, HMM: bool = True, cut_all: bool = False) -> list[str]: """ - 使用 fast-jieba 执行中文分词,并兼容 jieba.cut 的常用参数名。 + 使用 jieba-next 执行中文分词,并兼容 jieba.cut 的常用参数名。 """ - return fast_jieba_cut(text, hmm=HMM, cut_all=cut_all) + return list(jieba_next_cut(text, HMM=HMM, cut_all=cut_all)) diff --git a/jieba/__init__.py b/jieba/__init__.py new file mode 100644 index 000000000..3ab8068f8 --- /dev/null +++ b/jieba/__init__.py @@ -0,0 +1,44 @@ +"""jieba 兼容入口。""" + +from collections.abc import Iterator +from typing import Any + +import jieba_next as _jieba_next +from jieba_next import cut_for_search as _cut_for_search +from jieba_next import lcut as _lcut +from jieba_next import lcut_for_search as _lcut_for_search + + +def cut(sentence: str, cut_all: bool = False, HMM: bool = True, use_paddle: bool = False) -> Iterator[str]: + """ + 兼容旧 jieba.cut 入口,底层委托给 jieba-next 的 Rust 加速实现。 + """ + return _jieba_next.cut(sentence, cut_all=cut_all, HMM=HMM) + + +def lcut(sentence: str, cut_all: bool = False, HMM: bool = True, use_paddle: bool = False) -> list[str]: + """ + 兼容旧 jieba.lcut 入口,保持返回列表的调用习惯。 + """ + return _lcut(sentence, cut_all=cut_all, HMM=HMM) + + +def cut_for_search(sentence: str, HMM: bool = True) -> Iterator[str]: + """ + 兼容旧 jieba.cut_for_search 入口,用于搜索模式分词。 + """ + return _cut_for_search(sentence, HMM=HMM) + + +def lcut_for_search(sentence: str, HMM: bool = True) -> list[str]: + """ + 兼容旧 jieba.lcut_for_search 入口,用于搜索模式分词列表。 + """ + return _lcut_for_search(sentence, HMM=HMM) + + +def __getattr__(name: str) -> Any: + """ + 将未显式封装的 jieba 属性回退到 jieba-next,减少旧调用面的迁移成本。 + """ + return getattr(_jieba_next, name) diff --git a/requirements.in b/requirements.in index e0af5e36a..54ab1f4ed 100644 --- a/requirements.in +++ b/requirements.in @@ -64,7 +64,7 @@ pywebpush~=2.0.3 aiosqlite~=0.21.0 psycopg2-binary~=2.9.10 asyncpg~=0.30.0 -fast-jieba~=0.4.0 +jieba-next~=1.0.0rc1 rsa~=4.9 redis~=6.2.0 async_timeout~=5.0.1; python_full_version < "3.11.3" diff --git a/tests/test_fast_jieba_utils.py b/tests/test_fast_jieba_utils.py deleted file mode 100644 index 1568b223c..000000000 --- a/tests/test_fast_jieba_utils.py +++ /dev/null @@ -1,9 +0,0 @@ -from app.utils.jieba import cut - - -def test_cut_accepts_legacy_hmm_argument(): - """验证兼容封装仍支持旧 jieba.cut 的 HMM 参数名。""" - words = cut("台湾后台测试", HMM=False) - - assert "".join(words) == "台湾后台测试" - assert "后台" in words diff --git a/tests/test_jieba_utils.py b/tests/test_jieba_utils.py new file mode 100644 index 000000000..b60bcf1f2 --- /dev/null +++ b/tests/test_jieba_utils.py @@ -0,0 +1,19 @@ +import jieba + +from app.utils.jieba import cut + + +def test_cut_accepts_legacy_hmm_argument(): + """验证兼容封装仍支持旧 jieba.cut 的 HMM 参数名。""" + words = cut("台湾后台测试", HMM=False) + + assert "".join(words) == "台湾后台测试" + assert "后台" in words + + +def test_legacy_jieba_import_uses_compat_entrypoint(): + """验证插件仍可通过旧 jieba.cut 入口使用主程序分词实现。""" + words = list(jieba.cut("台湾后台测试", HMM=False)) + + assert "".join(words) == "台湾后台测试" + assert "后台" in words