-
Notifications
You must be signed in to change notification settings - Fork 3k
/
text_chunker.py
323 lines (269 loc) · 8.27 KB
/
text_chunker.py
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# Copyright (c) Microsoft. All rights reserved.
"""
Split text in chunks, attempting to leave meaning intact.
For plain text, split looking at new lines first, then periods, and so on.
For markdown, split looking at punctuation first, and so on.
"""
import os
import re
from collections.abc import Callable
NEWLINE = os.linesep
TEXT_SPLIT_OPTIONS = [
["\n", "\r"],
["."],
["?", "!"],
[";"],
[":"],
[","],
[")", "]", "}"],
[" "],
["-"],
None,
]
MD_SPLIT_OPTIONS = [
["."],
["?", "!"],
[";"],
[":"],
[","],
[")", "]", "}"],
[" "],
["-"],
["\n", "\r"],
None,
]
def _token_counter(text: str) -> int:
"""
Count the number of tokens in a string.
TODO: chunking methods should be configurable to allow for different
tokenization strategies depending on the model to be called.
For now, we use an extremely rough estimate.
"""
return len(text) // 4
def split_plaintext_lines(text: str, max_token_per_line: int, token_counter: Callable = _token_counter) -> list[str]:
"""
Split plain text into lines.
it will split on new lines first, and then on punctuation.
"""
return _split_text_lines(
text=text,
max_token_per_line=max_token_per_line,
trim=True,
token_counter=token_counter,
)
def split_markdown_lines(text: str, max_token_per_line: int, token_counter: Callable = _token_counter) -> list[str]:
"""
Split markdown into lines.
It will split on punctuation first, and then on space and new lines.
"""
return _split_markdown_lines(
text=text,
max_token_per_line=max_token_per_line,
trim=True,
token_counter=token_counter,
)
def split_plaintext_paragraph(text: list[str], max_tokens: int, token_counter: Callable = _token_counter) -> list[str]:
"""
Split plain text into paragraphs.
"""
split_lines = []
for line in text:
split_lines.extend(
_split_text_lines(
text=line,
max_token_per_line=max_tokens,
trim=True,
token_counter=token_counter,
)
)
return _split_text_paragraph(text=split_lines, max_tokens=max_tokens, token_counter=token_counter)
def split_markdown_paragraph(text: list[str], max_tokens: int, token_counter: Callable = _token_counter) -> list[str]:
"""
Split markdown into paragraphs.
"""
split_lines = []
for line in text:
split_lines.extend(
_split_markdown_lines(
text=line,
max_token_per_line=max_tokens,
trim=False,
token_counter=token_counter,
)
)
return _split_text_paragraph(text=split_lines, max_tokens=max_tokens, token_counter=token_counter)
def _split_text_paragraph(text: list[str], max_tokens: int, token_counter: Callable = _token_counter) -> list[str]:
"""
Split text into paragraphs.
"""
if not text:
return []
paragraphs = []
current_paragraph = []
for line in text:
num_tokens_line = token_counter(line)
num_tokens_paragraph = token_counter("".join(current_paragraph))
if num_tokens_paragraph + num_tokens_line + 1 >= max_tokens and len(current_paragraph) > 0:
paragraphs.append("".join(current_paragraph).strip())
current_paragraph = []
current_paragraph.append(f"{line}{NEWLINE}")
if len(current_paragraph) > 0:
paragraphs.append("".join(current_paragraph).strip())
current_paragraph = []
# Distribute text more evenly in the last paragraphs
# when the last paragraph is too short.
if len(paragraphs) > 1:
last_para = paragraphs[-1]
sec_last_para = paragraphs[-2]
if token_counter(last_para) < max_tokens / 4:
last_para_tokens = last_para.split(" ")
sec_last_para_tokens = sec_last_para.split(" ")
last_para_token_count = len(last_para_tokens)
sec_last_para_token_count = len(sec_last_para_tokens)
if last_para_token_count + sec_last_para_token_count <= max_tokens:
sec_last_para = " ".join(sec_last_para_tokens) + NEWLINE
last_para = " ".join(last_para_tokens)
new_sec_last_para = sec_last_para + last_para
paragraphs[-2] = new_sec_last_para.strip()
paragraphs.pop()
return paragraphs
def _split_markdown_lines(
text: str,
max_token_per_line: int,
trim: bool,
token_counter: Callable = _token_counter,
) -> list[str]:
"""
Split markdown into lines.
"""
return _split_str_lines(
text=text,
max_tokens=max_token_per_line,
separators=MD_SPLIT_OPTIONS,
trim=trim,
token_counter=token_counter,
)
def _split_text_lines(
text: str,
max_token_per_line: int,
trim: bool,
token_counter: Callable = _token_counter,
) -> list[str]:
"""
Split text into lines.
"""
return _split_str_lines(
text=text,
max_tokens=max_token_per_line,
separators=TEXT_SPLIT_OPTIONS,
trim=trim,
token_counter=token_counter,
)
def _split_str_lines(
text: str,
max_tokens: int,
separators: list[list[str]],
trim: bool,
token_counter: Callable = _token_counter,
) -> list[str]:
if not text:
return []
text = text.replace("\r\n", "\n")
lines = []
was_split = False
for split_option in separators:
if not lines:
lines, was_split = _split_str(
text=text,
max_tokens=max_tokens,
separators=split_option,
trim=trim,
token_counter=token_counter,
)
else:
lines, was_split = _split_list(
text=lines,
max_tokens=max_tokens,
separators=split_option,
trim=trim,
token_counter=token_counter,
)
if was_split:
break
return lines
def _split_str(
text: str,
max_tokens: int,
separators: list[str],
trim: bool,
token_counter: Callable = _token_counter,
) -> tuple[list[str], bool]:
"""
Split text into lines.
"""
input_was_split = False
if not text:
return [], input_was_split
if trim:
text = text.strip()
text_as_is = [text]
if token_counter(text) <= max_tokens:
return text_as_is, input_was_split
half = len(text) // 2
cutpoint = -1
if not separators:
cutpoint = half
elif set(separators) & set(text) and len(text) > 2:
regex_separators = re.compile("|".join(re.escape(s) for s in separators))
min_dist = half
for match in re.finditer(regex_separators, text):
end = match.end()
dist = abs(half - end)
if dist < min_dist:
min_dist = dist
cutpoint = end
elif end > half:
# distance is increasing, so we can stop searching
break
else:
return text_as_is, input_was_split
if 0 < cutpoint < len(text):
lines = []
for text_part in [text[:cutpoint], text[cutpoint:]]:
split, has_split = _split_str(
text=text_part,
max_tokens=max_tokens,
separators=separators,
trim=trim,
token_counter=token_counter,
)
lines.extend(split)
input_was_split = input_was_split or has_split
else:
return text_as_is, input_was_split
return lines, input_was_split
def _split_list(
text: list[str],
max_tokens: int,
separators: list[str],
trim: bool,
token_counter: Callable = _token_counter,
) -> tuple[list[str], bool]:
"""
Split list of string into lines.
"""
if not text:
return [], False
lines = []
input_was_split = False
for line in text:
split_str, was_split = _split_str(
text=line,
max_tokens=max_tokens,
separators=separators,
trim=trim,
token_counter=token_counter,
)
lines.extend(split_str)
input_was_split = input_was_split or was_split
return lines, input_was_split