Class: Natter::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/natter/rule.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pattern = //, 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. If none is

specified then we'll assume it's a global rule (default: '').

entities - Optional array of Entity objects in the form:

Each entity must have a correspondingly named capture group 
within the regex.


30
31
32
33
34
35
# File 'lib/natter/rule.rb', line 30

def initialize(name, pattern = //, skill = '', *entities)
  @name = name
  @pattern = pattern
  @skill = skill
  @entities = entities || []
end

Instance Attribute Details

#entitiesObject

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



13
14
15
# File 'lib/natter/rule.rb', line 13

def entities
  @entities
end

#nameObject

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



9
10
11
# File 'lib/natter/rule.rb', line 9

def name
  @name
end

#patternObject

The regex pattern to search for within the an utterance.



11
12
13
# File 'lib/natter/rule.rb', line 11

def pattern
  @pattern
end

#skillObject

The name of the skill that this rule belongs to. May be ” if global.



15
16
17
# File 'lib/natter/rule.rb', line 15

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.

skill - The name of the skill that this this rule belongs to.

Example

def = Rule.new(‘play’).belongs_to(‘sonos’)



69
70
71
72
# File 'lib/natter/rule.rb', line 69

def belongs_to(skill)
  @skill = skill
  return self
end

#identifierObject

Public: Returns this rule’s identifier. An identifier is in the format: skill.name unless skill = ” in which case we return global.name

Returns string



57
58
59
# File 'lib/natter/rule.rb', line 57

def identifier
  "#{@skill == '' ? 'global' : @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)



85
86
87
88
89
90
91
# File 'lib/natter/rule.rb', line 85

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)


48
49
50
51
# File 'lib/natter/rule.rb', line 48

def regex(pattern)
  @pattern = pattern
  return self
end