class Panini::DerivationStrategy::DampenedProbabilityProductionChoiceProxy

Public Class Methods

new(nonterminal, damping=0.25) click to toggle source
# File lib/derivation_strategy/random_dampened.rb, line 8
def initialize(nonterminal, damping=0.25)
  @nonterminal = nonterminal
  @damping = damping
  @production_counts = @nonterminal.productions.map do
    0
  end
end

Public Instance Methods

dump_weights() click to toggle source
# File lib/derivation_strategy/random_dampened.rb, line 49
def dump_weights
  puts "production_counts:"
  @weights.each do |weight|
    puts "#{weight} "
  end
end
initialize_copy(source) click to toggle source
Calls superclass method
# File lib/derivation_strategy/random_dampened.rb, line 16
def initialize_copy(source)
  super
  @production_counts = @production_counts.map do |production_count|
    production_count
  end
end
production() click to toggle source
# File lib/derivation_strategy/random_dampened.rb, line 23
def production
  i = find_index
  @production_counts[i] += 1
  @nonterminal.productions[i]
end

Private Instance Methods

find_index() click to toggle source
# File lib/derivation_strategy/random_dampened.rb, line 29
def find_index

  weights = @production_counts.map do |production_count|
    @damping ** production_count
  end

  selector = Kernel::rand() * weights.inject(:+)

  weights.each_with_index do |weight, i|
    selector -= weight
    if selector < 0
      return i
    end
  end

  raise "You shouldn't be able to get here. #{selector} #{@production_counts}"

end