Class: MarkovChains::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/markov_chains/dictionary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, order = 1) ⇒ Dictionary

Initialized the dictionary with a text source.

Examples:

Create a new dictionary of order 1

MarkovChains::Dictionary.new(string)

Create a new dictinary of order 2

MarkovChains::Dictionary.new(string, 2)

Parameters:

  • the (String)

    text source

  • the (int)

    order of the dictionary. The order is the “memory” of the dictionary, meaning that an order <n> dictionary will consider <n> words to generate the next one. Order of 1 or 2 are typical. More than 3 and the generated sentences will be the same as the source.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/markov_chains/dictionary.rb', line 15

def initialize(text, order = 1)
  @order = order
  @words_for = Hash.new
  @start_words = Array.new
  
  # Standarize input text
  text.delete! "\n"
  
  # Process each sentence
  
  # <sentences> has format sentence+terminator:
  #   ["sent1", "term1", "sent2", "term2", ...]
  seps = /([.!?]+)/
  sentences = text.split seps
  sentences.each_slice(2) { |s,t| process_sentence(s.strip,t) }
end

Instance Attribute Details

#orderObject (readonly)

Returns the value of attribute order.



3
4
5
# File 'lib/markov_chains/dictionary.rb', line 3

def order
  @order
end

Instance Method Details

#get(words) ⇒ String

Returns a word based on the likelihood of it appearing after the input array of words

Examples:

Get a word likely to appear next to the word ‘It’

get(['It'])           # => 'has'

Get a word likely to appear next to the words ‘It has been’ (with a dictionary of order 2)

get(['It has'])  # => 'been'

Parameters:

  • array ([String])

    of words for which we want a possible next word

Returns:

  • (String)

    word that is likely to follow the input



42
43
44
# File 'lib/markov_chains/dictionary.rb', line 42

def get(words)
  (@words_for[words] || []).sample
end

#get_start_words[String]

Returns a list of words beginning a sentence seen in the source

Examples:

Get a start words

get_start_word    # => ['It', 'has']

Returns:

  • ([String])

    array of words that could start a sentence



53
54
55
# File 'lib/markov_chains/dictionary.rb', line 53

def get_start_words
  @start_words.sample
end