Class: MarkovDictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/marky_markov/markov_dictionary.rb

Direct Known Subclasses

PersistentDictionary

Defined Under Namespace

Classes: FileNotFoundError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(depth = 2) ⇒ MarkovDictionary

Returns a new instance of MarkovDictionary.



4
5
6
7
8
# File 'lib/marky_markov/markov_dictionary.rb', line 4

def initialize(depth=2) @dictionary = {}
  @depth = depth
  @split_words = /(\.\s+)|(\.$)|([?!])|[\s]+/
  @split_sentence = /(?<=[.!?])\s+/
end

Instance Attribute Details

#depthObject (readonly)

:nodoc:



3
4
5
# File 'lib/marky_markov/markov_dictionary.rb', line 3

def depth
  @depth
end

#dictionaryObject (readonly)

:nodoc:



3
4
5
# File 'lib/marky_markov/markov_dictionary.rb', line 3

def dictionary
  @dictionary
end

Instance Method Details

#add_word(rootword, followedby) ⇒ Object

Given root word and what it is followed by, it adds them to the dictionary.

Examples:

Adding a word

add_word("Hello", "world")

Adding a multi-word dictionary

add_word("You are", "awesome")


29
30
31
32
# File 'lib/marky_markov/markov_dictionary.rb', line 29

def add_word(rootword, followedby)
  @dictionary[rootword] ||= []
  @dictionary[rootword] << followedby
end

#open_source(source) ⇒ Object

Open supplied text file:



15
16
17
18
19
20
21
# File 'lib/marky_markov/markov_dictionary.rb', line 15

def open_source(source)
  if File.exists?(source)
    File.open(source, "r").read.split(@split_sentence)
  else
    raise FileNotFoundError.new("#{source} does not exist!")
  end
end

#parse_source(source, file = true) ⇒ Object

Given a source of text, be it a text file (file=true) or a string (file=false) it will add all the words within the source to the markov dictionary.

Examples:

Add a text file

parse_source("text.txt")

Add a string

parse_source("Hi, how are you doing?", false)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/marky_markov/markov_dictionary.rb', line 41

def parse_source(source, file=true)
  if !source.nil?
    contents = file ? open_source(source) : contents = source.split(@split_sentence)
  else
    contents = []
  end
  if( !contents.empty? && !['.', '!', '?'].include?( contents[-1].strip[-1] ) )
    contents[-1] = contents[-1].strip + '.'
  end
  contents.map! {|sentence| sentence.gsub(/["()]/,"")}
  contents.each do |sentence|
    sentence.split(@split_words).each_cons(@depth+1) do |words|
      self.add_word(words[0..-2], words[-1])
    end
  end
end