Skip to content

Instantly share code, notes, and snippets.

@johnnyt
Created June 24, 2015 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnnyt/9f48c636f5a061e2cad2 to your computer and use it in GitHub Desktop.
Save johnnyt/9f48c636f5a061e2cad2 to your computer and use it in GitHub Desktop.
URUG 2015-06-23 Trigram Kata - check out: github.com/urug/trigram_kata
module Generator
def self.generate(trigram, prompt, size)
content = prompt.split(' ')
while content.length < size
new_word = trigram.get content[-2..-1].join(' ')
content << new_word
end
content.join(' ')
end
end
class Trigram
attr_reader :text
def initialize(text)
@text = text
build_dict
end
def get(two_words)
@dict[two_words].sample
end
private
def build_dict
@dict = Hash.new {|hsh,key| hsh[key] = []}
@text.split(' ').each_cons(3) {|a,b,c| @dict["#{a} #{b}"] << c}
end
def build_dict2
@dict = Hash.new
@text.split(' ').each_cons(3) do |a,b,c|
key = "#{a} #{b}"
@dict[key] ||= []
@dict[key].push c
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment