Module: RandomData::Grammar

Included in:
Random
Defined in:
lib/random_data/grammar.rb

Overview

Defines methods for the generation of random data based on a supplied grammar.

Instance Method Summary collapse

Instance Method Details

#grammatical_construct(grammar, what = nil) ⇒ Object

Returns simple sentences based on a supplied grammar, which must be a hash, the keys of which are symbols. The values are either an array of successive values or a grammar (i.e, hash with symbols as keys, and hashes or arrays as values. The arrays contain symbols referencing the keys in the present grammar, or strings to be output. The keys are always symbols.

Example: Random.grammatical_construct(=> [:man, “ bites ”, :dog], :man => { :bob => “Bob”, :dog => =>“Rex”, :b =>“Rover”}, :story)

> “Bob bites Rover”



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/random_data/grammar.rb', line 17

def grammatical_construct(grammar, what=nil)
  output = ""
  if what.nil?
    case grammar
    when Hash
      a_key = grammar.keys.sort_by{rand}[0]
      output += grammatical_construct(grammar, a_key)
    when Array
      grammar.each do |item|
        output += grammatical_construct(item)
      end
    when String
      output += grammar
    end
  else
    rhs = grammar[what]
    case rhs
    when Array
      rhs.each do |item|
        case item
        when Symbol
          output += grammatical_construct(grammar,item)
        when String
          output += item
        when Hash
          output += grammatical_construct(item)
        else
          raise "#{item.inspect} must be a symbol or string or Hash"
        end
      end
    when Hash
      output+= grammatical_construct(rhs)
    when Symbol
      output += grammatical_construct(rhs)
    when String
      output += rhs
    else
      raise "#{rhs.inspect} must be a symbol, string, Array or Hash"
    end
  end
  return output
end