Class: ChunkerRuby::Markdown
- Inherits:
-
BaseSplitter
- Object
- BaseSplitter
- ChunkerRuby::Markdown
- Defined in:
- lib/chunker_ruby/markdown.rb
Constant Summary collapse
- HEADER_PATTERN =
/^(\#{1,6})\s+(.+)$/
Instance Attribute Summary
Attributes inherited from BaseSplitter
Instance Method Summary collapse
-
#initialize(keep_headers: true, **kwargs) ⇒ Markdown
constructor
A new instance of Markdown.
- #split(text, metadata: {}) ⇒ Object
Methods inherited from BaseSplitter
Constructor Details
#initialize(keep_headers: true, **kwargs) ⇒ Markdown
Returns a new instance of Markdown.
7 8 9 10 |
# File 'lib/chunker_ruby/markdown.rb', line 7 def initialize(keep_headers: true, **kwargs) super(**kwargs) @keep_headers = keep_headers end |
Instance Method Details
#split(text, metadata: {}) ⇒ Object
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 |
# File 'lib/chunker_ruby/markdown.rb', line 12 def split(text, metadata: {}) return [] if text.nil? || text.empty? sections = split_by_headers(text) chunks = [] sections.each do |section| = .merge(section[:metadata]) if section[:text].length <= @chunk_size chunks << Chunk.new( text: section[:text], index: chunks.size, offset: section[:offset], metadata: ) else # Fall back to recursive splitting for large sections sub_splitter = RecursiveCharacter.new( chunk_size: @chunk_size, chunk_overlap: @chunk_overlap, separators: ["\n\n", "\n", ". ", " ", ""] ) sub_chunks = sub_splitter.split(section[:text], metadata: ) sub_chunks.each do |sc| chunks << Chunk.new( text: sc.text, index: chunks.size, offset: section[:offset] + sc.offset, metadata: sc. ) end end end chunks end |