Class: MarkovChains::Dictionary
- Inherits:
-
Object
- Object
- MarkovChains::Dictionary
- Defined in:
- lib/markov_chains/dictionary.rb
Instance Attribute Summary collapse
-
#order ⇒ Object
readonly
Returns the value of attribute order.
Instance Method Summary collapse
-
#get(words) ⇒ String
Returns a word based on the likelihood of it appearing after the input array of words.
-
#get_start_words ⇒ [String]
Returns a list of words beginning a sentence seen in the source.
-
#initialize(text, order = 1) ⇒ Dictionary
constructor
Initialized the dictionary with a text source.
Constructor Details
#initialize(text, order = 1) ⇒ Dictionary
Initialized the dictionary with a text 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
#order ⇒ Object (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
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
53 54 55 |
# File 'lib/markov_chains/dictionary.rb', line 53 def get_start_words @start_words.sample end |