MCPcopy Create free account
hub / github.com/OpenRaiser/PaperFlow / _chunk_text_with_overlap

Function _chunk_text_with_overlap

agents/reading-agent/main.py:3153–3211  ·  view source on GitHub ↗
(
    text: str,
    section: str,
    max_chars: int = READING_REPORT_CHUNK_CHARS,
    overlap: int = READING_REPORT_CHUNK_OVERLAP,
)

Source from the content-addressed store, hash-verified

3151
3152def _chunk_text_with_overlap(
3153 text: str,
3154 section: str,
3155 max_chars: int = READING_REPORT_CHUNK_CHARS,
3156 overlap: int = READING_REPORT_CHUNK_OVERLAP,
3157) -> List[Dict[str, Any]]:
3158 normalized = _clean_pdf_evidence_text(text)
3159 if not normalized:
3160 return []
3161
3162 if max_chars <= 0:
3163 max_chars = READING_REPORT_CHUNK_CHARS
3164 overlap = max(0, min(overlap, max_chars // 2 if max_chars > 1 else 0))
3165
3166 chunks: List[Dict[str, Any]] = []
3167 start = 0
3168 text_length = len(normalized)
3169 while start < text_length:
3170 end = min(text_length, start + max_chars)
3171 if end < text_length:
3172 window = normalized[start:end]
3173 boundary_candidates = [
3174 window.rfind("\n\n"),
3175 window.rfind(". "),
3176 window.rfind("? "),
3177 window.rfind("! "),
3178 window.rfind("; "),
3179 window.rfind("。"),
3180 window.rfind("!"),
3181 window.rfind("?"),
3182 window.rfind(";"),
3183 ]
3184 boundary = max(boundary_candidates)
3185 if boundary >= max_chars // 2:
3186 boundary_len = 2 if window[boundary:boundary + 2] in {". ", "? ", "! ", "; ", "\n\n"} else 1
3187 end = start + boundary + boundary_len
3188
3189 chunk_text = normalized[start:end].strip()
3190 chunk_text = _clean_pdf_evidence_text(chunk_text)
3191 if chunk_text and not _is_noisy_pdf_evidence_text(chunk_text):
3192 chunks.append(
3193 {
3194 "section": _clean_text(section) or "full_text",
3195 "text": chunk_text,
3196 "start": start,
3197 "end": end,
3198 "chunk_index": len(chunks),
3199 }
3200 )
3201
3202 if end >= text_length:
3203 break
3204
3205 next_start = max(end - overlap, start + 1)
3206 if next_start <= start:
3207 next_start = end
3208 start = next_start
3209
3210 return chunks

Callers 1

_build_pdf_chunksFunction · 0.85

Calls 3

_clean_pdf_evidence_textFunction · 0.85
_clean_textFunction · 0.70

Tested by

no test coverage detected