Class: MarkovDictionary
- Inherits:
-
Object
- Object
- MarkovDictionary
- Defined in:
- lib/marky_markov/markov_dictionary.rb
Direct Known Subclasses
Defined Under Namespace
Classes: FileNotFoundError
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
:nodoc:.
-
#dictionary ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#add_word(rootword, followedby) ⇒ Object
Given root word and what it is followed by, it adds them to the dictionary.
-
#initialize(depth = 2) ⇒ MarkovDictionary
constructor
A new instance of MarkovDictionary.
-
#open_source(source) ⇒ Object
Open supplied text file:.
-
#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.
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
#depth ⇒ Object (readonly)
:nodoc:
3 4 5 |
# File 'lib/marky_markov/markov_dictionary.rb', line 3 def depth @depth end |
#dictionary ⇒ Object (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.
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.
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 |