Class: Unbelievable::Todo

Inherits:
Generator show all
Defined in:
lib/unbelievable/todo.rb

Constant Summary collapse

TEMPLATES =
[
  [ :verb                                                ],
  [ :verb,   :plural                                     ],
  [ :verb,   :singular                                   ],
  [ :adverb, :verb,      :plural                         ],
  [ :adverb, :verb,      :singular                       ],
# [ :verb,   :numeral,   :plural                         ],
# [ :verb,   :adjective, :plural                         ],
  [ :verb,   :adjective, :singular                       ],
  [ :adverb, :verb,      :adjective, :plural             ],
  [ :adverb, :verb,      :adjective, :singular           ],
# [ :adverb, :verb,      :numeral,   :plural             ],
  [ :verb,   :numeral,   :adjective, :plural             ],
  [ :adverb, :verb,      :numeral,   :adjective, :plural ]
]

Instance Method Summary collapse

Methods inherited from Generator

#initialize

Constructor Details

This class inherits a constructor from Unbelievable::Generator

Instance Method Details

#sentence(words) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/unbelievable/todo.rb', line 38

def sentence(words)
  # Pick templates with given number of words.
  templates = TEMPLATES.select { |template| template.size == words.size }

  sentences = templates.map do |template|
    vars = words.dup
    formatted = template.map { |var| dictionary[var.to_s].pick(vars.shift) }
    formatted.first.capitalize! unless formatted.first =~ /^[A-Z]|[\?!]$/
    formatted.join(" ")
  end

  sentences.sample
end

#story(words) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/unbelievable/todo.rb', line 24

def story(words)
  sentences = []

  while words.any? do
    sentences += [ 5, 4 ].shuffle.map do |n|
      found = sentence(words[0, n]) if words.any?
      words.shift(found.split.size) if found
      found
    end
  end

  sentences.compact.each_with_index.map { |todo, i| "#{i+1}. #{todo}" }.join("\n")
end