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
34
35
# File 'lib/langextract/io.rb', line 25

def load_annotated_documents_jsonl(path)
  documents = []
  ::File.foreach(path, encoding: "UTF-8") do |line|
    next if line.strip.empty?

    documents << Core::AnnotatedDocument.from_h(JSON.parse(line))
  end
  documents
rescue SystemCallError, JSON::ParserError, EncodingError => 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 = documents.is_a?(Hash) ? [documents] : Array(documents)
  ::File.open(path, "w:UTF-8") 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, EncodingError => e
  raise Core::IOFailure, "failed to save annotated documents: #{e.message}"
end