Class: ProgramR::GraphNode

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

Constant Summary collapse

MATCHERS =
%w{_ *}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraphNode

Returns a new instance of GraphNode.


9
10
11
12
# File 'lib/programr/graph_node.rb', line 9

def initialize
  @template = nil
  @children = {}
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children


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

def children
  @children
end

#templateObject (readonly)

Returns the value of attribute template


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

def template
  @template
end

Instance Method Details

#get_template(input, matched) ⇒ Object

Get template for input by the matching algorithm

Open docs.google.com/document/d/1wNT25hJRyupcG51aO89UcQEiG-HkXRXusukADpFnDs4/pub and search `7. AIML Pattern Matching` to get information about the matching algorithm

This function will only return `nil` or `@template` if path ending,

Parameters:

  • input (Array<String>)
  • matched (Array<String>)

80
81
82
# File 'lib/programr/graph_node.rb', line 80

def get_template input, matched
  get_template_without_matcher clean_undef_section(input), matched
end

#get_template_with_matcher(input_path, matched, matcher) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/programr/graph_node.rb', line 23

def get_template_with_matcher input_path, matched, matcher
  current_branch = input_path.first
  if input_path.empty?
    @template
  elsif matcher_ending? input_path, matched
    new_matched = []
    if template = get_template_without_matcher(input_path, new_matched)
      matched.concat new_matched
      template
    end
  else
    matched << current_branch
    get_template_with_matcher array_tail(input_path), matched, matcher
  end
end

#get_template_without_matcher(input_path, matched, current_section = nil) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/programr/graph_node.rb', line 39

def get_template_without_matcher input_path, matched, current_section = nil
  current_branch = input_path.first
  if current_branch == GraphMark::THAT
    current_section = GraphMark::THAT
  elsif current_branch == GraphMark::TOPIC
    current_section = GraphMark::TOPIC
  end

  if input_path.empty? or rest_section_not_exists_in_tree?(input_path)
    template = @template
  elsif @children.has_key? current_branch
    template = @children[current_branch].get_template_without_matcher(array_tail(input_path), matched, current_section)
  elsif current_branch == GraphMark::THAT
    skip_result = skip_that_section current_branch, input_path
    template = get_template_without_matcher skip_result[:input_path], matched
  end

  # if any node have @template, it must be an ending of a branch even
  # though it have @children
  return template if template

  MATCHERS.each do |matcher|
    new_matched = [current_branch]
    next unless @children.has_key? matcher
    next unless template = @children[matcher].get_template_with_matcher(array_tail(input_path), new_matched, matcher)
    matched.concat (current_section ? [current_section, '<star>'] : ['<star>']) + new_matched
    return template
  end

  nil
end

#inspect_nodeObject


85
86
87
88
89
90
91
92
93
94
# File 'lib/programr/graph_node.rb', line 85

def inspect_node branch = nil, indent_level = 0
  line_head = [0, 1].include?(indent_level) ? '' : '' * (indent_level - 2) + ''
  line_content = (branch ? branch.to_s : '') + (@template ? ": [#{@template.inspect}]" : '')
  line_tail = indent_level == 0 ? '' : "\n"
  str = line_head + line_content + line_tail
  @children.each_key do |child_branch|
    str += @children[child_branch].inspect_node(child_branch, indent_level + 1)
  end
  str
end

#learn(category, path) ⇒ Object

Parameters:

  • category (Category)
  • path (Array<String>)

16
17
18
19
20
21
# File 'lib/programr/graph_node.rb', line 16

def learn category, path
  branch = path.first  # Only the tail node has @template

  return @template = category.template if branch.nil?
  (@children[branch] ||= GraphNode.new).learn(category, array_tail(path))
end