Class: Confabulator::Parser

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

Constant Summary collapse

REMOVE_SPACES =
/(\S+) {2,}(\S+)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, opts = {}) ⇒ Parser

Returns a new instance of Parser.



10
11
12
13
# File 'lib/confabulator/parser.rb', line 10

def initialize(str, opts = {})
  self.confabulation = str
  self.kb = opts[:knowledge]
end

Instance Attribute Details

#confabulationObject

Returns the value of attribute confabulation.



8
9
10
# File 'lib/confabulator/parser.rb', line 8

def confabulation
  @confabulation
end

#kbObject

Returns the value of attribute kb.



8
9
10
# File 'lib/confabulator/parser.rb', line 8

def kb
  @kb
end

Class Method Details

.expand_tree(s, combinations = [], x = nil, *a) ⇒ Object



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
70
71
# File 'lib/confabulator/parser.rb', line 42

def self.expand_tree(s, combinations = [], x = nil, *a)
  if !s.is_a?(String) && combinations == []
    expand_tree("", combinations, *s)
  else
    case x
      when Hash
        if x[:choices]
          x[:choices].each {|c| expand_tree(s, combinations, c, *a)}
        elsif x[:pluralize]
          tree_below_here = expand_tree("", [], x[:pluralize]).map { |r| r.en.plural }
          expand_tree(s, combinations, { :choices => tree_below_here })
        elsif x[:capitalize]
          tree_below_here = expand_tree("", [], x[:capitalize])
          tree_below_here.each { |r| r[0] = r[0].upcase if r[0] }
          expand_tree(s, combinations, { :choices => tree_below_here })
        else
          raise "Hash found without :choices, :pluralize, or :capitalize in it: #{x.inspect}"
        end
      when Array
        expand_tree(s, combinations, *x, *a)
      when String
        expand_tree(s+x, combinations, *a)
      when nil
        combinations << s
      else
        raise "Non String, Array, Hash, or nil value in expand_tree: #{x.inspect}"
    end
    combinations
  end
end

.remove_singleton_arrays(arr) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/confabulator/parser.rb', line 73

def self.remove_singleton_arrays(arr)
  if arr.is_a?(Array)
    if arr.length == 1
      remove_singleton_arrays(arr.first)
    else
      arr.map { |a| remove_singleton_arrays(a) }
    end
  else
    arr
  end
end

Instance Method Details

#all_confabulationsObject



25
26
27
# File 'lib/confabulator/parser.rb', line 25

def all_confabulations
  self.class.expand_tree(tree)
end

#confabulateObject



15
16
17
18
19
20
21
22
23
# File 'lib/confabulator/parser.rb', line 15

def confabulate
  if parser
    result = parser.confabulate(kb)
    result.gsub!(REMOVE_SPACES, '\1 \2') while result =~ REMOVE_SPACES
    result
  else
    ""
  end
end

#parserObject



33
34
35
36
37
38
39
40
# File 'lib/confabulator/parser.rb', line 33

def parser
  if !@parsed # this caches even a nil result
    @cached_parser = ConfabulatorLanguageParser.new.parse(confabulation)
    @parsed = true
  end

  @cached_parser
end

#treeObject



29
30
31
# File 'lib/confabulator/parser.rb', line 29

def tree
  parser ? parser.tree(kb) : []
end