(
self, text: str, source_id: str, source_column: str
)
| 114 | return str(source) |
| 115 | |
| 116 | def _chunk_by_words( |
| 117 | self, text: str, source_id: str, source_column: str |
| 118 | ) -> list[dict]: |
| 119 | words = text.split() |
| 120 | chunks = [] |
| 121 | |
| 122 | step = self.config.chunk_size - self.config.chunk_overlap |
| 123 | if step <= 0: |
| 124 | raise ValueError( |
| 125 | f"chunk_overlap ({self.config.chunk_overlap}) must be less than " |
| 126 | f"chunk_size ({self.config.chunk_size})" |
| 127 | ) |
| 128 | chunk_index = 0 |
| 129 | |
| 130 | for i in range(0, len(words), step): |
| 131 | chunk_words = words[i : i + self.config.chunk_size] |
| 132 | |
| 133 | if len(chunk_words) < self.config.min_chunk_size: |
| 134 | continue |
| 135 | |
| 136 | chunk_text = " ".join(chunk_words) |
| 137 | if self.config.max_chunk_chars: |
| 138 | chunk_text = chunk_text[: self.config.max_chunk_chars] |
| 139 | |
| 140 | chunks.append( |
| 141 | { |
| 142 | "chunk_id": f"{source_id}_{chunk_index}", |
| 143 | "original_id": source_id, |
| 144 | source_column: chunk_text, |
| 145 | "chunk_index": chunk_index, |
| 146 | } |
| 147 | ) |
| 148 | chunk_index += 1 |
| 149 | |
| 150 | return chunks |
no test coverage detected