Module: LangExtract::IO

Defined in:
lib/langextract/io.rb

Class Method Summary collapse

Class Method Details

.load_annotated_documents_jsonl(path) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/langextract/io.rb', line 25

def load_annotated_documents_jsonl(path)
  ::File.readlines(path, chomp: true).filter_map do |line|
    next if line.strip.empty?

    Core::AnnotatedDocument.from_h(JSON.parse(line))
  end
rescue SystemCallError, JSON::ParserError => e
  raise Core::IOFailure, "failed to load annotated documents: #{e.message}"
end

.save_annotated_documents(path, documents) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/langextract/io.rb', line 12

def save_annotated_documents(path, documents)
  normalized = Array(documents)
  ::File.open(path, "w") do |file|
    normalized.each do |document|
      annotated = document.is_a?(Core::AnnotatedDocument) ? document : Core::AnnotatedDocument.from_h(document)
      file.puts(JSON.generate(annotated.to_h))
    end
  end
  path
rescue SystemCallError, JSON::GeneratorError => e
  raise Core::IOFailure, "failed to save annotated documents: #{e.message}"
end