class Markovian::Chain::DictionaryEntry
Constants
- SIGNIFICANT_OCCURRENCE_THRESHOLD
Below this, we don’t have enough occurrences to draw conclusions about how a word is used. Longer-term, this could possibly be calculated in a more dynamic and effective way by analyzing the corpus itself.
- VALID_DIRECTIONS
Attributes
counts[R]
next_words[R]
for equality checking and other usage
previous_words[R]
for equality checking and other usage
word[R]
Public Class Methods
new(word)
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 10 def initialize(word) @word = word.to_s @next_words = [] @previous_words = [] @counts = Hash.new(0) end
Public Instance Methods
==(other)
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 42 def ==(other) other && self.word == other.word && self.next_words == other.next_words && self.previous_words == other.previous_words end
likelihood_to_end_sentence()
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 53 def likelihood_to_end_sentence # if we don't have enough data, we don't have enough data if occurrences >= SIGNIFICANT_OCCURRENCE_THRESHOLD counts[:ends_sentence].to_f / occurrences end end
next_word()
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 34 def next_word next_words.sample end
occurrences()
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 49 def occurrences counts[:total] end
previous_word()
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 38 def previous_word previous_words.sample end
push(next_word, direction: :forwards)
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 29 def push(next_word, direction: :forwards) # Also add the follwoing word array_for_direction(direction) << next_word.to_s end
record_observance(word_instance, direction: :forwards)
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 17 def record_observance(word_instance, direction: :forwards) # The word has been observed, so let's increase the appropriate counts. # We don't want to double-count words if we read the text both forward and backward, so # only count in the forward direction. (If we encounter a scenario where someone only wants # to read in the backward direction, we can deal with that then.) validate_direction(direction) if direction == :forwards @counts[:total] += 1 @counts[:ends_sentence] += 1 if word_instance.ends_sentence? end end
to_s()
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 60 def to_s word end
Protected Instance Methods
array_for_direction(direction)
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 71 def array_for_direction(direction) validate_direction(direction) direction == :backwards ? previous_words : next_words end
validate_direction(direction)
click to toggle source
# File lib/markovian/chain/dictionary_entry.rb, line 76 def validate_direction(direction) unless VALID_DIRECTIONS.include?(direction) raise ArgumentError.new("Invalid direction #{direction.inspect}, valid directions are #{VALID_DIRECTIONS.inspect}") end end