Class: Kotodama::Rule
- Inherits:
-
Object
- Object
- Kotodama::Rule
- Defined in:
- lib/rule.rb
Instance Attribute Summary collapse
-
#excludes ⇒ Object
readonly
Returns the value of attribute excludes.
-
#expressions ⇒ Object
readonly
Returns the value of attribute expressions.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#weights ⇒ Object
readonly
Returns the value of attribute weights.
Instance Method Summary collapse
- #add(expression, weight) ⇒ Object
- #generate ⇒ Object
-
#initialize(language) ⇒ Rule
constructor
A new instance of Rule.
Constructor Details
#initialize(language) ⇒ Rule
Returns a new instance of Rule.
5 6 7 8 9 10 |
# File 'lib/rule.rb', line 5 def initialize language @language = language @expressions = [] @weights = [] @excludes = [] end |
Instance Attribute Details
#excludes ⇒ Object (readonly)
Returns the value of attribute excludes.
3 4 5 |
# File 'lib/rule.rb', line 3 def excludes @excludes end |
#expressions ⇒ Object (readonly)
Returns the value of attribute expressions.
3 4 5 |
# File 'lib/rule.rb', line 3 def expressions @expressions end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
3 4 5 |
# File 'lib/rule.rb', line 3 def language @language end |
#weights ⇒ Object (readonly)
Returns the value of attribute weights.
3 4 5 |
# File 'lib/rule.rb', line 3 def weights @weights end |
Instance Method Details
#add(expression, weight) ⇒ Object
12 13 14 15 |
# File 'lib/rule.rb', line 12 def add expression, weight @expressions << expression @weights << (weight || @language.['$rule_weight'] || 1) end |
#generate ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rule.rb', line 17 def generate loop do expression = @expressions.random @weights output = [] expression.each do |atom| if @language.rules.key? atom output += @language.rules[atom].generate elsif @language.types.key? atom output << @language.types[atom].generate else raise "`#{atom}' is not associated with the given language" end end catch :fail do @excludes.each do |n| throw :fail if output.join.match /#{n}/ end return output end end end |