Class: LangExtract::Core::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/langextract/core/data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, id: nil, metadata: {}) ⇒ Document

Returns a new instance of Document.

Raises:

  • (ArgumentError)


168
169
170
171
172
173
174
175
# File 'lib/langextract/core/data.rb', line 168

def initialize(text:, id: nil, metadata: {})
  raise ArgumentError, "text is required" if text.nil?

  @text = text
  @id = id
  @metadata = HashCoercion.stringify_keys().freeze
  freeze
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



166
167
168
# File 'lib/langextract/core/data.rb', line 166

def id
  @id
end

#metadataObject (readonly)

Returns the value of attribute metadata.



166
167
168
# File 'lib/langextract/core/data.rb', line 166

def 
  @metadata
end

#textObject (readonly)

Returns the value of attribute text.



166
167
168
# File 'lib/langextract/core/data.rb', line 166

def text
  @text
end

Class Method Details

.from_h(hash) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/langextract/core/data.rb', line 181

def self.from_h(hash)
  new(
    id: HashCoercion.read(hash, :id),
    text: HashCoercion.read(hash, :text).to_s,
    metadata: HashCoercion.read(hash, :metadata) || {}
  )
end

Instance Method Details

#to_hObject



177
178
179
# File 'lib/langextract/core/data.rb', line 177

def to_h
  { "id" => id, "text" => text, "metadata" =>  }
end