Class: Legion::Extensions::Agentic::Integration::Zeitgeist::Helpers::TrendWindow

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb

Constant Summary

Constants included from Constants

Constants::CONVERGENCE_LABELS, Constants::CONVERGENCE_THRESHOLD, Constants::DEFAULT_INTENSITY, Constants::DIVERGENCE_THRESHOLD, Constants::MAX_SIGNALS, Constants::MOMENTUM_LABELS, Constants::MOMENTUM_THRESHOLD, Constants::MOOD_LABELS, Constants::SIGNAL_DOMAINS, Constants::WINDOW_SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constants

label_for

Constructor Details

#initialize(window_size: WINDOW_SIZE) ⇒ TrendWindow

Returns a new instance of TrendWindow.



14
15
16
17
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 14

def initialize(window_size: WINDOW_SIZE)
  @signals     = []
  @window_size = window_size
end

Instance Attribute Details

#signalsObject (readonly)

Returns the value of attribute signals.



12
13
14
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 12

def signals
  @signals
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



12
13
14
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 12

def window_size
  @window_size
end

Instance Method Details

#accelerating?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 55

def accelerating?
  momentum > MOMENTUM_THRESHOLD
end

#add(signal) ⇒ Object



19
20
21
22
23
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 19

def add(signal)
  @signals << signal
  @signals.shift while @signals.size > @window_size
  self
end

#decelerating?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 59

def decelerating?
  momentum < -MOMENTUM_THRESHOLD
end

#dominant_domainObject



25
26
27
28
29
30
31
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 25

def dominant_domain
  return nil if @signals.empty?

  counts = Hash.new(0.0)
  @signals.each { |s| counts[s.domain] += s.intensity }
  counts.max_by { |_d, weight| weight }&.first
end

#dominant_valenceObject



33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 33

def dominant_valence
  return 0.0 if @signals.empty?

  weighted_sum = @signals.sum { |s| s.valence * s.intensity }
  total_intensity = @signals.sum(&:intensity)
  return 0.0 if total_intensity.zero?

  (weighted_sum / total_intensity).clamp(-1.0, 1.0).round(10)
end

#momentumObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 43

def momentum
  return 0.0 if @signals.size < 2

  half = @signals.size / 2
  first_half  = @signals.first(half)
  second_half = @signals.last(half)

  avg_intensity = ->(arr) { arr.sum(&:intensity) / arr.size.to_f }
  delta = avg_intensity.call(second_half) - avg_intensity.call(first_half)
  delta.clamp(-1.0, 1.0).round(10)
end

#to_hObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/legion/extensions/agentic/integration/zeitgeist/helpers/trend_window.rb', line 63

def to_h
  {
    size:             @signals.size,
    window_size:      @window_size,
    dominant_domain:  dominant_domain,
    dominant_valence: dominant_valence,
    momentum:         momentum,
    accelerating:     accelerating?,
    decelerating:     decelerating?
  }
end