Created
June 24, 2015 16:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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