Class: Markovian::Corpus::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/markovian/corpus/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(starter_corpus = Corpus.new) ⇒ Compiler

Returns a new instance of Compiler.



27
28
29
# File 'lib/markovian/corpus/compiler.rb', line 27

def initialize(starter_corpus = Corpus.new)
  @corpus = starter_corpus
end

Instance Attribute Details

#corpusObject (readonly)

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 parent corpus.



26
27
28
# File 'lib/markovian/corpus/compiler.rb', line 26

def corpus
  @corpus
end

Instance Method Details

#build_corpus(texts) ⇒ Object



31
32
33
34
# File 'lib/markovian/corpus/compiler.rb', line 31

def build_corpus(texts)
  texts.each {|t| incorporate_text_into_corpus(t)}
  corpus
end

#incorporate_text_into_corpus(text) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/markovian/corpus/compiler.rb', line 36

def incorporate_text_into_corpus(text)
  add_text_to_chain(split_into_components(text), forward_chain)
  # to assemble backward text, we just create a corpus with all the texts reversed
  # that allows us to see what words precede any given word
  add_text_to_chain(split_into_components(text).reverse, backward_chain)
  corpus
end