feat(14-04): 实现长文本分块处理方法
- 添加 _split_into_chunks() 方法,按句子边界分割长文本(5000字符阈值) - 添加 _merge_segment_results() 方法,合并分块结果并修正索引 - 添加 _segment_single_chunk() 方法,处理单个文本块 - 添加 _segment_multiple_chunks() 方法,处理多个文本块(分块) - 添加 _fallback_chunk() 方法,用于单个分块失败时的降级处理 - 修改 segment() 方法,添加分块判断逻辑(len(content) > 5000) 遵守用户决策: - 分块阈值: 5000 字符 - 按句子边界分割(。!?\n) - 直接拼接各分块结果 - start_index/end_index 相对于原文 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -567,24 +567,44 @@ class UnifiedSegmentationStrategy:
|
|||||||
return all_segments
|
return all_segments
|
||||||
|
|
||||||
def _fallback_chunk(self, chunk_text: str, offset: int) -> List[VideoSegment]:
|
def _fallback_chunk(self, chunk_text: str, offset: int) -> List[VideoSegment]:
|
||||||
"""对单个分块使用规则分段
|
"""对单个分块使用规则分段(降级)
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
chunk_text: 分块文本
|
chunk_text: 分块文本
|
||||||
offset: 该块在原文中的起始位置
|
offset: 该分块在原文中的起始位置
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
VideoSegment 对象列表
|
VideoSegment 对象列表
|
||||||
"""
|
"""
|
||||||
# 简单实现:将整个块作为一个分段
|
# 创建虚拟的分段对象用于规则分段器
|
||||||
return [
|
# 规则分段器需要 Segment 对象列表,我们创建一个包含整个文本的虚拟分段
|
||||||
VideoSegment(
|
from kb_workflow.srt.parser import Segment
|
||||||
|
|
||||||
|
virtual_segment = Segment(
|
||||||
|
index=1,
|
||||||
|
start_time=0.0,
|
||||||
|
end_time=1.0,
|
||||||
|
text=chunk_text
|
||||||
|
)
|
||||||
|
|
||||||
|
# 使用规则分段器
|
||||||
|
paragraphs = self.fallback_segmenter.segment([virtual_segment])
|
||||||
|
|
||||||
|
# 转换为 VideoSegment,并设置正确的偏移量
|
||||||
|
segments = []
|
||||||
|
current_pos = offset
|
||||||
|
for para in paragraphs:
|
||||||
|
# 计算该分段在原文中的位置
|
||||||
|
seg = VideoSegment(
|
||||||
title=None,
|
title=None,
|
||||||
content=chunk_text,
|
content=para,
|
||||||
start_index=offset,
|
start_index=current_pos,
|
||||||
end_index=offset + len(chunk_text)
|
end_index=current_pos + len(para)
|
||||||
)
|
)
|
||||||
]
|
segments.append(seg)
|
||||||
|
current_pos += len(para)
|
||||||
|
|
||||||
|
return segments
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
|
|||||||
Reference in New Issue
Block a user