refactor(tree): extract _clean_join to keep entity-fields complexity at B
质量门要求新增代码复杂度不超过 B 级:将 node_entity_fields 中重复的 "清洗-去重-拼接"逻辑抽为模块级纯函数 _clean_join,主体收敛为两次 调用。radon:node_entity_fields C(13) → B(6),_clean_join A(5)。 算法 #11(树环境语义搜索)数据访问层,行为不变(既有 4 测试未改全绿)。
This commit is contained in:
+21
-13
@@ -94,6 +94,19 @@ _ENTITY_FIELDS_BY_LEVEL: dict[str, tuple[str, ...]] = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _clean_join(values: object) -> str:
|
||||||
|
"""过滤非空字符串、去重(保序)、分号拼接;无有效值返回空串。
|
||||||
|
|
||||||
|
参数:
|
||||||
|
values: 待清洗的值集合(通常为 list[str],容忍 None / 混入非字符串)。
|
||||||
|
|
||||||
|
返回:
|
||||||
|
"a; b; c" 形式的拼接串,无有效值时为空串。
|
||||||
|
"""
|
||||||
|
cleaned = [v.strip() for v in (values or []) if isinstance(v, str) and v.strip()]
|
||||||
|
return "; ".join(dict.fromkeys(cleaned))
|
||||||
|
|
||||||
|
|
||||||
def _collect_from_obj(
|
def _collect_from_obj(
|
||||||
obj: object,
|
obj: object,
|
||||||
out: list[str],
|
out: list[str],
|
||||||
@@ -240,23 +253,18 @@ class TreeEnvironment:
|
|||||||
if node is None:
|
if node is None:
|
||||||
raise KeyError(f"节点不存在: {node_id}")
|
raise KeyError(f"节点不存在: {node_id}")
|
||||||
level = _node_level(node)
|
level = _node_level(node)
|
||||||
out: dict[str, str] = {}
|
|
||||||
|
|
||||||
entity_values: list[str] = []
|
entity_values: list[str] = []
|
||||||
for field_name in _ENTITY_FIELDS_BY_LEVEL[level]:
|
for field_name in _ENTITY_FIELDS_BY_LEVEL[level]:
|
||||||
for value in getattr(node.card, field_name) or []:
|
entity_values.extend(getattr(node.card, field_name) or [])
|
||||||
if isinstance(value, str) and value.strip():
|
|
||||||
entity_values.append(value.strip())
|
|
||||||
if entity_values:
|
|
||||||
out["实体"] = "; ".join(dict.fromkeys(entity_values))
|
|
||||||
|
|
||||||
text_values = [
|
out: dict[str, str] = {}
|
||||||
v.strip()
|
entities_text = _clean_join(entity_values)
|
||||||
for v in (getattr(node.card, "visible_text", None) or [])
|
if entities_text:
|
||||||
if isinstance(v, str) and v.strip()
|
out["实体"] = entities_text
|
||||||
]
|
visible_text = _clean_join(getattr(node.card, "visible_text", None))
|
||||||
if text_values:
|
if visible_text:
|
||||||
out["画面文字"] = "; ".join(dict.fromkeys(text_values))
|
out["画面文字"] = visible_text
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def search_similar(
|
def search_similar(
|
||||||
|
|||||||
Reference in New Issue
Block a user