forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursiveCharacterTextSplitter.swift
More file actions
114 lines (103 loc) · 4.14 KB
/
RecursiveCharacterTextSplitter.swift
File metadata and controls
114 lines (103 loc) · 4.14 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
import Foundation
/// Implementation of splitting text that looks at characters.
/// Recursively tries to split by different characters to find one that works.
public class RecursiveCharacterTextSplitter: TextSplitter {
public var chunkSize: Int
public var chunkOverlap: Int
public var lengthFunction: (String) -> Int
/// A list of separators to try. They will be used in order. Supports regular expressions.
public var separators: [String]
/// Create a new splitter
/// - Parameters:
/// - separators: A list of separators to try. They will be used in order. Supports regular
/// expressions.
/// - chunkSize: The maximum size of chunks. Don't use chunk size larger than 8191, because
/// length safe embedding is not implemented.
/// - chunkOverlap: The maximum overlap between chunks.
/// - lengthFunction: A function to compute the length of text.
public init(
separators: [String] = ["\n\n", "\n", " ", ""],
chunkSize: Int = 4000,
chunkOverlap: Int = 200,
lengthFunction: @escaping (String) -> Int = { $0.count }
) {
assert(chunkOverlap <= chunkSize)
self.chunkSize = chunkSize
self.chunkOverlap = chunkOverlap
self.lengthFunction = lengthFunction
self.separators = separators
}
// Create a new splitter
/// - Parameters:
/// - separatorSet: A set of separators to try.
/// - chunkSize: The maximum size of chunks. Don't use chunk size larger than 8191, because
/// length safe embedding is not implemented.
/// - chunkOverlap: The maximum overlap between chunks.
/// - lengthFunction: A function to compute the length of text.
public init(
separatorSet: TextSplitterSeparatorSet,
chunkSize: Int = 4000,
chunkOverlap: Int = 200,
lengthFunction: @escaping (String) -> Int = { $0.count }
) {
assert(chunkOverlap <= chunkSize)
self.chunkSize = chunkSize
self.chunkOverlap = chunkOverlap
self.lengthFunction = lengthFunction
separators = separatorSet.separators
}
public func split(text: String) async throws -> [String] {
return split(text: text, separators: separators)
}
private func split(text: String, separators: [String]) -> [String] {
var finalChunks = [String]()
// Get appropriate separator to use
let firstSeparatorIndex = separators.firstIndex {
let pattern = "(\($0))"
guard let regex = try? NSRegularExpression(pattern: pattern) else { return false }
return regex.firstMatch(
in: text,
options: [],
range: NSRange(text.startIndex..., in: text)
) != nil
}
var separator: String
var nextSeparators: [String]
if let index = firstSeparatorIndex {
separator = separators[index]
if index < separators.endIndex - 1 {
nextSeparators = Array(separators[(index + 1)...])
} else {
nextSeparators = []
}
} else {
separator = ""
nextSeparators = []
}
let splits = split(text: text, separator: separator)
// Now go merging things, recursively splitting longer texts.
var goodSplits = [String]()
for s in splits {
if lengthFunction(s) < chunkSize {
goodSplits.append(s)
} else {
if !goodSplits.isEmpty {
let mergedText = mergeSplits(goodSplits)
finalChunks.append(contentsOf: mergedText)
goodSplits.removeAll()
}
if nextSeparators.isEmpty {
finalChunks.append(s)
} else {
let other_info = split(text: s, separators: nextSeparators)
finalChunks.append(contentsOf: other_info)
}
}
}
if !goodSplits.isEmpty {
let merged_text = mergeSplits(goodSplits)
finalChunks.append(contentsOf: merged_text)
}
return finalChunks
}
}