Class: Panini::DerivationStrategy::RandomDampened
- Defined in:
- lib/derivation_strategy/random_dampened.rb
Overview
This derivation strategy uses a dampening factor to reduce the liklihood of hitting either too-deep or infinite traversals through the grammar. This is based on material presented here:
eli.thegreenplace.net/2010/01/28/generating-random-sentences-from-a-context-free-grammar
Instance Method Summary collapse
-
#initialize(grammar, damping = 0.25) ⇒ RandomDampened
constructor
Initializes the derivator.
-
#sentence ⇒ Object
Generates a sentence.
Constructor Details
#initialize(grammar, damping = 0.25) ⇒ RandomDampened
Initializes the derivator. The damping factor is a number betweeon 0.0 and 1.0. In general, the smaller the number the shorter the senetence generated by the derivator. If the number is close to 1.0, it is possible that you will encounter stack errors!
69 70 71 72 73 74 75 |
# File 'lib/derivation_strategy/random_dampened.rb', line 69 def initialize(grammar, damping = 0.25) if (damping <= 0.0) || (damping >= 1.0) raise ArgumentError, "The damping factor must be greater than 0.0 and less than 1.0." end build_production_proxies(grammar, damping) super(grammar) end |
Instance Method Details
#sentence ⇒ Object
Generates a sentence.
86 87 88 |
# File 'lib/derivation_strategy/random_dampened.rb', line 86 def sentence substitute_nonterminal(@grammar.start, @production_proxies, 0) end |