Class: Legion::Extensions::Agentic::Language::PragmaticInference::Helpers::PragmaticEngine
- Inherits:
-
Object
- Object
- Legion::Extensions::Agentic::Language::PragmaticInference::Helpers::PragmaticEngine
- Defined in:
- lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb
Instance Attribute Summary collapse
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#utterances ⇒ Object
readonly
Returns the value of attribute utterances.
Instance Method Summary collapse
- #analyze_utterance(content:, speaker:, speech_act:, literal_meaning: nil, domain: nil, maxim_scores: {}) ⇒ Object
- #by_speaker(speaker:) ⇒ Object
- #by_speech_act(speech_act:) ⇒ Object
- #count ⇒ Object
- #decay_all ⇒ Object
- #detect_violations(utterance_id:) ⇒ Object
- #generate_implicature(utterance_id:, inferred_meaning:) ⇒ Object
-
#initialize ⇒ PragmaticEngine
constructor
A new instance of PragmaticEngine.
- #most_violated_maxim ⇒ Object
- #overall_cooperation ⇒ Object
- #reinforce(utterance_id:) ⇒ Object
- #speaker_profile(speaker:) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ PragmaticEngine
Returns a new instance of PragmaticEngine.
12 13 14 15 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 12 def initialize @utterances = {} @history = [] end |
Instance Attribute Details
#history ⇒ Object (readonly)
Returns the value of attribute history.
10 11 12 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 10 def history @history end |
#utterances ⇒ Object (readonly)
Returns the value of attribute utterances.
10 11 12 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 10 def utterances @utterances end |
Instance Method Details
#analyze_utterance(content:, speaker:, speech_act:, literal_meaning: nil, domain: nil, maxim_scores: {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 17 def analyze_utterance(content:, speaker:, speech_act:, literal_meaning: nil, domain: nil, maxim_scores: {}) trim_utterances if @utterances.size >= Constants::MAX_UTTERANCES utterance = Utterance.new( content: content, speaker: speaker, speech_act: speech_act, literal_meaning: literal_meaning, domain: domain, maxim_scores: maxim_scores ) @utterances[utterance.id] = utterance record_history(utterance.id, :analyzed) utterance end |
#by_speaker(speaker:) ⇒ Object
83 84 85 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 83 def by_speaker(speaker:) @utterances.values.select { |u| u.speaker == speaker } end |
#by_speech_act(speech_act:) ⇒ Object
79 80 81 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 79 def by_speech_act(speech_act:) @utterances.values.select { |u| u.speech_act == speech_act } end |
#count ⇒ Object
120 121 122 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 120 def count @utterances.size end |
#decay_all ⇒ Object
114 115 116 117 118 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 114 def decay_all @utterances.each_value do |utterance| utterance.update_confidence(-Constants::DECAY_RATE) end end |
#detect_violations(utterance_id:) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 35 def detect_violations(utterance_id:) utterance = @utterances[utterance_id] return [] unless utterance violations = [] utterance.maxim_scores.each do |maxim, score| next if score >= 0.5 violation_type = classify_violation(score) violation = { maxim: maxim, violation_type: violation_type, score: score } violations << violation utterance.violations << violation end violations end |
#generate_implicature(utterance_id:, inferred_meaning:) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 52 def generate_implicature(utterance_id:, inferred_meaning:) utterance = @utterances[utterance_id] return nil unless utterance utterance.add_implicature(meaning: inferred_meaning) record_history(utterance_id, :implicature_added) inferred_meaning end |
#most_violated_maxim ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 87 def most_violated_maxim return nil if @utterances.empty? violation_counts = Hash.new(0) @utterances.each_value do |utterance| utterance.violated_maxims.each { |maxim| violation_counts[maxim] += 1 } end return nil if violation_counts.empty? violation_counts.max_by { |_maxim, count| count }&.first end |
#overall_cooperation ⇒ Object
100 101 102 103 104 105 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 100 def overall_cooperation return 0.0 if @utterances.empty? total = @utterances.values.sum(&:overall_compliance) total / @utterances.size.to_f end |
#reinforce(utterance_id:) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 107 def reinforce(utterance_id:) utterance = @utterances[utterance_id] return unless utterance utterance.update_confidence(Constants::REINFORCEMENT_RATE) end |
#speaker_profile(speaker:) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 61 def speaker_profile(speaker:) speaker_utterances = by_speaker(speaker: speaker) return { speaker: speaker, utterance_count: 0, compliance_by_maxim: {} } if speaker_utterances.empty? compliance_by_maxim = Constants::MAXIMS.to_h do |maxim| scores = speaker_utterances.map { |u| u.maxim_scores[maxim] } mean = scores.sum / scores.size.to_f [maxim, mean.round(3)] end { speaker: speaker, utterance_count: speaker_utterances.size, compliance_by_maxim: compliance_by_maxim, overall_compliance: (compliance_by_maxim.values.sum / compliance_by_maxim.size.to_f).round(3) } end |
#to_h ⇒ Object
124 125 126 127 128 129 130 131 132 |
# File 'lib/legion/extensions/agentic/language/pragmatic_inference/helpers/pragmatic_engine.rb', line 124 def to_h { utterance_count: @utterances.size, overall_cooperation: overall_cooperation.round(3), most_violated_maxim: most_violated_maxim, history_size: @history.size, speakers: @utterances.values.map(&:speaker).uniq.size } end |