-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk.js
More file actions
64 lines (55 loc) · 1.68 KB
/
Copy pathchunk.js
File metadata and controls
64 lines (55 loc) · 1.68 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
/**
* Chunk text into smaller pieces
* @param {string} text - Text to chunk
* @param {Object} options - Chunking options
* @returns {string[]}
*/
export function chunk(text, options = {}) {
const {
size = 500, // Character count per chunk
overlap = 0, // Overlap between chunks (in characters)
strategy = "character", // "character", "sentence", "word"
} = options;
if (!text || text.length === 0) {
return [];
}
const chunks = [];
if (strategy === "character") {
const step = Math.max(1, size - overlap);
for (let i = 0; i < text.length; i += step) {
chunks.push(text.slice(i, i + size));
}
} else if (strategy === "sentence") {
const sentences = text.match(/[^.!?]+[.!?]+/g) || [text];
let currentChunk = "";
for (const sentence of sentences) {
if (currentChunk.length + sentence.length > size && currentChunk) {
chunks.push(currentChunk.trim());
currentChunk = sentence;
} else {
currentChunk += sentence;
}
}
if (currentChunk.trim()) {
chunks.push(currentChunk.trim());
}
} else if (strategy === "word") {
const words = text.split(/\s+/);
let currentChunk = [];
let currentSize = 0;
for (const word of words) {
if (currentSize + word.length > size && currentChunk.length > 0) {
chunks.push(currentChunk.join(" "));
currentChunk = [word];
currentSize = word.length;
} else {
currentChunk.push(word);
currentSize += word.length + 1; // +1 for space
}
}
if (currentChunk.length > 0) {
chunks.push(currentChunk.join(" "));
}
}
return chunks.filter(ch => ch.length > 0);
}