-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
268 lines (227 loc) · 6.19 KB
/
main.go
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
package chunker
import (
"fmt"
"regexp"
"sort"
"strings"
)
type Chunker struct {
ChunkSize int
Overlap int
Separators []string
OutputWithoutNewline bool
Debug bool
// internal
chunks []string
}
var (
DefaultSeparators = []string{"\n\n", " ", "\n"}
)
// ChunkSentences splits the input text into individual sentences.
// The sentences are delimited by periods, exclamation marks, and question marks.
// Do not split abbreviations like Mr. and Mrs.
//
// The input data is the text to be split into sentences.
// Returns a slice of strings, where each string is a sentence.
func ChunkSentences(data string) []string {
var sentences []string
// List of common abbreviations to avoid splitting on
abbreviations := []string{"Mr.", "Mrs.", "Dr.", "Ms.", "Jr.", "Sr.", "Prof.", "St."}
re := regexp.MustCompile(`([.?!])\s+`)
indexes := re.FindAllStringIndex(data, -1)
start := 0
for _, match := range indexes {
text := data[start : match[1]-1]
text = strings.TrimSpace(text)
// Check if the text ends with an abbreviation
split := true
for _, abbr := range abbreviations {
if strings.HasSuffix(text, abbr) {
split = false
break
}
}
if split {
sentences = append(sentences, text)
start = match[1]
}
}
// Add any remaining text as the last sentence
if start < len(data) {
text := data[start:]
text = strings.TrimSpace(text)
sentences = append(sentences, text)
}
return sentences
}
// NewChunker returns a new Chunker instance.
//
// The chunkSize parameter determines the maximum size of each chunk.
// The overlap parameter determines the overlap between chunks.
// The separators parameter determines the separators used to split the text.
// The outputWithoutNewline parameter determines whether newlines are removed from the output.
// The debug parameter determines whether debug mode is enabled.
// *Chunker
func NewChunker(chunkSize, overlap int, separators []string, outputWithoutNewline, debug bool) *Chunker {
if chunkSize <= 0 {
chunkSize = 150
}
if overlap <= 0 {
overlap = 30
}
if overlap >= chunkSize {
overlap = int(chunkSize / 4)
}
if len(separators) == 0 {
separators = DefaultSeparators
}
return &Chunker{
ChunkSize: chunkSize,
Overlap: overlap,
Separators: separators,
OutputWithoutNewline: outputWithoutNewline,
Debug: debug,
}
}
// Chunk chunks the given data into smaller parts based on the chunk size and overlap.
//
// The data parameter is the input string to be chunked.
// Returns a slice of strings representing the chunked data.
func (c *Chunker) Chunk(data string) []string {
c.ClearChunks()
var i int = 0
for {
if c.Debug {
fmt.Println("i: ", i, "len(data): ", len(data))
fmt.Println("chunks: ", len(c.chunks))
}
if i == 0 {
if len(data) < c.ChunkSize {
possibleChunk := data
c.addChunk(possibleChunk)
break
}
possibleChunk := data[:c.ChunkSize]
lastSeparator, ss := findLastSeparator(possibleChunk, c.Separators, 0)
possibleChunk = possibleChunk[:lastSeparator]
c.addChunk(possibleChunk)
i = lastSeparator + ss - c.Overlap
} else {
if len(data)-i < c.ChunkSize {
possibleChunk := data[i:]
firstSeparator := findFirstSeparator(possibleChunk, c.Separators)
if firstSeparator >= c.Overlap {
firstSeparator = 0
}
possibleChunk = possibleChunk[firstSeparator:]
c.addChunk(possibleChunk)
break
}
possibleChunk := data[i : i+c.ChunkSize]
firstSeparator := findFirstSeparator(possibleChunk, c.Separators)
if firstSeparator >= c.Overlap {
firstSeparator = 0
}
lastSeparator, ss := findLastSeparator(possibleChunk, c.Separators, firstSeparator)
possibleChunk = possibleChunk[firstSeparator:lastSeparator]
c.addChunk(possibleChunk)
if len(possibleChunk) > c.Overlap {
i += lastSeparator + ss - c.Overlap
} else {
if lastSeparator == 0 && ss == 0 {
i += c.Overlap
} else {
i += lastSeparator + ss
}
}
}
}
return c.GetChunks()
}
// ClearChunks clears the existing chunks in the Chunker.
//
// No parameters.
// No return value.
func (c *Chunker) ClearChunks() {
c.chunks = make([]string, 0)
}
// GetChunkSize returns the count on chunks.
//
// No parameters.
// Return value: int
func (c *Chunker) GetChunkSize() int {
return c.ChunkSize
}
// GetChunks returns the chunks of text that have been generated by the Chunker.
//
// No parameters.
// Returns a slice of strings representing the chunks of text.
func (c *Chunker) GetChunks() []string {
return c.chunks
}
func (c *Chunker) addChunk(chunk string) {
if c.OutputWithoutNewline {
chunk = removeNewlineInChunk(chunk)
}
chunk = strings.TrimSpace(chunk)
if len(chunk) == 0 {
return
}
c.chunks = append(c.chunks, chunk)
}
func findFirstSeparator(chunk string, separators []string) (offset int) {
pos := [][]int{}
for _, sp := range separators {
if len(chunk) >= len(sp) {
firstPos := strings.Index(chunk, sp)
if firstPos != -1 {
pos = append(pos, []int{firstPos, len(sp)})
continue
}
}
}
if len(pos) == 0 {
return 0
}
sort.Slice(pos, func(i, j int) bool {
return pos[i][0] < pos[j][0]
})
return pos[0][0] + pos[0][1]
}
func findLastSeparator(chunk string, separators []string, from int) (offset, separatorSize int) {
pos := [][]int{}
for _, sp := range separators {
if len(chunk) >= len(sp) {
lastPos := strings.LastIndex(chunk, sp)
if lastPos != -1 && lastPos > from {
pos = append(pos, []int{lastPos, len(sp)})
continue
}
}
}
if len(pos) == 0 {
return len(chunk), 0
}
sort.Slice(pos, func(i, j int) bool {
return pos[i][0] > pos[j][0]
})
return pos[0][0], pos[0][1]
}
func removeNewlineInChunk(chunk string) string {
if len(chunk) == 0 {
return chunk
}
// remove /n from the beginning of the chunk
if chunk[0] == '\n' {
chunk = chunk[1:]
}
// remove /n from the end of the chunk
if chunk[len(chunk)-1] == '\n' {
chunk = chunk[:len(chunk)-1]
}
// remove /n in the middle of the chunk, replace with space if it is not followed by a space
chunk = strings.ReplaceAll(chunk, "\n ", " ")
chunk = strings.ReplaceAll(chunk, " \n", " ")
chunk = strings.ReplaceAll(chunk, "\n", " ")
return chunk
}