Class: ProgramR::Graphmaster

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphmaster

Returns a new instance of Graphmaster.


5
6
7
8
# File 'lib/programr/graphmaster.rb', line 5

def initialize
  @segmenter_map = {}
  reset
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph


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

def graph
  @graph
end

Instance Method Details

#get_reaction(stimula, last_said, cur_topic, starGreedy) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/programr/graphmaster.rb', line 22

def get_reaction stimula, last_said, cur_topic, starGreedy
  if @segmenter_map.empty?
    segmented_reaction = [stimula]
  else
    segmented_reaction = @segmenter_map.reduce [stimula] do |last_state, kvmap|
      kvmap.last.call last_state
    end
  end

  path = "#{segmented_reaction.join ' '} #{GraphMark::THAT} #{last_said} #{GraphMark::TOPIC} #{cur_topic}".split(/\s+/)
  template = @graph.get_template(path, starGreedy)
  template ? template.value : []
end

#learn(category) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
# File 'lib/programr/graphmaster.rb', line 10

def learn category
  segmenter = @segmenter_map[category.language]
  if category.language.nil? or segmenter.nil?
    path = category.patterns
  else
    path = segmenter.call category.patterns
  end
  path += [GraphMark::THAT] + category.thats unless category.thats.empty?
  path += [GraphMark::TOPIC] + category.topics unless category.topics.empty?
  @graph.learn(category, path)
end

#register_segmenter(lang, &block) ⇒ Object


44
45
46
47
48
# File 'lib/programr/graphmaster.rb', line 44

def register_segmenter lang, &block
  if block_given?
    @segmenter_map[lang] = block
  end
end

#resetObject


36
37
38
# File 'lib/programr/graphmaster.rb', line 36

def reset
  @graph = GraphNode.new
end

#to_sObject


40
41
42
# File 'lib/programr/graphmaster.rb', line 40

def to_s
  @graph.inspect_node
end