Class: MarkovGenerator
- Inherits:
-
Object
- Object
- MarkovGenerator
- Defined in:
- lib/text_generator.rb
Instance Attribute Summary collapse
-
#beginnings ⇒ Object
Returns the value of attribute beginnings.
-
#genmax ⇒ Object
Returns the value of attribute genmax.
-
#ngrams ⇒ Object
Returns the value of attribute ngrams.
Instance Method Summary collapse
- #feed(line) ⇒ Object
- #generate ⇒ Object
-
#initialize(n, genmax) ⇒ MarkovGenerator
constructor
A new instance of MarkovGenerator.
- #tokenize(line) ⇒ Object
Constructor Details
#initialize(n, genmax) ⇒ MarkovGenerator
Returns a new instance of MarkovGenerator.
6 7 8 9 10 11 |
# File 'lib/text_generator.rb', line 6 def initialize(n, genmax) @size = n @ngrams = {} @genmax = genmax @beginnings = [] end |
Instance Attribute Details
#beginnings ⇒ Object
Returns the value of attribute beginnings.
4 5 6 |
# File 'lib/text_generator.rb', line 4 def beginnings @beginnings end |
#genmax ⇒ Object
Returns the value of attribute genmax.
4 5 6 |
# File 'lib/text_generator.rb', line 4 def genmax @genmax end |
#ngrams ⇒ Object
Returns the value of attribute ngrams.
4 5 6 |
# File 'lib/text_generator.rb', line 4 def ngrams @ngrams end |
Instance Method Details
#feed(line) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/text_generator.rb', line 17 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 |
#generate ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/text_generator.rb', line 35 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 |
#tokenize(line) ⇒ Object
13 14 15 |
# File 'lib/text_generator.rb', line 13 def tokenize(line) line.strip.split(" ") end |