calyx 0.6.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +13 -2
  3. data/lib/calyx.rb +22 -12
  4. data/lib/calyx/version.rb +1 -1
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e95edf8bf75786d7a458d5618358a672b1b45b98
4
- data.tar.gz: 432df67afb18be349d2fe7d1ee99d9f296f98c22
3
+ metadata.gz: 4116e94128a27a95985b8fa9780de2eabfec427c
4
+ data.tar.gz: 5b396249d4439e4a849b57ded682778e9b695be0
5
5
  SHA512:
6
- metadata.gz: 44e91eaf7bc772408fe8bf840d0e1f15554aba7db941b72113a52e856012512d3e8482e092de9d221a3d092b7f22835a0b488bc940ec46375cad3aa0696eccdd
7
- data.tar.gz: 7afd950288f64d1ab80c79caeb1177f6c9349b1b4b3c9f383a084d5d69f07b53715b338897641a500c6a5133c1b32ee91ee6ac5c2a840d5849f9e357f3244483
6
+ metadata.gz: 1a48eb785e80d97e999cae3dfe9b254ebb68982dffe959e7d823bbdc73204b4baab96803a1a6693f1fddb80d5f6fb21c6350d43ffcf99967ccabc7cd11df7908
7
+ data.tar.gz: 8b83890ae677018d977e4cc3435ac414de947806af2fe912eeab12fee541ba0d3daf206989fc0f0a227c3987f5f3184f853e048cf36365384f9a9c27feda7887
data/README.md CHANGED
@@ -90,7 +90,7 @@ class HelloWorld < Calyx::Grammar
90
90
  end
91
91
  ```
92
92
 
93
- Nesting and hierarchy can be manipulated to balance consistency with variation. The exact same word atoms can be combined in different ways to produce strikingly different resulting texts.
93
+ Nesting and hierarchy can be manipulated to balance consistency with novelty. The exact same word atoms can be combined in a variety of ways to produce strikingly different resulting texts.
94
94
 
95
95
  ```ruby
96
96
  module HelloWorld
@@ -114,7 +114,18 @@ end
114
114
 
115
115
  ### Random Sampling
116
116
 
117
- By default, the outcomes of generated rules are selected with Ruby’s built-in random number generator (as seen in methods like `Kernel.rand` and `Array.sample`). If you want to supply a weighted probability list, you can pass in arrays to the rule constructor, with the first argument being the template text string and the second argument being a float representing the probability between `0` and `1` of this choice being selected.
117
+ By default, the outcomes of generated rules are selected with Ruby’s built-in pseudorandom number generator (as seen in methods like `Kernel.rand` and `Array.sample`). To seed the random number generator, pass in a seed value (an integer) as the first argument to the constructor:
118
+
119
+ ```ruby
120
+ MyGrammar.new(12345)
121
+ Calyx::Grammar.new(12345, &rules)
122
+ ```
123
+
124
+ When a seed value isn’t supplied, `Time.new.to_i` is used as the default seed, which makes each run of the generator relatively unique.
125
+
126
+ ### Weighted Selection
127
+
128
+ If you want to supply a weighted probability list, you can pass in arrays to the rule constructor, with the first argument being the template text string and the second argument being a float representing the probability between `0` and `1` of this choice being selected.
118
129
 
119
130
  For example, you can model the triangular distribution produced by rolling 2d6:
120
131
 
@@ -1,6 +1,8 @@
1
1
  module Calyx
2
2
  class Grammar
3
3
  class Registry
4
+ attr_reader :rules
5
+
4
6
  def initialize
5
7
  @rules = {}
6
8
  end
@@ -17,16 +19,24 @@ module Calyx
17
19
  @rules[symbol]
18
20
  end
19
21
 
20
- def combine(rules)
21
- @rules.merge!(rules.to_h)
22
+ def combine(registry)
23
+ @rules = rules.merge(registry.rules)
22
24
  end
23
25
 
24
- def evaluate
25
- @rules[:start].evaluate
26
- end
26
+ def evaluate(context={})
27
+ context.each do |key, value|
28
+ if @rules.key?(key.to_sym)
29
+ raise "Rule already declared in grammar: #{key}"
30
+ end
27
31
 
28
- def to_h
29
- @rules
32
+ @rules[key.to_sym] = if value.is_a?(Array)
33
+ Production::Choices.parse(value, self)
34
+ else
35
+ Production::Concat.parse(value.to_s, self)
36
+ end
37
+ end
38
+
39
+ @rules[:start].evaluate
30
40
  end
31
41
 
32
42
  private
@@ -53,8 +63,8 @@ module Calyx
53
63
  registry.rule(name, *productions)
54
64
  end
55
65
 
56
- def inherit_registry(rules)
57
- registry.combine(rules) unless rules.nil?
66
+ def inherit_registry(child_registry)
67
+ registry.combine(child_registry) unless child_registry.nil?
58
68
  end
59
69
 
60
70
  def inherited(subclass)
@@ -199,12 +209,12 @@ module Calyx
199
209
  @registry = Registry.new
200
210
  @registry.instance_eval(&block)
201
211
  else
202
- @registry = self.class.registry
212
+ @registry = self.class.registry.clone
203
213
  end
204
214
  end
205
215
 
206
- def generate
207
- @registry.evaluate
216
+ def generate(context={})
217
+ @registry.evaluate(context)
208
218
  end
209
219
  end
210
220
  end
@@ -1,3 +1,3 @@
1
1
  module Calyx
2
- VERSION = '0.6.1'.freeze
2
+ VERSION = '0.7.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: calyx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rickerby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-12 00:00:00.000000000 Z
11
+ date: 2016-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler