Sha256: c293c7a08c56cbd322948c56fc944c19fde37a5ada5be190ec5172ce1cc15381
Contents?: true
Size: 1.01 KB
Versions: 2
Compression:
Stored size: 1.01 KB
Contents
require "text_generator/version" class MarkovGenerator attr_accessor :ngrams, :beginnings, :genmax def initialize(n, genmax) @size = n @ngrams = {} @genmax = genmax @beginnings = [] end def tokenize(line) line.strip.split(" ") end def feed(line) tokens = tokenize(line) return if tokens.length < @size @beginnings << tokens[0..@size-1] (0..(tokens.length - @size)).each do |i| ngram = tokens[i..i+@size-1].to_a next_gram = [tokens[i+@size]] if @ngrams.has_key? ngram @ngrams[ngram] << next_gram else @ngrams[ngram] = next_gram end end end def generate current = @beginnings.sample output = current.to_a while output.length <= @genmax-1 if @ngrams.has_key? current possible_next = @ngrams[current].flatten next_gram = possible_next.sample output << next_gram current = output.last(@size) else break end end return output.join(" ") end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
text_generator-0.0.2 | lib/text_generator.rb |
text_generator-0.0.1 | lib/text_generator.rb |