Class: Natter::Rule
- Inherits:
-
Object
- Object
- Natter::Rule
- Defined in:
- lib/natter/rule.rb
Overview
Public: The Rule class represents a rule which defines an in intent that occurs within an utterance.
Constant Summary collapse
- NO_SKILL =
Identifier for rules added without an owning skill.
"_None"
Instance Attribute Summary collapse
-
#entities ⇒ Object
An array of Entity objects this rule expects.
-
#name ⇒ Object
The string name of this rule.
-
#pattern ⇒ Object
The regex pattern to search for within the an utterance.
-
#skill ⇒ Object
The name of the skill that this rule belongs to.
Instance Method Summary collapse
-
#belongs_to(skill) ⇒ Object
Public: A convenience method to permit prettier building of rules.
-
#identifier ⇒ Object
Public: Returns this rule’s identifier.
-
#initialize(name, pattern = //, skill = NO_SKILL, *entities) ⇒ Rule
constructor
Public: Constructor.
-
#needs_entity(name, type) ⇒ Object
Public: A nicer syntax method for adding an Entity to this rule.
-
#regex(pattern) ⇒ Object
Public: A convenience method to permit prettier building of rules.
Constructor Details
#initialize(name, pattern = //, skill = NO_SKILL, *entities) ⇒ Rule
Public: Constructor.
name - The name of this intent. Names do not need to be unique
within a parser.
pattern - The regular expression that matches the contents of an
utterance. If this intent contains entities then the regex
must capture the entities within the utterance as named
capture groups. See examples below. Default: //
skill - The name of the skill that this rule belongs to. The first
letter will be capitalised (e.g. `sonos` => `Sonos`). If none
is passed then is set to Rule::NO_SKILL
entities - Optional array of Entity objects in the form:
Each entity must have a correspondingly named capture group
within the regex.
33 34 35 36 37 38 |
# File 'lib/natter/rule.rb', line 33 def initialize(name, pattern = //, skill = NO_SKILL, *entities) @name = name @pattern = pattern skill == NO_SKILL ? @skill = NO_SKILL : @skill = skill.capitalize @entities = entities || [] end |
Instance Attribute Details
#entities ⇒ Object
An array of Entity objects this rule expects. May be empty.
15 16 17 |
# File 'lib/natter/rule.rb', line 15 def entities @entities end |
#name ⇒ Object
The string name of this rule. Defferent rules can share the same name.
11 12 13 |
# File 'lib/natter/rule.rb', line 11 def name @name end |
#pattern ⇒ Object
The regex pattern to search for within the an utterance.
13 14 15 |
# File 'lib/natter/rule.rb', line 13 def pattern @pattern end |
#skill ⇒ Object
The name of the skill that this rule belongs to.
17 18 19 |
# File 'lib/natter/rule.rb', line 17 def skill @skill end |
Instance Method Details
#belongs_to(skill) ⇒ Object
Public: A convenience method to permit prettier building of rules. Returns the Rule back to the calling method to allowing chaining. The skill name will be capitalised (e.g. ‘sonos` => `Sonos`).
skill - The name of the skill that this this rule belongs to.
Example
def = Rule.new(‘play’).belongs_to(‘sonos’)
73 74 75 76 |
# File 'lib/natter/rule.rb', line 73 def belongs_to(skill) @skill = skill.capitalize return self end |
#identifier ⇒ Object
Public: Returns this rule’s identifier. An identifier is in the format ‘skill.name`.
Returns a string.
60 61 62 |
# File 'lib/natter/rule.rb', line 60 def identifier "#{@skill}.#{@name}" end |
#needs_entity(name, type) ⇒ Object
Public: A nicer syntax method for adding an Entity to this rule.
name - The name of this entity. Must be unique within this rule definition
and must match a named capture group within the definition's regex.
type - The entity’s type. Should be one of the predefined constants in
Natter::EntityType
Example
definition.needs_entity(‘artist’, EntityType::GENERIC)
89 90 91 92 93 94 95 |
# File 'lib/natter/rule.rb', line 89 def needs_entity(name, type) @entities.each do |entity| return if entity.name == name # this rule already contains this entity end @entities << Entity.new(name, type) return self end |
#regex(pattern) ⇒ Object
Public: A convenience method to permit prettier building of rules. Returns the Rule back to the calling method to allow chaining.
pattern - The regex pattern to assign to this rule
Examples
definition = Rule.new(‘reminder’).regex(/remind me to/i) definition = Rule.new(‘reminder’)\
.regex(/remind me/)\
.needs_entity('task', EntityType::GENERIC)
51 52 53 54 |
# File 'lib/natter/rule.rb', line 51 def regex(pattern) @pattern = pattern return self end |