Method: MarkovTwitter::MarkovBuilder::Node#add_linkage!

Defined in:
lib/markov_twitter/markov_builder/node.rb

#add_linkage!(direction, other_node, probability, mirror_change = true) ⇒ void

This method returns an undefined value.

Force-adds a linkage at a specific probability. Readjusts other probabilities but may break their proportionality. Updates the opposite direction to keep :next and :prev in sync

Parameters:

  • direction (Symbol)
  • other_node (Node)
  • probability (Float)

    between 0 and 1.

  • mirror_change (Boolean) (defaults to: true)

    whether to update the opposite direction.

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/markov_twitter/markov_builder/node.rb', line 127

def add_linkage!(direction, other_node, probability, mirror_change=true)
  raise ArgumentError, "invalid probability" unless probability.between?(0,1)
  # first remove any existing node there and distribute the probability.
  delete_linkage!(direction, other_node)
  # Re-adjust each probability to account for the added value
  linkages[direction].each_key do |key|
    linkages[direction][key] *= (1 - probability)
    # remove the linkage if it's probability is zero
    if linkages[direction][key].zero?
      delete_linkage!(direction, @nodes[key])
    end
  end
  # Add the new value and set its probability
  binding.pry if other_node.value == "dog"
  linkages[direction][other_node.value] = probability
  # increment the total count
  total_num_inputs[direction] += 1
  # Add a linkage in the opposite direction to keep :next and :prev in sync      
  if mirror_change
    update_opposite_direction(direction, other_node, __method__, probability)
  end
end