Class: RubyLabs::ElizaLab::Rule

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

Overview

Rule

A transformation rule is associated with a key word, and is triggered when that word is found in an input sentence. Rules have integer priorities, and if more than one rule is enabled Eliza applies the one with the highest priority.

Each rule has an ordered list of patterns, which control how Eliza will respond to sentences containing the key word (see the Pattern class).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, priority = 1) ⇒ Rule

Create a new Rule object for sentences containing the word key. An optional second argument specifies the rule’s priority (the default is 1, which is the lowest priority). The list of patterns is initially empty.



48
49
50
51
52
# File 'lib/elizalab.rb', line 48

def initialize(key, priority = 1)
  @key = key
  @priority = priority
  @patterns = Array.new
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



42
43
44
# File 'lib/elizalab.rb', line 42

def key
  @key
end

#patternsObject

Returns the value of attribute patterns.



42
43
44
# File 'lib/elizalab.rb', line 42

def patterns
  @patterns
end

#priorityObject

Returns the value of attribute priority.



42
43
44
# File 'lib/elizalab.rb', line 42

def priority
  @priority
end

Instance Method Details

#<(x) ⇒ Object

Compare this Rule with another Rule object x based on their priority attributes. The rule comparison operator is used when a Rule is added to a priority queue. – The >= operator in the method body is important, in order to make sure the default rule stays at the end of the queue (i.e. new rules will be inserted at the front).



61
62
63
# File 'lib/elizalab.rb', line 61

def <(x)
  @priority >= x.priority
end

#[](n) ⇒ Object

Return a reference to sentence pattern n associated with this rule.



82
83
84
# File 'lib/elizalab.rb', line 82

def [](n)
  @patterns[n]
end

#addPattern(expr) ⇒ Object

Add a new sentence pattern (represented by a Pattern object) to the list of patterns for this rule. expr can either be a reference to an existing Pattern object, or a string, in which case a new Pattern is created.



69
70
71
72
73
74
75
76
77
78
# File 'lib/elizalab.rb', line 69

def addPattern(expr)
  if expr.class == Pattern
    @patterns << expr
  else
    if expr.class == String
      expr = Regexp.new(expr.slice(1..-2))
    end
    @patterns << Pattern.new(expr)
  end
end

#addReassembly(line, n = -1)) ⇒ Object

Helper method called by methods that read scripts from a file – add a response string to sentence pattern n.



89
90
91
# File 'lib/elizalab.rb', line 89

def addReassembly(line, n = -1)
  @patterns[n].add_response(line)
end

#apply(s, opt) ⇒ Object

Apply this rule to a sentence s. Try the patterns in order, to see if any of them match s.

When s matches a pattern, return the next reassembly for that pattern. Apply variable substitutions to both the patterns and the reassemblies if they contain variables. Return nil if no patterns apply to s.

The second argument, opt, is a symbol that is passed to Pattern#apply to control whether or not it should do preprocessing. Possible values are :preprocess or :no_preprocess.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/elizalab.rb', line 100

def apply(s, opt)
  @patterns.each do |p|
    if @@verbose
      print "trying pattern "
      p p.regexp
    end
    res = p.apply(s, opt)
    return res if ! res.nil?
  end
  return nil    
end

#inspectObject

Create a string that describes the attributes of this Rule object.



122
123
124
125
126
127
128
# File 'lib/elizalab.rb', line 122

def inspect
  # s = @key.inspect
  s = ""
  s += " [#{@priority}]" if @priority > 1
  s += " --> [\n" + @patterns.join("\n") + "]"
  return s
end

#to_sObject

Create a string that contains the rule name and priority.



114
115
116
117
118
# File 'lib/elizalab.rb', line 114

def to_s
  s = @key + " / " + @priority.to_s + "\n"
  @patterns.each { |r| s += "  " + r.to_s + "\n" }
  return s
end