Class: FactDb::Extractors::ManualExtractor

Inherits:
Base
  • Object
show all
Defined in:
lib/fact_db/extractors/manual_extractor.rb

Overview

Manual fact extractor for API-driven fact creation

Passes through user-provided text as a single fact without any automated extraction. Used when the user provides fact text and metadata directly via the API.

Examples:

Extract a manual fact

extractor = ManualExtractor.new
facts = extractor.extract("John works at Acme", valid_at: Date.today)

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

available_types, #extraction_method, for, #initialize

Constructor Details

This class inherits a constructor from FactDb::Extractors::Base

Instance Method Details

#create_entity(name:, type:, aliases: [], attributes: {}) ⇒ Hash

Creates an entity hash

Convenience method for building entity data manually.

Parameters:

  • name (String)

    the entity name

  • type (String, Symbol)

    entity kind (person, organization, etc.)

  • aliases (Array<String>) (defaults to: [])

    alternative names

  • attributes (Hash) (defaults to: {})

    additional attributes

Returns:

  • (Hash)

    the entity hash



83
84
85
86
87
88
89
90
# File 'lib/fact_db/extractors/manual_extractor.rb', line 83

def create_entity(name:, type:, aliases: [], attributes: {})
  build_entity(
    name: name,
    type: type,
    aliases: aliases,
    attributes: attributes
  )
end

#create_fact(text:, valid_at:, invalid_at: nil, mentions: [], confidence: 1.0, metadata: {}) ⇒ Hash

Creates a single fact with full control over all attributes

Convenience method that wraps #extract with named parameters.

Parameters:

  • text (String)

    the fact text

  • valid_at (Date, Time)

    when the fact became valid

  • invalid_at (Date, Time, nil) (defaults to: nil)

    when the fact became invalid

  • mentions (Array<Hash>) (defaults to: [])

    entity mentions

  • confidence (Float) (defaults to: 1.0)

    confidence score (0.0 to 1.0)

  • metadata (Hash) (defaults to: {})

    additional metadata

Returns:

  • (Hash)

    the fact hash



64
65
66
67
68
69
70
71
72
# File 'lib/fact_db/extractors/manual_extractor.rb', line 64

def create_fact(text:, valid_at:, invalid_at: nil, mentions: [], confidence: 1.0, metadata: {})
  extract(text, {
    valid_at: valid_at,
    invalid_at: invalid_at,
    mentions: mentions,
    confidence: confidence,
    metadata: 
  }).first
end

#extract(text, context = {}) ⇒ Array<Hash>

Extracts a single fact from the provided text

Returns the text as-is without parsing. All metadata comes from context.

Parameters:

  • text (String)

    the fact text

  • context (Hash) (defaults to: {})

    fact metadata

Options Hash (context):

  • :valid_at (Date, Time)

    when the fact became valid

  • :invalid_at (Date, Time)

    when the fact became invalid

  • :mentions (Array<Hash>)

    entity mentions

  • :confidence (Float)

    confidence score

  • :metadata (Hash)

    additional metadata

Returns:

  • (Array<Hash>)

    array with single fact hash, or empty if text is blank



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fact_db/extractors/manual_extractor.rb', line 28

def extract(text, context = {})
  return [] if text.nil? || text.strip.empty?

  valid_at = context[:valid_at] || context[:captured_at] || Time.current

  [
    build_fact(
      text: text,
      valid_at: valid_at,
      invalid_at: context[:invalid_at],
      mentions: context[:mentions] || [],
      confidence: context[:confidence] || 1.0,
      metadata: context[:metadata] || {}
    )
  ]
end

#extract_entities(text) ⇒ Array

Returns empty array since manual extraction expects entities to be provided

Parameters:

  • text (String)

    ignored

Returns:

  • (Array)

    empty array



49
50
51
# File 'lib/fact_db/extractors/manual_extractor.rb', line 49

def extract_entities(text)
  []
end