Lesson 2 of 5 · 2 min
Chunking
Documents are cut into pieces so the right piece can be found later. Pieces that are too big blur many topics together. Pieces that are too small lose the sentence that gave them meaning. A little overlap stops an answer being cut in half.
def chunks(text, size=12, overlap=3):
words = text.split()
return [" ".join(words[i:i + size]) for i in range(0, len(words), size - overlap)]
text = "Refunds are accepted within thirty days. Damaged items can be returned at any time. Shipping fees are not refunded unless the error was ours."
for c in chunks(text):
print("-", c)
