Class: ProgramR::Facade

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_environment = Environment, custom_history = History) ⇒ Facade

Create a new robot

Parameters:

  • custom_environment (defaults to: Environment)

    the custom Environment class

  • custom_history (defaults to: History)

    the custom History class


9
10
11
12
13
14
# File 'lib/programr/facade.rb', line 9

def initialize custom_environment = Environment, custom_history = History
  @history      = custom_history.new
  @environment  = custom_environment.new @history
  @graphmaster = Graphmaster.new
  @parser       = AimlParser.new @graphmaster, @environment, @history
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment


3
4
5
# File 'lib/programr/facade.rb', line 3

def environment
  @environment
end

#historyObject (readonly)

Returns the value of attribute history


3
4
5
# File 'lib/programr/facade.rb', line 3

def history
  @history
end

Instance Method Details

#get_reaction(stimula) ⇒ String

Talk with robot

Parameters:

  • stimula (String)

    the message speak to robot

Returns:

  • (String)

    the message robot said


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/programr/facade.rb', line 32

def get_reaction(stimula, firstStimula = true)
  starGreedy = []
  #TODO verify if case insensitive. Cross check with parser
  @history.update_stimula(stimula.upcase) if firstStimula
  reaction = @graphmaster.get_reaction stimula.upcase, @history.that, @history.topic, starGreedy
  @history.update_star_matches starGreedy
  res = evaluate(reaction, starGreedy).flatten.reduce([]) do |memo, part|
    # clean case [" ", "part 1 ", " ", "part 2", " "]
    if !memo.last || !memo.last.end_with?(' ')
      memo << part
    elsif part && memo.last.end_with?(' ') && !part.strip.empty?
      memo << part
    end
    memo
  end.join('').strip
  #TODO verify if case insensitive. Cross check with main program & parser
  @history.update_response(res.upcase) if firstStimula
  res
end

#learn(content) ⇒ Object

Learn knowledges

Parameters:

  • content (String, Array<String>)

    folder array of aiml files or plain aiml content


19
20
21
22
23
24
25
# File 'lib/programr/facade.rb', line 19

def learn content
  if content.is_a? Array
    read_aiml(content) { |f| @parser.parse f }
  else
    @parser.parse content
  end
end

#register_segmenter(lang, &block) ⇒ Object

Note:

Graphmaster decide which segmenters process aiml partten by read `language` attribute of `Category` tag.

But it will not detect which language of user stimula, Graphmaster will pass stimula to all segmenter, block should decide what to do by it self.

Register segmenter for specified language

Parameters:

  • lang (Symbol)

    the language tag segmenter for

  • block (Block)

    the segmenter


63
64
65
# File 'lib/programr/facade.rb', line 63

def register_segmenter lang, &block
  @graphmaster.register_segmenter lang, &block
end

#resetObject

Reset Graphmaster


68
69
70
# File 'lib/programr/facade.rb', line 68

def reset
  @graphmaster.reset
end

#to_sObject


72
73
74
# File 'lib/programr/facade.rb', line 72

def to_s
  @graphmaster.to_s
end