Class: FactDb::Models::FactSource

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/fact_db/models/fact_source.rb

Overview

Join model linking facts to source documents

Represents the provenance relationship between a fact and the source document(s) it was extracted from, including the relationship type and an optional excerpt.

Examples:

Link a fact to a source

fact_source = FactSource.create!(
  fact: fact, source: document,
  kind: "primary", excerpt: "relevant quote..."
)

Constant Summary collapse

KINDS =

Returns valid source relationship kinds.

Returns:

  • (Array<String>)

    valid source relationship kinds

%w[primary supporting corroborating].freeze

Instance Method Summary collapse

Instance Method Details

#corroboratingActiveRecord::Relation

Returns corroborating source links

Returns:

  • (ActiveRecord::Relation)


43
# File 'lib/fact_db/models/fact_source.rb', line 43

scope :corroborating, -> { where(kind: "corroborating") }

#excerpt_preview(length: 100) ⇒ String?

Returns a preview of the excerpt, truncated if needed

Parameters:

  • length (Integer) (defaults to: 100)

    maximum length (default: 100)

Returns:

  • (String, nil)

    excerpt preview with “…” if truncated, or nil if no excerpt



61
62
63
64
65
66
# File 'lib/fact_db/models/fact_source.rb', line 61

def excerpt_preview(length: 100)
  return nil if excerpt.nil?
  return excerpt if excerpt.length <= length

  "#{excerpt[0, length]}..."
end

#high_confidenceActiveRecord::Relation

Returns source links with confidence >= 0.9

Returns:

  • (ActiveRecord::Relation)


48
# File 'lib/fact_db/models/fact_source.rb', line 48

scope :high_confidence, -> { where("confidence >= ?", 0.9) }

#primaryActiveRecord::Relation

Returns primary source links

Returns:

  • (ActiveRecord::Relation)


33
# File 'lib/fact_db/models/fact_source.rb', line 33

scope :primary, -> { where(kind: "primary") }

#primary?Boolean

Checks if this is the primary source for the fact

Returns:

  • (Boolean)

    true if kind is “primary”



53
54
55
# File 'lib/fact_db/models/fact_source.rb', line 53

def primary?
  kind == "primary"
end

#supportingActiveRecord::Relation

Returns supporting source links

Returns:

  • (ActiveRecord::Relation)


38
# File 'lib/fact_db/models/fact_source.rb', line 38

scope :supporting, -> { where(kind: "supporting") }