I\'m working on a Ruby verison of RSG and somehow stuck on the sentence generating process (...)
so I managed to implement all functions like read, convert to hash...,et
I assume they're looking for a recursive method, let's call it generate
.
def generate(key)
Read the hash at the key and take one randomly using sample
:
words = @hash[key].sample
Then, for each word, check to see if it's a
. If so, call generate
on it, otherwise save it:
if (word.start_with?("<") && word.end_with?(">"))
generate(word)
else
@sentence << word
end
Putting it all together:
@hash = {""=>[["The", "
Notice I used @-variables to make their scope reachable from within the method.
Sample output: The big yellow flowers sigh grumpily tonight.