semantic_chunks 0.1.0
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +46 -0
- data/exe/semantic-chunks +23 -0
- data/lib/semantic_chunks/chunk.rb +15 -0
- data/lib/semantic_chunks/splitter.rb +143 -0
- data/lib/semantic_chunks/version.rb +3 -0
- data/lib/semantic_chunks.rb +11 -0
- metadata +82 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: f68092098bba7fb9d5d521e5bda9cc7ccce6e668ecf90a3c872932a3528a7700
|
|
4
|
+
data.tar.gz: bfc7c91df83ebf5caa23187d35e4d459fed38470369e798089ec4bb0a3e6a5ae
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 71bd25566777edace169e54c3013dddd5452e66fe1643b0350351ff18a955488751b5baad789d6255eeafb1b2ba8169a71c719be7a1bfe796e0019bf27ae4eac
|
|
7
|
+
data.tar.gz: a24da65e4102fed7f7cec62308a7a247f642cd15581f15e308fdd6ace77c69cf5863ce45e2127f464873d2ba51175309273fdd49622ca3e3d92f423a707f6b3d
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 iamzayn19
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# SemanticChunks
|
|
2
|
+
|
|
3
|
+
SemanticChunks is a pure Ruby text chunker for RAG, search indexing, and LLM apps.
|
|
4
|
+
|
|
5
|
+
It solves a common Ruby AI problem: document preparation often gets pushed to Python just to split text into useful chunks. This gem keeps that step in Ruby.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
gem install semantic_chunks
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
require "semantic_chunks"
|
|
17
|
+
|
|
18
|
+
chunks = SemanticChunks.split(markdown, max_tokens: 300, overlap: 40)
|
|
19
|
+
|
|
20
|
+
chunks.each do |chunk|
|
|
21
|
+
puts chunk.text
|
|
22
|
+
p chunk.metadata
|
|
23
|
+
end
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Each chunk includes:
|
|
27
|
+
|
|
28
|
+
- `text`
|
|
29
|
+
- `index`
|
|
30
|
+
- `token_count`
|
|
31
|
+
- `char_start`
|
|
32
|
+
- `char_end`
|
|
33
|
+
- `headings`
|
|
34
|
+
- `metadata`
|
|
35
|
+
|
|
36
|
+
## CLI
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
semantic-chunks README.md --max-tokens 300 --overlap 40
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Outputs JSON.
|
|
43
|
+
|
|
44
|
+
## Why
|
|
45
|
+
|
|
46
|
+
Ruby has excellent web frameworks and backend tooling, but a lot of AI utility work still assumes Python. SemanticChunks gives Ruby apps a native, predictable chunking layer before embeddings, vector search, or LLM calls.
|
data/exe/semantic-chunks
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "json"
|
|
5
|
+
require "optparse"
|
|
6
|
+
require "semantic_chunks"
|
|
7
|
+
|
|
8
|
+
options = { max_tokens: 400, overlap: 60 }
|
|
9
|
+
|
|
10
|
+
OptionParser.new do |parser|
|
|
11
|
+
parser.banner = "Usage: semantic-chunks FILE [--max-tokens N] [--overlap N]"
|
|
12
|
+
parser.on("--max-tokens N", Integer) { |value| options[:max_tokens] = value }
|
|
13
|
+
parser.on("--overlap N", Integer) { |value| options[:overlap] = value }
|
|
14
|
+
end.parse!
|
|
15
|
+
|
|
16
|
+
path = ARGV.fetch(0) do
|
|
17
|
+
warn "semantic-chunks: missing file"
|
|
18
|
+
exit 1
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
text = File.read(path, encoding: "UTF-8")
|
|
22
|
+
chunks = SemanticChunks.split(text, **options).map(&:to_h)
|
|
23
|
+
puts JSON.pretty_generate(chunks)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module SemanticChunks
|
|
2
|
+
Chunk = Struct.new(:text, :index, :token_count, :char_start, :char_end, :headings, :metadata, keyword_init: true) do
|
|
3
|
+
def to_h
|
|
4
|
+
{
|
|
5
|
+
text: text,
|
|
6
|
+
index: index,
|
|
7
|
+
token_count: token_count,
|
|
8
|
+
char_start: char_start,
|
|
9
|
+
char_end: char_end,
|
|
10
|
+
headings: headings,
|
|
11
|
+
metadata: metadata
|
|
12
|
+
}
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
module SemanticChunks
|
|
2
|
+
class Splitter
|
|
3
|
+
WORD = /[[:alnum:]_]+(?:['-][[:alnum:]_]+)?|[^\s]/.freeze
|
|
4
|
+
SENTENCE_END = /(?<=[.!?])\s+/.freeze
|
|
5
|
+
HEADING = /\A(#{'#'}{1,6})\s+(.+?)\s*\z/.freeze
|
|
6
|
+
|
|
7
|
+
attr_reader :max_tokens, :overlap, :min_tokens
|
|
8
|
+
|
|
9
|
+
def initialize(max_tokens: 400, overlap: 60, min_tokens: 40)
|
|
10
|
+
raise ArgumentError, "max_tokens must be positive" unless max_tokens.positive?
|
|
11
|
+
raise ArgumentError, "overlap must be non-negative" if overlap.negative?
|
|
12
|
+
raise ArgumentError, "overlap must be smaller than max_tokens" if overlap >= max_tokens
|
|
13
|
+
|
|
14
|
+
@max_tokens = max_tokens
|
|
15
|
+
@overlap = overlap
|
|
16
|
+
@min_tokens = min_tokens
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def split(text)
|
|
20
|
+
source = String(text)
|
|
21
|
+
units = semantic_units(source)
|
|
22
|
+
chunks = []
|
|
23
|
+
current = []
|
|
24
|
+
current_tokens = 0
|
|
25
|
+
current_start = nil
|
|
26
|
+
headings = []
|
|
27
|
+
|
|
28
|
+
units.each do |unit|
|
|
29
|
+
headings = unit[:headings] if unit[:heading]
|
|
30
|
+
if current.any? && current_tokens + unit[:tokens] > max_tokens
|
|
31
|
+
chunks << build_chunk(chunks.length, current, current_start, headings)
|
|
32
|
+
current, current_tokens, current_start = carry_overlap(current)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
current_start ||= unit[:start]
|
|
36
|
+
current << unit
|
|
37
|
+
current_tokens += unit[:tokens]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
chunks << build_chunk(chunks.length, current, current_start, headings) if current.any?
|
|
41
|
+
merge_small_tail(chunks)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
def semantic_units(text)
|
|
47
|
+
headings = []
|
|
48
|
+
offset = 0
|
|
49
|
+
units = []
|
|
50
|
+
|
|
51
|
+
text.each_line do |line|
|
|
52
|
+
line_start = offset
|
|
53
|
+
offset += line.length
|
|
54
|
+
|
|
55
|
+
if (match = line.strip.match(HEADING))
|
|
56
|
+
level = match[1].length
|
|
57
|
+
headings = headings.take(level - 1) + [match[2]]
|
|
58
|
+
units << unit(line, line_start, headings, heading: true)
|
|
59
|
+
next
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
split_sentences(line).each do |sentence, sentence_start|
|
|
63
|
+
units << unit(sentence, line_start + sentence_start, headings)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
units.reject { |entry| entry[:text].strip.empty? }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def split_sentences(line)
|
|
71
|
+
cursor = 0
|
|
72
|
+
line.split(SENTENCE_END).map do |sentence|
|
|
73
|
+
start = line.index(sentence, cursor) || cursor
|
|
74
|
+
cursor = start + sentence.length
|
|
75
|
+
[sentence, start]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def unit(text, start, headings, heading: false)
|
|
80
|
+
{
|
|
81
|
+
text: text,
|
|
82
|
+
start: start,
|
|
83
|
+
finish: start + text.length,
|
|
84
|
+
tokens: token_count(text),
|
|
85
|
+
headings: headings.dup,
|
|
86
|
+
heading: heading
|
|
87
|
+
}
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def token_count(text)
|
|
91
|
+
text.scan(WORD).length
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def carry_overlap(units)
|
|
95
|
+
return [[], 0, nil] if overlap.zero?
|
|
96
|
+
|
|
97
|
+
kept = []
|
|
98
|
+
count = 0
|
|
99
|
+
units.reverse_each do |unit|
|
|
100
|
+
break if count + unit[:tokens] > overlap && kept.any?
|
|
101
|
+
|
|
102
|
+
kept.unshift(unit)
|
|
103
|
+
count += unit[:tokens]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
[kept, count, kept.first&.fetch(:start)]
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def build_chunk(index, units, start, headings)
|
|
110
|
+
text = units.map { |unit| unit[:text].strip }.reject(&:empty?).join("\n\n")
|
|
111
|
+
chunk_headings = units.reverse.find { |unit| unit[:headings].any? }&.fetch(:headings) || headings
|
|
112
|
+
|
|
113
|
+
Chunk.new(
|
|
114
|
+
text: text,
|
|
115
|
+
index: index,
|
|
116
|
+
token_count: token_count(text),
|
|
117
|
+
char_start: start || 0,
|
|
118
|
+
char_end: units.last[:finish],
|
|
119
|
+
headings: chunk_headings,
|
|
120
|
+
metadata: { strategy: "semantic", overlap: overlap, max_tokens: max_tokens }
|
|
121
|
+
)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def merge_small_tail(chunks)
|
|
125
|
+
return chunks if chunks.length < 2
|
|
126
|
+
return chunks if chunks.last.token_count >= min_tokens
|
|
127
|
+
|
|
128
|
+
tail = chunks.pop
|
|
129
|
+
previous = chunks.pop
|
|
130
|
+
merged_text = [previous.text, tail.text].join("\n\n")
|
|
131
|
+
|
|
132
|
+
chunks << Chunk.new(
|
|
133
|
+
text: merged_text,
|
|
134
|
+
index: previous.index,
|
|
135
|
+
token_count: token_count(merged_text),
|
|
136
|
+
char_start: previous.char_start,
|
|
137
|
+
char_end: tail.char_end,
|
|
138
|
+
headings: tail.headings.any? ? tail.headings : previous.headings,
|
|
139
|
+
metadata: previous.metadata.merge(merged_tail: true)
|
|
140
|
+
)
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
require_relative "semantic_chunks/chunk"
|
|
2
|
+
require_relative "semantic_chunks/splitter"
|
|
3
|
+
require_relative "semantic_chunks/version"
|
|
4
|
+
|
|
5
|
+
module SemanticChunks
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
def self.split(text, **options)
|
|
9
|
+
Splitter.new(**options).split(text)
|
|
10
|
+
end
|
|
11
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: semantic_chunks
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- iamzayn19
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-09-27 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: minitest
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '5.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '5.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '13.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '13.0'
|
|
41
|
+
description: SemanticChunks splits Markdown and plain text into token-budgeted chunks
|
|
42
|
+
with overlap, headings, offsets, and metadata without Python dependencies.
|
|
43
|
+
email:
|
|
44
|
+
- iamzayn19@gmail.com
|
|
45
|
+
executables:
|
|
46
|
+
- semantic-chunks
|
|
47
|
+
extensions: []
|
|
48
|
+
extra_rdoc_files: []
|
|
49
|
+
files:
|
|
50
|
+
- LICENSE
|
|
51
|
+
- README.md
|
|
52
|
+
- exe/semantic-chunks
|
|
53
|
+
- lib/semantic_chunks.rb
|
|
54
|
+
- lib/semantic_chunks/chunk.rb
|
|
55
|
+
- lib/semantic_chunks/splitter.rb
|
|
56
|
+
- lib/semantic_chunks/version.rb
|
|
57
|
+
homepage: https://github.com/iamzayn19/semantic_chunks
|
|
58
|
+
licenses:
|
|
59
|
+
- MIT
|
|
60
|
+
metadata:
|
|
61
|
+
bug_tracker_uri: https://github.com/iamzayn19/semantic_chunks/issues
|
|
62
|
+
source_code_uri: https://github.com/iamzayn19/semantic_chunks
|
|
63
|
+
post_install_message:
|
|
64
|
+
rdoc_options: []
|
|
65
|
+
require_paths:
|
|
66
|
+
- lib
|
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
68
|
+
requirements:
|
|
69
|
+
- - ">="
|
|
70
|
+
- !ruby/object:Gem::Version
|
|
71
|
+
version: '3.0'
|
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
73
|
+
requirements:
|
|
74
|
+
- - ">="
|
|
75
|
+
- !ruby/object:Gem::Version
|
|
76
|
+
version: '0'
|
|
77
|
+
requirements: []
|
|
78
|
+
rubygems_version: 3.5.22
|
|
79
|
+
signing_key:
|
|
80
|
+
specification_version: 4
|
|
81
|
+
summary: Pure Ruby semantic text chunking for RAG and LLM apps.
|
|
82
|
+
test_files: []
|