Class: Confabulator::Parser
- Inherits:
-
Object
- Object
- Confabulator::Parser
- Defined in:
- lib/confabulator/parser.rb
Constant Summary collapse
- REMOVE_SPACES =
/(\S+) {2,}(\S+)/
Instance Attribute Summary collapse
-
#confabulation ⇒ Object
Returns the value of attribute confabulation.
-
#kb ⇒ Object
Returns the value of attribute kb.
Class Method Summary collapse
Instance Method Summary collapse
- #all_confabulations ⇒ Object
- #confabulate ⇒ Object
-
#initialize(str, opts = {}) ⇒ Parser
constructor
A new instance of Parser.
- #parser ⇒ Object
- #tree ⇒ Object
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
#confabulation ⇒ Object
Returns the value of attribute confabulation.
8 9 10 |
# File 'lib/confabulator/parser.rb', line 8 def confabulation @confabulation end |
#kb ⇒ Object
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.(s, combinations = [], x = nil, *a) if !s.is_a?(String) && combinations == [] ("", combinations, *s) else case x when Hash if x[:choices] x[:choices].each {|c| (s, combinations, c, *a)} elsif x[:pluralize] tree_below_here = ("", [], x[:pluralize]).map { |r| r.en.plural } (s, combinations, { :choices => tree_below_here }) elsif x[:capitalize] tree_below_here = ("", [], x[:capitalize]) tree_below_here.each { |r| r[0] = r[0].upcase if r[0] } (s, combinations, { :choices => tree_below_here }) else raise "Hash found without :choices, :pluralize, or :capitalize in it: #{x.inspect}" end when Array (s, combinations, *x, *a) when String (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_confabulations ⇒ Object
25 26 27 |
# File 'lib/confabulator/parser.rb', line 25 def all_confabulations self.class.(tree) end |
#confabulate ⇒ Object
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 |
#parser ⇒ Object
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 |
#tree ⇒ Object
29 30 31 |
# File 'lib/confabulator/parser.rb', line 29 def tree parser ? parser.tree(kb) : [] end |