-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk.go
More file actions
202 lines (169 loc) · 3.93 KB
/
chunk.go
File metadata and controls
202 lines (169 loc) · 3.93 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
package chunky
import (
"strings"
)
type Chunker interface {
Chunk(string) []string
}
type SentenceChunker struct {
separators []byte
chunkSize int
}
func NewSentenceChunker(chunkSize int) *SentenceChunker {
return &SentenceChunker{
separators: []byte{'\n', '.', '!', '?'},
chunkSize: chunkSize,
}
}
func (c *SentenceChunker) Chunk(text string) []string {
if text == "" {
return nil
}
data := []byte(text)
return chunkBytes(data, c.chunkSize, c.separators)
}
func chunkBytes(data []byte, blockSize int, separators []byte) []string {
var chunks [][]byte
i := 0
for i < len(data) {
end := i + blockSize
if end > len(data) {
end = len(data)
}
block := data[i:end]
found := -1
for j := len(block) - 1; j >= 0; j-- {
for _, d := range separators {
if block[j] == d {
found = j + 1
break
}
}
if found > 0 {
break
}
}
if found <= 0 {
found = len(block)
}
chunks = append(chunks, block[:found])
i += found
}
// convert to strings
out := make([]string, len(chunks))
for i, c := range chunks {
out[i] = string(c)
}
return out
}
type RecursiveCharacterTextSplitter struct {
separators []string
chunkSize int
chunkOverlap int
}
func NewRecursiveCharacterTextSplitter(chunkSize, chunkOverlap int) *RecursiveCharacterTextSplitter {
return &RecursiveCharacterTextSplitter{
separators: []string{"\n\n", "\n", " ", ""},
chunkSize: chunkSize,
chunkOverlap: chunkOverlap,
}
}
func (c *RecursiveCharacterTextSplitter) Chunk(text string) []string {
return c.splitText(text, c.separators)
}
func (c *RecursiveCharacterTextSplitter) splitText(text string, separators []string) []string {
sep, remaining := c.chooseSeparator(text, separators)
splits := filterEmpty(splitOnSeparator(text, sep))
var goodSplits []string
var chunks []string
for _, piece := range splits {
if len(piece) <= c.chunkSize {
goodSplits = append(goodSplits, piece)
continue
}
if len(goodSplits) > 0 {
chunks = append(chunks, c.mergeSplits(goodSplits, sep)...)
goodSplits = nil
}
if len(remaining) == 0 {
chunks = append(chunks, piece)
} else {
chunks = append(chunks, c.splitText(piece, remaining)...)
}
}
if len(goodSplits) > 0 {
chunks = append(chunks, c.mergeSplits(goodSplits, sep)...)
}
return chunks
}
func (c *RecursiveCharacterTextSplitter) chooseSeparator(text string, separators []string) (string, []string) {
for i, sep := range separators {
if sep == "" || strings.Contains(text, sep) {
return sep, separators[i+1:]
}
}
last := separators[len(separators)-1]
return last, nil
}
func joinedLen(pieces []string, sep string) int {
if len(pieces) == 0 {
return 0
}
total := 0
for _, p := range pieces {
total += len(p)
}
total += len(sep) * (len(pieces) - 1)
return total
}
func (c *RecursiveCharacterTextSplitter) mergeSplits(splits []string, sep string) []string {
joinSep := cleanJoinSep(sep)
var chunks []string
var current []string
for _, piece := range splits {
addLen := len(piece)
if len(current) > 0 {
addLen += len(joinSep)
}
if joinedLen(current, joinSep)+addLen > c.chunkSize && len(current) > 0 {
chunks = append(chunks, strings.Join(current, joinSep))
for len(current) > 0 && joinedLen(current, joinSep) > c.chunkOverlap {
current = current[1:]
}
}
current = append(current, piece)
}
if len(current) > 0 {
chunks = append(chunks, strings.Join(current, joinSep))
}
return chunks
}
func cleanJoinSep(sep string) string {
if strings.Contains(sep, "\n") {
return "\n"
}
if sep == " " {
return " "
}
return ""
}
func filterEmpty(ss []string) []string {
out := ss[:0]
for _, s := range ss {
if strings.TrimSpace(s) != "" {
out = append(out, s)
}
}
return out
}
func splitOnSeparator(text, sep string) []string {
if sep == "" {
runes := []rune(text)
out := make([]string, len(runes))
for i, r := range runes {
out[i] = string(r)
}
return out
}
return strings.Split(text, sep)
}