Class: MarkyMarkov::TemporaryDictionary

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

Direct Known Subclasses

Dictionary

Instance Method Summary collapse

Constructor Details

#initialize(depth = 2) ⇒ Object

Create a new Temporary Markov Chain Dictionary and sentence generator for use. Depth defaults to two words but can be set to any number between 1 and 9.

Examples:

Create a new Temporary Dictionary.

markov = MarkyMarkov::TemporaryDictionary.new

Create a three word Temporary Dictionary.

markov = MarkyMarkov::TemporaryDictionary.new(3)

Parameters:

  • depth (Int) (defaults to: 2)

    Optional dictionary depth. Defaults to 2.



24
25
26
27
# File 'lib/marky_markov.rb', line 24

def initialize(depth=2)
  @dictionary = MarkovDictionary.new(depth)
  @sentence = MarkovSentenceGenerator.new(@dictionary)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, &block) ⇒ String

Dynamically call generate_n_words or generate_n_sentences if an Int is substituted for the n in the method call.

Examples:

Generate a 40 and a 1 word long string of words.

markov.generate_40_words
markov.generate_1_word

Generate 2 sentences

markov.generate_2_sentences

Returns:

  • (String)

    the sentence generated by the dictionary.

Since:

  • 0.1.4



91
92
93
94
95
96
97
98
99
# File 'lib/marky_markov.rb', line 91

def method_missing(method_sym, *args, &block)
  if method_sym.to_s =~ /^generate_(\d*)_word[s]*$/
    generate_n_words($1.to_i)
  elsif method_sym.to_s =~ /^generate_(\d*)_sentence[s]*$/
    generate_n_sentences($1.to_i)
  else
    super
  end
end

Instance Method Details

#clear!Object

Clears the temporary dictionary’s hash, useful for keeping the same dictionary object but removing the words it has learned.

Examples:

Clear the Dictionary hash.

markov.clear!


119
120
121
# File 'lib/marky_markov.rb', line 119

def clear!
  @dictionary.dictionary.clear
end

#dictionaryHash

Returns the MarkovDictionary objects dictionary hash.

Returns:

  • (Hash)

    the MarkovDictionary hash.



31
32
33
# File 'lib/marky_markov.rb', line 31

def dictionary
  @dictionary.dictionary
end

#generate_n_sentences(sentencecount) ⇒ String

Generates n sentences using the dictionary generated via parse_string or parse_file. A sentence is defined as beginning with a capitalized word and ending with either a . ! or ?

Examples:

Generate three sentences.

markov.generate_n_sentences(3)

Generate six sentences with method_missing.

markov.generate_6_sentences

Parameters:

  • wordcount (Int)

    the number of sentences you want generated.

Returns:

  • (String)

    the sentences generated by the dictionary.

Since:

  • 0.2.0



77
78
79
# File 'lib/marky_markov.rb', line 77

def generate_n_sentences(sentencecount)
  @sentence.generate_sentence(sentencecount)
end

#generate_n_words(wordcount) ⇒ String

Generates a sentence/sentences of n words using the dictionary generated via parse_string or parse_file.

Examples:

Generate a 40 word long string of words.

markov.generate_n_words(40)

Generate a 10 word long string of words with method_missing.

markov.generate_10_words

Parameters:

  • wordcount (Int)

    the number of words you want generated.

Returns:

  • (String)

    the sentence generated by the dictionary.



62
63
64
# File 'lib/marky_markov.rb', line 62

def generate_n_words(wordcount)
  @sentence.generate(wordcount)
end

#parse_file(location) ⇒ Object

Parses a given file and adds the sentences it contains to the current dictionary.

Examples:

Open a text file and add its contents to the dictionary.

markov.parse_file "text.txt"

Parameters:

  • location (File)

    the file you want to add to the dictionary.



40
41
42
# File 'lib/marky_markov.rb', line 40

def parse_file(location)
  @dictionary.parse_source(location, true)
end

#parse_string(string) ⇒ Object

Parses a given string and adds them to the current dictionary.

Examples:

Add a string to the dictionary.

markov.parse_string "I could really go for some Chicken Makhani."

Parameters:

  • string (String)

    the sentence you want to add to the dictionary.



49
50
51
# File 'lib/marky_markov.rb', line 49

def parse_string(string)
  @dictionary.parse_source(string, false)
end

#respond_to_missing?(method_sym, include_private) ⇒ Boolean

Modify respond_to_missing? to include generate_n_words and generate_n_sentences method_missing implementation.

Returns:

  • (Boolean)

Since:

  • 0.1.4



104
105
106
107
108
109
110
111
112
# File 'lib/marky_markov.rb', line 104

def respond_to_missing?(method_sym, include_private)
  if method_sym.to_s =~ /^generate_(\d*)_word[s]*$/
    true
  elsif method_sym.to_s =~ /^generate_(\d*)_sentence[s]*$/
    true
  else
    super
  end
end