baran 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -1
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/baran/character_text_splitter.rb +3 -1
- data/lib/baran/recursive_character_text_splitter.rb +14 -22
- data/lib/baran/text_splitter.rb +14 -20
- data/lib/baran/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8342de6adf861cc80d2e3b2b409e607b7b6a4a9eded4ce6218710ba9fa66d164
|
4
|
+
data.tar.gz: 2b50b7070a46aadc2f2a6727e9418134d6123dda81c5f3b9f21787e500142b7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '05859643d5c56270306611402878d0290658c327d086cc0fc3faf3098ada35a9efaa0a2bd027a848ef84157ba82014438e9ceab30139a2d56e1fe1223d8a4e5d'
|
7
|
+
data.tar.gz: b778120886153d0ad29eceb118534ef65ab68de7c6c956d8752d0c78b2aec947831b5a73ba75a202e2b8980a68c5f6d68bda69199b6be6f21657f3b6250b6941
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -41,7 +41,7 @@ Splitting by the specified characters recursively.
|
|
41
41
|
|
42
42
|
```ruby
|
43
43
|
splitter = Baran::RecursiveCharacterTextSplitter.new(
|
44
|
-
separators: ["\
|
44
|
+
separators: ["\n\n", "\n", " ", ""]
|
45
45
|
)
|
46
46
|
splitter.chunks(text)
|
47
47
|
# => [{ cursor: 0, text: "..." }, ...]
|
@@ -2,13 +2,15 @@ require_relative './text_splitter'
|
|
2
2
|
|
3
3
|
module Baran
|
4
4
|
class CharacterTextSplitter < TextSplitter
|
5
|
+
attr_accessor :separator
|
6
|
+
|
5
7
|
def initialize(chunk_size: 1024, chunk_overlap: 64, separator: nil)
|
6
8
|
super(chunk_size: chunk_size, chunk_overlap: chunk_overlap)
|
7
9
|
@separator = separator || "\n\n"
|
8
10
|
end
|
9
11
|
|
10
12
|
def splitted(text)
|
11
|
-
splits =
|
13
|
+
splits = separator.empty? ? text.chars : text.split(separator)
|
12
14
|
merged(splits, @separator)
|
13
15
|
end
|
14
16
|
end
|
@@ -6,46 +6,38 @@ module Baran
|
|
6
6
|
|
7
7
|
def initialize(chunk_size: 1024, chunk_overlap: 64, separators: nil)
|
8
8
|
super(chunk_size: chunk_size, chunk_overlap: chunk_overlap)
|
9
|
-
@separators = separators || ["\n\n", "\n", " "
|
9
|
+
@separators = separators || ["\n\n", "\n", " "]
|
10
10
|
end
|
11
11
|
|
12
12
|
def splitted(text)
|
13
|
-
|
13
|
+
results = []
|
14
|
+
good_splits = []
|
15
|
+
separator = ''
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
if s.empty?
|
18
|
-
separator = s
|
19
|
-
break
|
20
|
-
elsif text.include?(s)
|
17
|
+
separators.each do |s|
|
18
|
+
if text.include?(s)
|
21
19
|
separator = s
|
22
20
|
break
|
23
21
|
end
|
24
22
|
end
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
good_splits = []
|
29
|
-
splits.each do |s|
|
30
|
-
if s.length < @chunk_size
|
24
|
+
text.split(separator).each do |s|
|
25
|
+
if s.length < chunk_size
|
31
26
|
good_splits << s
|
32
27
|
else
|
33
|
-
|
34
|
-
|
35
|
-
final_chunks.concat(merged_text)
|
28
|
+
if good_splits.length.positive?
|
29
|
+
results += merged(good_splits, separator)
|
36
30
|
good_splits.clear
|
37
31
|
end
|
38
|
-
|
39
|
-
final_chunks.concat(other_info)
|
32
|
+
results += splitted(s)
|
40
33
|
end
|
41
34
|
end
|
42
35
|
|
43
|
-
|
44
|
-
|
45
|
-
final_chunks.concat(merged_text)
|
36
|
+
if good_splits.length.positive?
|
37
|
+
results += merged(good_splits, separator)
|
46
38
|
end
|
47
39
|
|
48
|
-
|
40
|
+
results
|
49
41
|
end
|
50
42
|
end
|
51
43
|
end
|
data/lib/baran/text_splitter.rb
CHANGED
@@ -24,39 +24,33 @@ module Baran
|
|
24
24
|
chunks
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
text =
|
27
|
+
def joined(items, separator)
|
28
|
+
text = items.join(separator).strip
|
29
29
|
text.empty? ? nil : text
|
30
30
|
end
|
31
31
|
|
32
32
|
def merged(splits, separator)
|
33
|
-
|
34
|
-
|
33
|
+
results = [] # Array of strings
|
34
|
+
current_splits = [] # Array of strings
|
35
35
|
total = 0
|
36
36
|
|
37
|
-
splits.each do |
|
38
|
-
|
37
|
+
splits.each do |split|
|
38
|
+
if total + split.length >= chunk_size && current_splits.length.positive?
|
39
|
+
results << joined(current_splits, separator)
|
39
40
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
docs.push(doc) unless doc.nil?
|
44
|
-
|
45
|
-
while total > @chunk_overlap || (total + len > @chunk_size && total.positive?)
|
46
|
-
total -= current_doc.first.length
|
47
|
-
current_doc.shift
|
48
|
-
end
|
41
|
+
while total > chunk_overlap || (total + split.length > chunk_size && total.positive?)
|
42
|
+
total -= current_splits.first.length
|
43
|
+
current_splits.shift
|
49
44
|
end
|
50
45
|
end
|
51
46
|
|
52
|
-
|
53
|
-
total +=
|
47
|
+
current_splits << split
|
48
|
+
total += split.length
|
54
49
|
end
|
55
50
|
|
56
|
-
|
57
|
-
docs.push(doc) unless doc.nil?
|
51
|
+
results << joined(current_splits, separator)
|
58
52
|
|
59
|
-
|
53
|
+
results
|
60
54
|
end
|
61
55
|
end
|
62
56
|
end
|
data/lib/baran/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: baran
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Moeki Kawakami
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-06-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Text Splitter for Large Language Model Datasets.
|
14
14
|
email:
|