-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_chunker.py
More file actions
230 lines (187 loc) · 7.85 KB
/
Copy pathtext_chunker.py
File metadata and controls
230 lines (187 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
"""
Smart Text Chunker for Long Document Translation
Handles texts of any length by intelligently splitting at natural boundaries
"""
import re
from typing import List, Tuple
class SmartTextChunker:
"""
Intelligently chunks long texts for translation while preserving:
- Paragraph boundaries
- Sentence boundaries
- Context and coherence
"""
def __init__(self, max_tokens=400, tokenizer=None):
"""
Initialize the text chunker.
Args:
max_tokens: Target maximum tokens per chunk (default 400, safe margin from 512)
tokenizer: Optional tokenizer for accurate token counting
"""
self.max_tokens = max_tokens
self.tokenizer = tokenizer
def estimate_tokens(self, text: str) -> int:
"""
Estimate token count for text.
If tokenizer available, use it. Otherwise estimate:
- 1 token ≈ 0.75 words for European languages
- 1 token ≈ 4 characters
"""
if self.tokenizer:
try:
tokens = self.tokenizer(text, return_tensors="pt", add_special_tokens=False)
return len(tokens['input_ids'][0])
except:
pass
# Fallback: estimate based on words
word_count = len(text.split())
# Conservative estimate: 1.3 tokens per word (safer than 0.75 words per token)
return int(word_count * 1.3)
def split_paragraphs(self, text: str) -> List[str]:
"""Split text into paragraphs."""
# Split on double newlines or more
paragraphs = re.split(r'\n\s*\n', text)
return [p.strip() for p in paragraphs if p.strip()]
def split_sentences(self, text: str) -> List[str]:
"""Split text into sentences."""
# Enhanced sentence splitting pattern
# Handles: . ! ? followed by space and capital letter, or end of string
pattern = r'(?<=[.!?])\s+(?=[A-Z])|(?<=[.!?])$'
sentences = re.split(pattern, text)
return [s.strip() for s in sentences if s.strip()]
def split_by_tokens(self, text: str, max_tokens: int) -> List[str]:
"""
Split text into chunks of approximately max_tokens.
Tries to split at sentence boundaries if possible.
"""
sentences = self.split_sentences(text)
chunks = []
current_chunk = []
current_tokens = 0
for sentence in sentences:
sentence_tokens = self.estimate_tokens(sentence)
# If single sentence is too long, split it further
if sentence_tokens > max_tokens:
# Save current chunk if any
if current_chunk:
chunks.append(' '.join(current_chunk))
current_chunk = []
current_tokens = 0
# Split long sentence by words
words = sentence.split()
temp_chunk = []
temp_tokens = 0
for word in words:
word_tokens = self.estimate_tokens(word)
if temp_tokens + word_tokens > max_tokens and temp_chunk:
chunks.append(' '.join(temp_chunk))
temp_chunk = [word]
temp_tokens = word_tokens
else:
temp_chunk.append(word)
temp_tokens += word_tokens
if temp_chunk:
chunks.append(' '.join(temp_chunk))
# If adding this sentence would exceed limit, start new chunk
elif current_tokens + sentence_tokens > max_tokens and current_chunk:
chunks.append(' '.join(current_chunk))
current_chunk = [sentence]
current_tokens = sentence_tokens
else:
current_chunk.append(sentence)
current_tokens += sentence_tokens
# Add remaining chunk
if current_chunk:
chunks.append(' '.join(current_chunk))
return chunks
def chunk_text(self, text: str) -> List[Tuple[str, str]]:
"""
Main method: Intelligently chunk text of any length.
Strategy:
1. Check if text fits in one chunk → return as-is
2. Try splitting by paragraphs
3. If paragraphs too long, split by sentences
4. If sentences too long, split by words
Returns:
List of (chunk_text, chunk_type) tuples
chunk_type: 'full', 'paragraph', 'sentence', 'word-split'
"""
text = text.strip()
if not text:
return []
# Check if entire text fits
total_tokens = self.estimate_tokens(text)
if total_tokens <= self.max_tokens:
return [(text, 'full')]
# Split into paragraphs first
paragraphs = self.split_paragraphs(text)
chunks = []
for para in paragraphs:
para_tokens = self.estimate_tokens(para)
# If paragraph fits, keep it whole
if para_tokens <= self.max_tokens:
chunks.append((para, 'paragraph'))
# Otherwise, split paragraph into smaller chunks
else:
para_chunks = self.split_by_tokens(para, self.max_tokens)
for chunk in para_chunks:
chunk_tokens = self.estimate_tokens(chunk)
if chunk_tokens <= self.max_tokens:
chunks.append((chunk, 'sentence'))
else:
# Very long sentence, had to split by words
chunks.append((chunk, 'word-split'))
return chunks
def get_stats(self, text: str) -> dict:
"""Get statistics about text chunking."""
chunks = self.chunk_text(text)
return {
'total_chars': len(text),
'estimated_tokens': self.estimate_tokens(text),
'num_chunks': len(chunks),
'chunk_types': {
'full': sum(1 for _, t in chunks if t == 'full'),
'paragraph': sum(1 for _, t in chunks if t == 'paragraph'),
'sentence': sum(1 for _, t in chunks if t == 'sentence'),
'word-split': sum(1 for _, t in chunks if t == 'word-split')
},
'chunks': [
{
'text_preview': chunk[:100] + '...' if len(chunk) > 100 else chunk,
'type': chunk_type,
'chars': len(chunk),
'estimated_tokens': self.estimate_tokens(chunk)
}
for chunk, chunk_type in chunks
]
}
def demo():
"""Demo the text chunker."""
chunker = SmartTextChunker(max_tokens=100)
test_texts = [
# Short text
"Hello, how are you?",
# Multi-paragraph
"""This is the first paragraph. It contains multiple sentences. Each sentence adds information.
This is the second paragraph. It also has several sentences. They flow naturally together.
And here is a third paragraph to test the chunking.""",
# Very long text
" ".join(["This is sentence number {}.".format(i) for i in range(1, 51)])
]
for i, text in enumerate(test_texts, 1):
print(f"\n{'='*70}")
print(f"TEST {i}")
print(f"{'='*70}")
print(f"Input length: {len(text)} chars")
chunks = chunker.chunk_text(text)
stats = chunker.get_stats(text)
print(f"\nChunks: {stats['num_chunks']}")
print(f"Estimated tokens: {stats['estimated_tokens']}")
print(f"\nChunk breakdown:")
for j, (chunk, chunk_type) in enumerate(chunks, 1):
print(f" [{j}] {chunk_type}: {len(chunk)} chars, ~{chunker.estimate_tokens(chunk)} tokens")
preview = chunk[:80] + '...' if len(chunk) > 80 else chunk
print(f" \"{preview}\"")
if __name__ == "__main__":
demo()