Class: Engram::Provenance

Inherits:
Object
  • Object
show all
Defined in:
lib/engram/provenance.rb

Overview

Provider-neutral source provenance stored in Record metadata.

Offsets are zero-based Unicode codepoint indexes into one host-owned source message. The end offset is exclusive. Engram stores references and spans, never the source text itself.

Defined Under Namespace

Classes: Extractor, Source, Span

Constant Summary collapse

RESERVED_KEY =
"_engram"
METADATA_KEY =
"provenance"
SCHEMA_VERSION =
1
ALIGNMENTS =
%i[exact normalized inferred ungrounded].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources:, extractor:, confidence:) ⇒ Provenance

Returns a new instance of Provenance.

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/engram/provenance.rb', line 127

def initialize(sources:, extractor:, confidence:)
  raise ArgumentError, "sources must be an array" unless sources.is_a?(Array)
  raise ArgumentError, "sources must contain at least one source" if sources.empty?
  @sources = sources.map do |source|
    raise ArgumentError, "sources must contain Provenance::Source values" unless source.is_a?(Source)
    source
  end.freeze
  raise ArgumentError, "extractor must be a Provenance::Extractor" unless extractor.is_a?(Extractor)
  valid_confidence = (confidence.is_a?(Integer) || confidence.is_a?(Float)) && confidence.between?(0, 1)
  unless valid_confidence
    raise ArgumentError, "confidence must be an Integer or Float between 0 and 1"
  end

  @extractor = extractor
  @confidence = confidence
  freeze
end

Instance Attribute Details

#confidenceObject (readonly)

Returns the value of attribute confidence.



125
126
127
# File 'lib/engram/provenance.rb', line 125

def confidence
  @confidence
end

#extractorObject (readonly)

Returns the value of attribute extractor.



125
126
127
# File 'lib/engram/provenance.rb', line 125

def extractor
  @extractor
end

#sourcesObject (readonly)

Returns the value of attribute sources.



125
126
127
# File 'lib/engram/provenance.rb', line 125

def sources
  @sources
end

Class Method Details

.attach(metadata, provenance) ⇒ Object

Raises:

  • (ArgumentError)


162
163
164
165
166
# File 'lib/engram/provenance.rb', line 162

def attach(, provenance)
  raise ArgumentError, "provenance must be a Provenance value" unless provenance.is_a?(self)

  Engram::ReservedMetadata.attach(, METADATA_KEY, provenance.to_h)
end

.extract(metadata) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/engram/provenance.rb', line 168

def extract()
   = deep_stringify( || {})
  return nil unless .is_a?(Hash)

  reserved = [RESERVED_KEY]
  return nil unless reserved.is_a?(Hash)

  data = reserved[METADATA_KEY]
  return nil unless data.is_a?(Hash) && data["version"] == SCHEMA_VERSION

  from_h(data)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



145
146
147
# File 'lib/engram/provenance.rb', line 145

def ==(other)
  other.is_a?(self.class) && to_h == other.to_h
end

#hashObject



150
# File 'lib/engram/provenance.rb', line 150

def hash = to_h.hash

#to_hObject



152
153
154
155
156
157
158
159
# File 'lib/engram/provenance.rb', line 152

def to_h
  {
    "version" => SCHEMA_VERSION,
    "sources" => sources.map(&:to_h),
    "extractor" => extractor.to_h,
    "confidence" => confidence
  }
end