fix(data-process): 合并文档结构短切片
This commit is contained in:
@@ -256,7 +256,7 @@ def _merge_short_chunks(
|
||||
min_token_count: int,
|
||||
chunk_size: int,
|
||||
) -> list[TextChunk]:
|
||||
"""在同一章节内合并短块,同时严格遵守切片大小上限。"""
|
||||
"""按原文顺序合并相邻短块,同时严格遵守切片大小上限。"""
|
||||
|
||||
merged: list[TextChunk] = []
|
||||
index = 0
|
||||
@@ -308,7 +308,7 @@ def _chunk_source_text(
|
||||
config: dict[str, Any],
|
||||
preprocess_options: set[str],
|
||||
) -> list[tuple[TextChunk, tuple[str, ...]]]:
|
||||
"""按可选文档结构分段后切片,章节之间绝不共享 overlap。"""
|
||||
"""按可选文档结构分段后切片,结构边界之间不共享 overlap。"""
|
||||
|
||||
method = str(_value(config, "chunk_method", "chunkMethod", "structure"))
|
||||
detect_structure = (
|
||||
@@ -368,14 +368,19 @@ def _chunk_source_text(
|
||||
section_text = text[start:end]
|
||||
local_chunks = chunk_unstructured(section_text, **common)
|
||||
shifted = [_shift_chunk(chunk, start, text) for chunk in local_chunks]
|
||||
if merge_short:
|
||||
shifted = _merge_short_chunks(
|
||||
shifted,
|
||||
text,
|
||||
min_token_count=configured_minimum,
|
||||
chunk_size=chunk_size,
|
||||
)
|
||||
result.extend((chunk, heading_path) for chunk in shifted)
|
||||
|
||||
if merge_short and result:
|
||||
# 结构分段只负责提供标题路径和隔离 overlap,不应让目录项或短小节
|
||||
# 突破 min_chunk_size 约束。合并后保留首个原始块的标题路径。
|
||||
heading_paths = {chunk.start: heading_path for chunk, heading_path in result}
|
||||
merged = _merge_short_chunks(
|
||||
[chunk for chunk, _ in result],
|
||||
text,
|
||||
min_token_count=configured_minimum,
|
||||
chunk_size=chunk_size,
|
||||
)
|
||||
result = [(chunk, heading_paths.get(chunk.start, ())) for chunk in merged]
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user