Class: Calyx::Production::WeightedChoices

Inherits:
Object
  • Object
show all
Defined in:
lib/calyx/production/weighted_choices.rb

Overview

A type of production rule representing a map of possible rules with associated weights that define the expected probability of a rule being chosen.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ WeightedChoices

Initialize a new choice with a list of child nodes.

Parameters:

  • collection (Array)


33
34
35
# File 'lib/calyx/production/weighted_choices.rb', line 33

def initialize(collection)
  @collection = collection
end

Class Method Details

.parse(productions, registry) ⇒ Object

Parse a given list or hash of productions into a syntax tree of weighted choices.

Parameters:

  • productions (Array<Array>, Hash<#to_s, Float>)
  • registry (Calyx::Registry)

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/calyx/production/weighted_choices.rb', line 12

def self.parse(productions, registry)
  weights_sum = productions.reduce(0) do |memo, choice|
    memo += choice.last
  end

  raise Errors::InvalidDefinition, 'Weights must sum to 1' if weights_sum != 1.0

  choices = productions.map do |choice, weight|
    if choice.is_a?(String)
      [Concat.parse(choice, registry), weight]
    elsif choice.is_a?(Symbol)
      [NonTerminal.new(choice, registry), weight]
    end
  end

  self.new(choices)
end

Instance Method Details

#evaluate(random) ⇒ Array

Evaluate the choice by randomly picking one of its possible options, balanced according to the given weights.

The method for selecting weighted probabilities is based on a snippet of code recommended in the Ruby standard library documentation.

Parameters:

  • random (Random)

Returns:

  • (Array)


45
46
47
48
49
50
51
# File 'lib/calyx/production/weighted_choices.rb', line 45

def evaluate(random)
  choice = @collection.max_by do |_, weight|
    random.rand ** (1.0 / weight)
  end.first

  [:weighted_choice, choice.evaluate(random)]
end