"""辅料连写串词典分词回归测试(skills/compatibility/skill._heuristic_excipients)。 复现真实问题:多个辅料无分隔符连写时只识别其中少数。修复后应按 KB 别名最大匹配 切分为全部辅料,未知段保留为独立(未知)辅料。 """ import os import sys sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "core")) from skills.compatibility.skill import _heuristic_excipients # noqa: E402 from skills.compatibility.knowledge_base import get_default_kb # noqa: E402 def test_concatenated_excipients_segmented_to_six(): text = "马铃薯淀粉微晶纤维素聚维酮硬脂酸钙交联羧甲基纤维素钠欧巴代" out = _heuristic_excipients(text) assert out == [ "马铃薯淀粉", "微晶纤维素", "聚维酮", "硬脂酸钙", "交联羧甲基纤维素钠", "欧巴代", ] kb = get_default_kb() # 全部应解析为知识库已知条目(修复前 硬脂酸钙 / 欧巴代 / 马铃薯淀粉 无法识别)。 keys = {kb.resolve(n).key for n in out} assert keys == {"starch", "mcc", "povidone", "calcium_stearate", "croscarmellose_sodium", "opadry"} assert all(kb.resolve(n).known for n in out) def test_delimited_input_preserved(): assert _heuristic_excipients("乳糖、硬脂酸镁,微晶纤维素") == ["乳糖", "硬脂酸镁", "微晶纤维素"] def test_single_known_and_unknown_preserved(): assert _heuristic_excipients("乳糖") == ["乳糖"] assert _heuristic_excipients("完全不存在的辅料QWERTY") == ["完全不存在的辅料QWERTY"] def test_dedup_repeated_excipients(): out = _heuristic_excipients("乳糖、乳糖、微晶纤维素") assert out == ["乳糖", "微晶纤维素"] def test_unknown_run_between_known_kept(): """已知辅料之间夹未知串时,未知串作为独立辅料保留(不丢失)。""" out = _heuristic_excipients("微晶纤维素某新型辅料XYZ聚维酮") assert "微晶纤维素" in out and "聚维酮" in out assert any("某新型辅料" in x for x in out)