Class: Legion::Extensions::Agentic::Language::ConceptualMetaphor::Helpers::Metaphor

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb

Constant Summary

Constants included from Constants

Constants::CONVENTIONALITY_LABELS, Constants::CONVENTIONALITY_THRESHOLD, Constants::DECAY_RATE, Constants::DEFAULT_STRENGTH, Constants::MAX_DOMAINS, Constants::MAX_HISTORY, Constants::MAX_MAPPINGS, Constants::MAX_METAPHORS, Constants::METAPHOR_TYPES, Constants::NOVELTY_THRESHOLD, Constants::REINFORCEMENT_BOOST, Constants::STALE_THRESHOLD, Constants::STRENGTH_CEILING, Constants::STRENGTH_FLOOR, Constants::STRENGTH_LABELS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_domain:, target_domain:, metaphor_type:, mappings:, strength: nil, conventionality: nil) ⇒ Metaphor

Returns a new instance of Metaphor.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 18

def initialize(source_domain:, target_domain:, metaphor_type:,
               mappings:, strength: nil, conventionality: nil)
  @id              = SecureRandom.uuid
  @source_domain   = source_domain
  @target_domain   = target_domain
  @metaphor_type   = metaphor_type
  @mappings        = mappings
  @entailments     = []
  @strength        = (strength || DEFAULT_STRENGTH).to_f.clamp(STRENGTH_FLOOR, STRENGTH_CEILING)
  @conventionality = (conventionality || DEFAULT_STRENGTH).to_f.clamp(STRENGTH_FLOOR, STRENGTH_CEILING)
  @use_count       = 0
  @created_at      = Time.now.utc
  @last_used_at    = @created_at
end

Instance Attribute Details

#conventionalityObject (readonly)

Returns the value of attribute conventionality.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def conventionality
  @conventionality
end

#created_atObject (readonly)

Returns the value of attribute created_at.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def created_at
  @created_at
end

#entailmentsObject (readonly)

Returns the value of attribute entailments.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def entailments
  @entailments
end

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def id
  @id
end

#last_used_atObject (readonly)

Returns the value of attribute last_used_at.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def last_used_at
  @last_used_at
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def mappings
  @mappings
end

#metaphor_typeObject (readonly)

Returns the value of attribute metaphor_type.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def metaphor_type
  @metaphor_type
end

#source_domainObject (readonly)

Returns the value of attribute source_domain.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def source_domain
  @source_domain
end

#strengthObject (readonly)

Returns the value of attribute strength.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def strength
  @strength
end

#target_domainObject (readonly)

Returns the value of attribute target_domain.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def target_domain
  @target_domain
end

#use_countObject (readonly)

Returns the value of attribute use_count.



14
15
16
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 14

def use_count
  @use_count
end

Instance Method Details

#add_entailment(entailment) ⇒ Object



40
41
42
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 40

def add_entailment(entailment)
  @entailments << entailment
end

#conventional?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 54

def conventional?
  @conventionality >= CONVENTIONALITY_THRESHOLD
end

#conventionality_labelObject



62
63
64
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 62

def conventionality_label
  CONVENTIONALITY_LABELS.find { |range, _| range.cover?(@conventionality) }&.last || :unknown
end

#coverageObject



48
49
50
51
52
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 48

def coverage
  return 0.0 if @mappings.empty?

  @mappings.values.compact.size.to_f / @mappings.size
end

#decay!Object



70
71
72
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 70

def decay!
  @strength = (@strength - DECAY_RATE).clamp(STRENGTH_FLOOR, STRENGTH_CEILING)
end

#map_concept(source_concept) ⇒ Object



44
45
46
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 44

def map_concept(source_concept)
  @mappings[source_concept]
end

#novel?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 58

def novel?
  @conventionality <= NOVELTY_THRESHOLD
end

#stale?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 74

def stale?
  (Time.now.utc - @last_used_at) > STALE_THRESHOLD
end

#strength_labelObject



66
67
68
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 66

def strength_label
  STRENGTH_LABELS.find { |range, _| range.cover?(@strength) }&.last || :unknown
end

#to_hObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 78

def to_h
  {
    id:                    @id,
    source_domain:         @source_domain,
    target_domain:         @target_domain,
    metaphor_type:         @metaphor_type,
    mappings:              @mappings,
    entailments:           @entailments,
    strength:              @strength,
    conventionality:       @conventionality,
    conventionality_label: conventionality_label,
    strength_label:        strength_label,
    use_count:             @use_count,
    coverage:              coverage,
    created_at:            @created_at,
    last_used_at:          @last_used_at
  }
end

#use!Object



33
34
35
36
37
38
# File 'lib/legion/extensions/agentic/language/conceptual_metaphor/helpers/metaphor.rb', line 33

def use!
  @use_count   += 1
  @last_used_at = Time.now.utc
  @strength     = (@strength + REINFORCEMENT_BOOST).clamp(STRENGTH_FLOOR, STRENGTH_CEILING)
  increase_conventionality
end