Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
unleashy committed Jun 27, 2017
1 parent 84ebc83 commit 9379e5b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -4,6 +4,9 @@ AllCops:
Layout/EndOfLine:
EnforcedStyle: lf

Metrics/LineLength:
Max: 120

Metrics/AbcSize:
Enabled: false

Expand Down
30 changes: 26 additions & 4 deletions lib/markov_noodles/markov_noodles.rb
Expand Up @@ -4,18 +4,32 @@

# This is the main MarkovNoodles class.
class MarkovNoodles
# The dictionary managed by this instance. This is a hash, with
# array keys (length depending on {#depth}), and array values.
# @return [Hash{Array<String> => Array<String>}] the dictionary
attr_reader :dictionary

# The depth of Markov chains. This defines how many elements are in the
# hash keys of {#dictionary}.
attr_reader :depth

# Create a new MarkovNoodles instance with an empty dictionary.
# @param [Integer] depth the depth of the markov chain.
# @return [MarkovNoodles] the new MarkovNoodles instance.
def initialize(depth = 2)
@depth = depth
@dictionary = Hash.new { |hash, missing_word| hash[missing_word] = [] }
end

# Analyses a file and integrates it into the dictionary.
# All this does is pass `File.read(filename)` to {#analyse_string}.
# @param [String] filename the name of the file to analyse
def analyse_file(filename)
analyse_string(File.read(filename))
end

# Analyses a string and integrates it into the dictionary.
# @param [String] text the text to analyse
def analyse_string(text)
current_words = Array.new(depth)
text_array = split_text_to_array(text)
Expand All @@ -27,20 +41,28 @@ def analyse_string(text)
end
end

# Saves the dictionary in this instance to a file, to be restored later by {#load_dictionary}.
# Note that this overwrites the given file!
# @param [String] filename the name of the file to store the dictionary in
def save_dictionary(filename)
File.open(filename, 'w') do |file|
file.write @dictionary.to_msgpack
end
File.write(filename, @dictionary.to_msgpack, mode: 'w')
end

# Loads a dictionary saved with {#save_dictionary} into this instance.
# @param [String] filename the name of the file to restore the dictionary from
def load_dictionary(filename)
@dictionary = MessagePack.unpack(File.read(filename))
end

# Generates the specified amount of sentences using {#generate_sentence}, separating them with spaces.
# @param [Integer] n the amount of sentences to generate
# @return [String] the generated sentences separated by spaces
def generate_n_sentences(n)
Array.new(n) { generate_sentence }.join(' ')
end

# Generates a single sentence, using the current dictionary.
# @return [String] the generated sentence
def generate_sentence
current_words = Array.new(depth)
sentence_array = []
Expand All @@ -62,7 +84,7 @@ def generate_sentence

private

# Splits a text into array and inserts @depth nils after each sentence.
# Splits a text into array and inserts {#depth} nils after each sentence.
#
# This way generated texts can start with any word that is at the beginning of
# any sentence in analysed text, instead of always starting with the first
Expand Down

0 comments on commit 9379e5b

Please sign in to comment.