Class: PatternRuby::Intent
- Inherits:
-
Object
- Object
- PatternRuby::Intent
- Defined in:
- lib/pattern_ruby/intent.rb
Instance Attribute Summary collapse
-
#compiled_patterns ⇒ Object
readonly
Returns the value of attribute compiled_patterns.
-
#context_requirement ⇒ Object
readonly
Returns the value of attribute context_requirement.
-
#entity_definitions ⇒ Object
readonly
Returns the value of attribute entity_definitions.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#response_text ⇒ Object
readonly
Returns the value of attribute response_text.
Instance Method Summary collapse
- #context(ctx) ⇒ Object
- #entity(name, type: :string, **options) ⇒ Object
-
#initialize(name, compiler:) ⇒ Intent
constructor
A new instance of Intent.
- #match(input, entity_registry: nil) ⇒ Object
- #pattern(pat) ⇒ Object
- #response(text) ⇒ Object
Constructor Details
#initialize(name, compiler:) ⇒ Intent
Returns a new instance of Intent.
5 6 7 8 9 10 11 12 |
# File 'lib/pattern_ruby/intent.rb', line 5 def initialize(name, compiler:) @name = name.to_sym @compiler = compiler @compiled_patterns = [] @entity_definitions = {} @response_text = nil @context_requirement = nil end |
Instance Attribute Details
#compiled_patterns ⇒ Object (readonly)
Returns the value of attribute compiled_patterns.
3 4 5 |
# File 'lib/pattern_ruby/intent.rb', line 3 def compiled_patterns @compiled_patterns end |
#context_requirement ⇒ Object (readonly)
Returns the value of attribute context_requirement.
3 4 5 |
# File 'lib/pattern_ruby/intent.rb', line 3 def context_requirement @context_requirement end |
#entity_definitions ⇒ Object (readonly)
Returns the value of attribute entity_definitions.
3 4 5 |
# File 'lib/pattern_ruby/intent.rb', line 3 def entity_definitions @entity_definitions end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/pattern_ruby/intent.rb', line 3 def name @name end |
#response_text ⇒ Object (readonly)
Returns the value of attribute response_text.
3 4 5 |
# File 'lib/pattern_ruby/intent.rb', line 3 def response_text @response_text end |
Instance Method Details
#context(ctx) ⇒ Object
33 34 35 |
# File 'lib/pattern_ruby/intent.rb', line 33 def context(ctx) @context_requirement = ctx end |
#entity(name, type: :string, **options) ⇒ Object
25 26 27 |
# File 'lib/pattern_ruby/intent.rb', line 25 def entity(name, type: :string, **) @entity_definitions[name.to_sym] = { type: type, ** } end |
#match(input, entity_registry: nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pattern_ruby/intent.rb', line 37 def match(input, entity_registry: nil) @compiled_patterns.each do |cp| md = cp.match(input) next unless md entities = extract_entities(md, cp, entity_registry) score = compute_score(cp, md, input) return MatchResult.new( intent: @name, entities: entities, pattern: cp.source, score: score, input: input, response: @response_text ) end nil end |
#pattern(pat) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pattern_ruby/intent.rb', line 14 def pattern(pat) if pat.is_a?(Regexp) @compiled_patterns << RegexPattern.new(pat) elsif pat.is_a?(String) raise ArgumentError, "pattern cannot be empty" if pat.strip.empty? @compiled_patterns << @compiler.compile(pat) else raise ArgumentError, "pattern must be a String or Regexp, got #{pat.class}" end end |
#response(text) ⇒ Object
29 30 31 |
# File 'lib/pattern_ruby/intent.rb', line 29 def response(text) @response_text = text end |