Class: PatternRuby::Conversation
- Inherits:
-
Object
- Object
- PatternRuby::Conversation
- Defined in:
- lib/pattern_ruby/conversation.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#slots ⇒ Object
readonly
Returns the value of attribute slots.
Instance Method Summary collapse
- #clear_context ⇒ Object
-
#initialize(router) ⇒ Conversation
constructor
A new instance of Conversation.
- #process(input) ⇒ Object
- #reset ⇒ Object
- #set_context(ctx) ⇒ Object
Constructor Details
#initialize(router) ⇒ Conversation
Returns a new instance of Conversation.
5 6 7 8 9 10 11 |
# File 'lib/pattern_ruby/conversation.rb', line 5 def initialize(router) @router = router @context = nil @history = [] @slots = {} @mutex = Mutex.new end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
3 4 5 |
# File 'lib/pattern_ruby/conversation.rb', line 3 def context @context end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
3 4 5 |
# File 'lib/pattern_ruby/conversation.rb', line 3 def history @history end |
#slots ⇒ Object (readonly)
Returns the value of attribute slots.
3 4 5 |
# File 'lib/pattern_ruby/conversation.rb', line 3 def slots @slots end |
Instance Method Details
#clear_context ⇒ Object
59 60 61 |
# File 'lib/pattern_ruby/conversation.rb', line 59 def clear_context @mutex.synchronize { @context = nil } end |
#process(input) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/pattern_ruby/conversation.rb', line 13 def process(input) @mutex.synchronize do result = @router.match(input, context: @context) if result.matched? # Merge new entities into accumulated slots result.entities.each do |k, v| @slots[k] = v unless v.nil? end # Build enriched result with accumulated slots result = MatchResult.new( intent: result.intent, entities: @slots.dup, pattern: result.pattern, score: result.score, input: result.input, response: result.response ) elsif !@history.empty? && @slots.any? # Try to fill a missing slot from context last = @history.last if last&.matched? filled = try_slot_fill(input, last.intent) if filled result = MatchResult.new( intent: last.intent, entities: @slots.dup, pattern: last.pattern, score: last.score, input: input, response: last.response ) end end end @history << result result end end |
#reset ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/pattern_ruby/conversation.rb', line 63 def reset @mutex.synchronize do @context = nil @history = [] @slots = {} end end |
#set_context(ctx) ⇒ Object
55 56 57 |
# File 'lib/pattern_ruby/conversation.rb', line 55 def set_context(ctx) @mutex.synchronize { @context = ctx } end |