Class: PersistentDictionary
- Inherits:
-
MarkovDictionary
- Object
- MarkovDictionary
- PersistentDictionary
- Defined in:
- lib/marky_markov/persistent_dictionary.rb
Defined Under Namespace
Classes: DepthNotInRangeError
Instance Attribute Summary collapse
-
#depth ⇒ Object
readonly
Creates a PersistentDictionary object using the supplied dictionary file.
-
#dictionarylocation ⇒ Object
readonly
Creates a PersistentDictionary object using the supplied dictionary file.
Attributes inherited from MarkovDictionary
Class Method Summary collapse
-
.delete_dictionary!(dictionary) ⇒ Object
Deletes the supplied dictionary file.
Instance Method Summary collapse
-
#initialize(dictionary, depth = 2) ⇒ PersistentDictionary
constructor
A new instance of PersistentDictionary.
-
#open_dictionary ⇒ Object
Opens the dictionary objects dictionary file.
-
#save_dictionary! ⇒ Object
Saves the PersistentDictionary objects @dictionary hash to disk in JSON format.
Methods inherited from MarkovDictionary
#add_word, #open_source, #parse_source
Constructor Details
#initialize(dictionary, depth = 2) ⇒ PersistentDictionary
Returns a new instance of PersistentDictionary.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/marky_markov/persistent_dictionary.rb', line 16 def initialize(dictionary, depth=2) @depth = depth unless (1..5).include?(depth) raise DepthNotInRangeError.new("Depth must be between 1 and 5. For best results, use 2.") end @dictionarylocation = dictionary @split_words = /([.?!])|[\s]+/ @split_sentence = /(?<=[.!?])\s+/ self.open_dictionary end |
Instance Attribute Details
#depth ⇒ Object (readonly)
Creates a PersistentDictionary object using the supplied dictionary file.
15 16 17 |
# File 'lib/marky_markov/persistent_dictionary.rb', line 15 def depth @depth end |
#dictionarylocation ⇒ Object (readonly)
Creates a PersistentDictionary object using the supplied dictionary file.
15 16 17 |
# File 'lib/marky_markov/persistent_dictionary.rb', line 15 def dictionarylocation @dictionarylocation end |
Class Method Details
.delete_dictionary!(dictionary) ⇒ Object
Deletes the supplied dictionary file. Can either be passed the dictionary location and name, or a PersistentDictionary object.
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/marky_markov/persistent_dictionary.rb', line 54 def self.delete_dictionary!(dictionary) if dictionary.respond_to?(:dictionarylocation) dictionary = dictionary.dictionarylocation end if File.exists?(dictionary) File.delete(dictionary) "Deleted #{dictionary}" else "#{dictionary} does not exist." end end |
Instance Method Details
#open_dictionary ⇒ Object
Opens the dictionary objects dictionary file. If the file exists it assigns the contents to a hash, otherwise it creates an empty hash.
31 32 33 34 35 36 37 38 39 |
# File 'lib/marky_markov/persistent_dictionary.rb', line 31 def open_dictionary if File.exists?(@dictionarylocation) file = File.new(@dictionarylocation, 'rb').read @depth = file[0].to_i @dictionary = MessagePack.unpack(file[1..-1]) else @dictionary = {} end end |
#save_dictionary! ⇒ Object
Saves the PersistentDictionary objects @dictionary hash to disk in JSON format.
43 44 45 46 47 48 49 |
# File 'lib/marky_markov/persistent_dictionary.rb', line 43 def save_dictionary! packed = @dictionary.to_msgpack File.open(@dictionarylocation, 'wb') do |f| f.write @depth.to_s + packed end true end |