baran 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d2d2cc921acca7ce1f9462f118d7c6f3ea8933f450b739291ae8c6d10f0148e0
4
+ data.tar.gz: c1a2ce276c1146d1e6d2fd1540fd50591335bd2daf698d38eb13c32055df6a2e
5
+ SHA512:
6
+ metadata.gz: ce4b6febc196836d00ac3520a44f31252ae9f59900a962e1763e26427952776099afa88b19ba04fee87f377eb24a16670ba6054e53808333e67a09019bb2fa7f
7
+ data.tar.gz: 36c094c54f0c45efd16781b69f02f0230e9281a1eff737fe3251d065e1d4f4b1b4e8b48e6c72e038f2065b4b5bf1b259c13099fcb5484e486a98b0a7bfc03203
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-28
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at me@moeki.dev. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in baran.gemspec
6
+ gemspec
7
+
8
+ gem "minitest", "~> 5.18"
9
+
10
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ baran (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.18.0)
10
+ rake (13.0.6)
11
+
12
+ PLATFORMS
13
+ arm64-darwin-22
14
+ x86_64-linux
15
+
16
+ DEPENDENCIES
17
+ baran!
18
+ minitest (~> 5.18)
19
+ rake (~> 13.0)
20
+
21
+ BUNDLED WITH
22
+ 2.4.7
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Moeki Kawakami
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # Baran
2
+
3
+ Text Splitter for Large Language Model datasets.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add baran
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install baran
14
+
15
+ ## Usage
16
+
17
+ ### Default Paramters
18
+
19
+ - `chunk_size`: 1024 (characters)
20
+ - `chunk_overlap`: 64 (characters)
21
+
22
+ ### Character Text Splitter
23
+
24
+ Splitting by the specified character.
25
+
26
+ ```ruby
27
+ splitter = Baran::CharacterTextSplitter.new(
28
+ chunk_size: 1024,
29
+ chunk_overlap: 64,
30
+ separator: "\n\n"
31
+ )
32
+ splitter.chunks(text)
33
+ ```
34
+
35
+ ### Recursive Character Text Splitter
36
+
37
+ Splitting by the specified characters recursively.
38
+
39
+ ```ruby
40
+ splitter = Baran::RecursiveCharacterTextSplitter.new(
41
+ separators: ["\nn", "\n", " ", ""]
42
+ )
43
+ splitter.chunks(text)
44
+ ```
45
+
46
+ ### Markdown Text Splitter
47
+
48
+ Splitting by the Markdown descriptions.
49
+
50
+ ```ruby
51
+ splitter = Baran::MarkdownSplitter.new
52
+ splitter.chunks(markdown)
53
+ ```
54
+
55
+ Split with the following priority.
56
+
57
+ ```ruby
58
+ [
59
+ "\n# ", # h1
60
+ "\n## ", # h2
61
+ "\n### ", # h3
62
+ "\n#### ", # h4
63
+ "\n##### ", # h5
64
+ "\n###### ", # h6
65
+ "```\n\n", # code block
66
+ "\n\n***\n\n", # horizontal rule
67
+ "\n\n---\n\n", # horizontal rule
68
+ "\n\n___\n\n", # horizontal rule
69
+ "\n\n", # new line
70
+ "\n", # new line
71
+ " ", # space
72
+ "" # empty
73
+ ]
74
+ ```
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+
80
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/moekidev/baran. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/moekidev/baran/blob/main/CODE_OF_CONDUCT.md).
85
+
86
+ ## License
87
+
88
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
89
+
90
+ ## Code of Conduct
91
+
92
+ Everyone interacting in the Baran project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/moekidev/baran/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => [:test]
4
+
5
+ Rake::TestTask.new do |test|
6
+ test.test_files = Dir['test/**/test_*.rb']
7
+ test.verbose = true
8
+ end
data/baran.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/baran/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "baran"
7
+ spec.version = Baran::VERSION
8
+ spec.authors = ["Moeki Kawakami"]
9
+ spec.email = ["me@moeki.dev"]
10
+
11
+ spec.summary = "Text Splitter for Large Language Model Datasets"
12
+ spec.description = "Text Splitter for Large Language Model Datasets."
13
+ spec.homepage = "https://github.com/moekidev/baran"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/moekidev/baran"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (File.expand_path(f) == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,15 @@
1
+ require_relative './text_splitter'
2
+
3
+ module Baran
4
+ class CharacterTextSplitter < TextSplitter
5
+ def initialize(chunk_size: 1024, chunk_overlap: 64, separator: nil)
6
+ super(chunk_size: chunk_size, chunk_overlap: chunk_overlap)
7
+ @separator = separator || "\n\n"
8
+ end
9
+
10
+ def splitted(text)
11
+ splits = @separator.empty? ? text.chars : text.split(@separator)
12
+ merged(splits, @separator)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ require_relative './recursive_character_text_splitter'
2
+
3
+ module Baran
4
+ class MarkdownSplitter < RecursiveCharacterTextSplitter
5
+ def initialize(chunk_size: 1024, chunk_overlap: 64)
6
+ @separators = [
7
+ "\n# ", # h1
8
+ "\n## ", # h2
9
+ "\n### ", # h3
10
+ "\n#### ", # h4
11
+ "\n##### ", # h5
12
+ "\n###### ", # h6
13
+ "```\n\n", # code block
14
+ "\n\n***\n\n", # horizontal rule
15
+ "\n\n---\n\n", # horizontal rule
16
+ "\n\n___\n\n", # horizontal rule
17
+ "\n\n", # new line
18
+ "\n", # new line
19
+ " ", # space
20
+ "" # empty
21
+ ]
22
+ super(chunk_size: chunk_size, chunk_overlap: chunk_overlap)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,51 @@
1
+ require_relative './text_splitter'
2
+
3
+ module Baran
4
+ class RecursiveCharacterTextSplitter < TextSplitter
5
+ attr_accessor :separators
6
+
7
+ def initialize(chunk_size: 1024, chunk_overlap: 64, separators: nil)
8
+ super(chunk_size: chunk_size, chunk_overlap: chunk_overlap)
9
+ @separators = separators || ["\n\n", "\n", " ", ""]
10
+ end
11
+
12
+ def splitted(text)
13
+ final_chunks = []
14
+
15
+ separator = @separators.last
16
+ @separators.each do |s|
17
+ if s.empty?
18
+ separator = s
19
+ break
20
+ elsif text.include?(s)
21
+ separator = s
22
+ break
23
+ end
24
+ end
25
+
26
+ splits = separator.empty? ? text.chars : text.split(separator)
27
+
28
+ good_splits = []
29
+ splits.each do |s|
30
+ if s.length < @chunk_size
31
+ good_splits << s
32
+ else
33
+ unless good_splits.empty?
34
+ merged_text = merged(good_splits, separator)
35
+ final_chunks.concat(merged_text)
36
+ good_splits.clear
37
+ end
38
+ other_info = splitted(s)
39
+ final_chunks.concat(other_info)
40
+ end
41
+ end
42
+
43
+ unless good_splits.empty?
44
+ merged_text = merged(good_splits, separator)
45
+ final_chunks.concat(merged_text)
46
+ end
47
+
48
+ final_chunks
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,62 @@
1
+ module Baran
2
+ class TextSplitter
3
+ attr_accessor :chunk_size, :chunk_overlap
4
+
5
+ def initialize(chunk_size: 1024, chunk_overlap: 64)
6
+ @chunk_size = chunk_size
7
+ @chunk_overlap = chunk_overlap
8
+ raise "Cannot have chunk_overlap >= chunk_size" if @chunk_overlap >= @chunk_size
9
+ end
10
+
11
+ def splitted(text)
12
+ raise NotImplementedError, "splitted method should be implemented in a subclass"
13
+ end
14
+
15
+ def chunks(text)
16
+ cursor = 0
17
+ chunks = []
18
+
19
+ splitted(text).each do |chunk|
20
+ chunks << { text: chunk, cursor: cursor }
21
+ cursor += chunk.length
22
+ end
23
+
24
+ chunks
25
+ end
26
+
27
+ def join_docs(docs, separator)
28
+ text = docs.join(separator).strip
29
+ text.empty? ? nil : text
30
+ end
31
+
32
+ def merged(splits, separator)
33
+ docs = [] # Array of strings
34
+ current_doc = [] # Array of strings
35
+ total = 0
36
+
37
+ splits.each do |d|
38
+ len = d.length
39
+
40
+ if total + len >= @chunk_size
41
+ unless current_doc.empty?
42
+ doc = join_docs(current_doc, separator)
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
49
+ end
50
+ end
51
+
52
+ current_doc.push(d)
53
+ total += len
54
+ end
55
+
56
+ doc = join_docs(current_doc, separator)
57
+ docs.push(doc) unless doc.nil?
58
+
59
+ docs
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Baran
4
+ VERSION = "0.1.0"
5
+ end
data/lib/baran.rb ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "baran/version"
4
+ require_relative "baran/text_splitter"
5
+ require_relative "baran/markdown_splitter"
6
+ require_relative "baran/recursive_character_text_splitter"
7
+ require_relative "baran/character_text_splitter"
8
+
9
+ module Baran
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
data/sig/baran.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Baran
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baran
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Moeki Kawakami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-05-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Text Splitter for Large Language Model Datasets.
14
+ email:
15
+ - me@moeki.dev
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".standard.yml"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - baran.gemspec
29
+ - lib/baran.rb
30
+ - lib/baran/character_text_splitter.rb
31
+ - lib/baran/markdown_splitter.rb
32
+ - lib/baran/recursive_character_text_splitter.rb
33
+ - lib/baran/text_splitter.rb
34
+ - lib/baran/version.rb
35
+ - sig/baran.rbs
36
+ homepage: https://github.com/moekidev/baran
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ homepage_uri: https://github.com/moekidev/baran
41
+ source_code_uri: https://github.com/moekidev/baran
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.6.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.3.26
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Text Splitter for Large Language Model Datasets
61
+ test_files: []