|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any |
| 4 | + |
| 5 | +from yuxi.knowledge.chunking.ragflow_like import nlp |
| 6 | +from yuxi.knowledge.chunking.ragflow_like.parsers.general import _iter_sections, _unescape_delimiter |
| 7 | + |
| 8 | + |
| 9 | +def _slice_text_by_tokens(text: str, max_tokens: int, overlap_tokens: int) -> list[str]: |
| 10 | + if max_tokens <= 0: |
| 11 | + return [text] if text.strip() else [] |
| 12 | + |
| 13 | + units = [part for part in text] |
| 14 | + chunks: list[str] = [] |
| 15 | + start = 0 |
| 16 | + |
| 17 | + while start < len(units): |
| 18 | + current = "" |
| 19 | + current_tokens = 0 |
| 20 | + end = start |
| 21 | + |
| 22 | + while end < len(units): |
| 23 | + next_text = current + units[end] |
| 24 | + next_tokens = nlp.count_tokens(next_text) |
| 25 | + if current and next_tokens > max_tokens: |
| 26 | + break |
| 27 | + current = next_text |
| 28 | + current_tokens = next_tokens |
| 29 | + end += 1 |
| 30 | + if current_tokens >= max_tokens: |
| 31 | + break |
| 32 | + |
| 33 | + chunk = current.strip() |
| 34 | + if chunk: |
| 35 | + chunks.append(chunk) |
| 36 | + |
| 37 | + if end >= len(units): |
| 38 | + break |
| 39 | + |
| 40 | + if overlap_tokens <= 0: |
| 41 | + start = end |
| 42 | + continue |
| 43 | + |
| 44 | + backtrack = end |
| 45 | + overlap_text = "" |
| 46 | + while backtrack > start: |
| 47 | + candidate = units[backtrack - 1] + overlap_text |
| 48 | + if nlp.count_tokens(candidate) > overlap_tokens: |
| 49 | + break |
| 50 | + overlap_text = candidate |
| 51 | + backtrack -= 1 |
| 52 | + |
| 53 | + start = backtrack if backtrack < end else end |
| 54 | + |
| 55 | + return chunks |
| 56 | + |
| 57 | + |
| 58 | +def _split_section_with_overlap(section: str, chunk_token_num: int, overlapped_percent: int) -> list[str]: |
| 59 | + overlap_tokens = 0 |
| 60 | + if chunk_token_num > 0 and overlapped_percent > 0: |
| 61 | + overlap_tokens = int(chunk_token_num * max(0, min(overlapped_percent, 99)) / 100) |
| 62 | + return _slice_text_by_tokens(section, chunk_token_num, overlap_tokens) |
| 63 | + |
| 64 | + |
| 65 | +def chunk_markdown(markdown_content: str, parser_config: dict[str, Any] | None = None) -> list[str]: |
| 66 | + parser_config = parser_config or {} |
| 67 | + |
| 68 | + delimiter = _unescape_delimiter(str(parser_config.get("delimiter", "\n") or "\n")) |
| 69 | + chunk_token_num = int(parser_config.get("chunk_token_num", 512) or 512) |
| 70 | + overlapped_percent = int(parser_config.get("overlapped_percent", 0) or 0) |
| 71 | + |
| 72 | + sections = _iter_sections(markdown_content, delimiter) |
| 73 | + chunks: list[str] = [] |
| 74 | + |
| 75 | + for section, _ in sections: |
| 76 | + text = (section or "").strip() |
| 77 | + if not text: |
| 78 | + continue |
| 79 | + |
| 80 | + if chunk_token_num > 0 and nlp.count_tokens(text) > chunk_token_num: |
| 81 | + chunks.extend(_split_section_with_overlap(text, chunk_token_num, overlapped_percent)) |
| 82 | + continue |
| 83 | + |
| 84 | + chunks.append(text) |
| 85 | + |
| 86 | + return chunks |
0 commit comments