class Markovian::Chain::Compiler
Attributes
chain[R]
Pass in a text, and optionally an existing Markov chain to add data to. In many cases, you may be building a chain using a set of smaller texts instead of one large texts (dialog, for instance, or Twitter archives), and so may call this class repeatedly for elements of the corpus.
Public Class Methods
new(starter_chain = Chain.new)
click to toggle source
# File lib/markovian/chain/compiler.rb, line 27 def initialize(starter_chain = Chain.new) @chain = starter_chain end
Public Instance Methods
build_chain(texts)
click to toggle source
# File lib/markovian/chain/compiler.rb, line 31 def build_chain(texts) texts.each {|t| incorporate_text_into_chain(t)} chain end
incorporate_text_into_chain(text)
click to toggle source
# File lib/markovian/chain/compiler.rb, line 36 def incorporate_text_into_chain(text) add_text_to_chain(split_into_components(text), chain) chain end
Protected Instance Methods
add_text_to_chain(text_elements, chain)
click to toggle source
# File lib/markovian/chain/compiler.rb, line 43 def add_text_to_chain(text_elements, chain) previous_word = nil text_elements.each_with_index do |word, index| # if we're not at the beginning or the end of the text -- e.g. we have a full triple if next_word = text_elements[index + 1] chain.lengthen(word, next_word: next_word, previous_word: previous_word) end previous_word = word end end
split_into_components(text)
click to toggle source
# File lib/markovian/chain/compiler.rb, line 54 def split_into_components(text) Utils::TextSplitter.new(text).components end