Class: MarkyMarkov::Dictionary

Inherits:
TemporaryDictionary show all
Defined in:
lib/marky_markov.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TemporaryDictionary

#clear!, #dictionary, #generate_n_sentences, #generate_n_words, #method_missing, #parse_file, #parse_string, #respond_to_missing?

Constructor Details

#initialize(location, depth = 2) ⇒ Dictionary

Returns a new instance of Dictionary.


135
136
137
138
139
# File 'lib/marky_markov.rb', line 135

def initialize(location, depth=2)
  @dictionarylocation = "#{location}.mmd"
  @dictionary = PersistentDictionary.new(@dictionarylocation, depth)
  @sentence = MarkovSentenceGenerator.new(@dictionary)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MarkyMarkov::TemporaryDictionary

Instance Attribute Details

#dictionarylocationObject (readonly)

Open (or create if it doesn't exist) a Persistent Markov Chain Dictionary and sentence generator for use. Optional dictionary depth may be supplied.

Examples:

Create a new Persistent Dictionary object.

markov = MarkyMarkov::Dictionary.new("#{ENV["HOME"]}/markov_dictionary")

Create a new Persistent Dictionary object with a depth of 4.

markov = MarkyMarkov::Dictionary.new('mmdict.mmd'. 4)

Parameters:

  • location (File)

    The location the dictionary file is/will be stored.

  • depth (Int)

    The depth of the dictionary. Defaults to 2.


134
135
136
# File 'lib/marky_markov.rb', line 134

def dictionarylocation
  @dictionarylocation
end

Class Method Details

.delete_dictionary!(location) ⇒ Object

Note:

To ensure that someone doesn't pass in something that shouldn't

Takes a dictionary location/name and deletes it from the file-system. Alternatively, pass in a MarkyMarkov::Dictionary object in directly and it will delete that objects dictionary from disk.

be deleted by accident, the filetype .mmd is added to the end of the supplied argument, so do not include the extension when calling the method.

Examples:

Delete the dictionary located at '~/markov_dictionary.mmd'

MarkyMarkov::Dictionary.delete_dictionary!("#{ENV["HOME"]}/markov_dictionary")

Delete the dictionary of the object 'markov'

MarkyMarkov::Dictionary.delete_dictionary!(markov)

Parameters:

  • location (String/Object)

    location/name of the dictionary file to be deleted.


162
163
164
165
# File 'lib/marky_markov.rb', line 162

def self.delete_dictionary!(location)
  location += ".mmd" if location.class == String
  PersistentDictionary.delete_dictionary!(location)
end

Instance Method Details

#save_dictionary!Object

Save the Persistent Dictionary file into JSON format for later use.

Examples:

Save the dictionary to disk.

markov.save_dictionary!

145
146
147
# File 'lib/marky_markov.rb', line 145

def save_dictionary!
  @dictionary.save_dictionary!
end