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

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

Instance Method Details

#confabulateObject



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

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

#parserObject



25
26
27
28
29
30
31
32
# File 'lib/confabulator/parser.rb', line 25

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

  @cached_parser
end