class Natter::Rule

Public: The Rule class represents a rule which defines an in intent that occurs within an utterance.

Constants

NO_SKILL

Identifier for rules added without an owning skill.

Attributes

entities[RW]

An array of Entity objects this rule expects. May be empty.

name[RW]

The string name of this rule. Defferent rules can share the same name.

pattern[RW]

The regex pattern to search for within the an utterance.

skill[RW]

The name of the skill that this rule belongs to.

Public Class Methods

new(name, pattern = //, skill = NO_SKILL, *entities) click to toggle source

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.
# 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

Public Instance Methods

belongs_to(skill) click to toggle source

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')

# File lib/natter/rule.rb, line 73
def belongs_to(skill)
  @skill = skill.capitalize
  return self
end
identifier() click to toggle source

Public: Returns this rule's identifier. An identifier is in the format `skill.name`.

Returns a string.

# File lib/natter/rule.rb, line 60
def identifier
  "#{@skill}.#{@name}"
end
needs_entity(name, type) click to toggle source

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)

# 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) click to toggle source

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)
# File lib/natter/rule.rb, line 51
def regex(pattern)
  @pattern = pattern
  return self
end