Class: MarkovUuid::Markov

Inherits:
Object
  • Object
show all
Includes:
KeySelector
Defined in:
lib/markov_uuid/markov.rb

Constant Summary collapse

SEPARATOR =
"#-#-"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KeySelector

#new_key

Constructor Details

#initialize(words) ⇒ Markov

Returns a new instance of Markov.



10
11
12
# File 'lib/markov_uuid/markov.rb', line 10

def initialize words
  @words = words
end

Class Method Details

.from_file(f) ⇒ Object



27
28
29
# File 'lib/markov_uuid/markov.rb', line 27

def from_file( f )
  new lines(f).map { |l| from_string l }.flatten
end

Instance Method Details

#add_to(markov_data) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/markov_uuid/markov.rb', line 14

def add_to markov_data
  key = SEPARATOR

  @words.each do |word|
    (markov_data[key] ||= []) << word

    key = new_key(key, word)
  end
end

#to_s(i = 32) ⇒ Object



6
7
8
# File 'lib/markov_uuid/markov.rb', line 6

def to_s i=32
  @words.join("-")[0..i.to_i].gsub(/\A\w+-/,'').gsub(/-$/,"").gsub(/-\w+$/,"")
end