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.
- checksums.yaml +4 -4
- data/README.md +13 -2
- data/lib/calyx.rb +22 -12
- data/lib/calyx/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4116e94128a27a95985b8fa9780de2eabfec427c
|
4
|
+
data.tar.gz: 5b396249d4439e4a849b57ded682778e9b695be0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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
|
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
|
|
data/lib/calyx.rb
CHANGED
@@ -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(
|
21
|
-
@rules.merge
|
22
|
+
def combine(registry)
|
23
|
+
@rules = rules.merge(registry.rules)
|
22
24
|
end
|
23
25
|
|
24
|
-
def evaluate
|
25
|
-
|
26
|
-
|
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
|
-
|
29
|
-
|
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(
|
57
|
-
registry.combine(
|
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
|
data/lib/calyx/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2016-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|