calyx 0.12.0 → 0.12.1
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/examples/tiny_woodland_bot.rb +0 -1
- data/lib/calyx/grammar.rb +7 -7
- data/lib/calyx/production/choices.rb +3 -2
- data/lib/calyx/production/concat.rb +3 -2
- data/lib/calyx/production/expression.rb +3 -2
- data/lib/calyx/production/memo.rb +2 -1
- data/lib/calyx/production/non_terminal.rb +3 -2
- data/lib/calyx/production/terminal.rb +2 -1
- data/lib/calyx/production/weighted_choices.rb +4 -4
- data/lib/calyx/registry.rb +11 -8
- 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: 5ef3b96315fd2ed6eb9352e21a11a4fe650c06cb
|
4
|
+
data.tar.gz: d5c9cb27ee6626cdae2fe30ceef5c6bdb369e260
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97709bec536e1f1ea619ec7f882184b4e4ec45c32b29371cd1c2ba30a88bbbcb0c637cce3d52ac5ebc6c37d642c8c51ff9fb47785a8c9775ffee0490d91bcdcd
|
7
|
+
data.tar.gz: 927d4e0ee0380e58299b97f6cffaae351575fbd15fa99e446c48c7a16dff9b90055625460862a533a02476748809aeb9840ce2b872549d0917f19b618b7ccc46
|
data/lib/calyx/grammar.rb
CHANGED
@@ -98,12 +98,12 @@ module Calyx
|
|
98
98
|
# Grammar rules can be constructed on the fly when the passed-in block is
|
99
99
|
# evaluated.
|
100
100
|
#
|
101
|
-
# @param [
|
102
|
-
def initialize(
|
103
|
-
if
|
104
|
-
@
|
101
|
+
# @param [Numeric, Random] random
|
102
|
+
def initialize(random=Random.new, &block)
|
103
|
+
if random.is_a?(Numeric)
|
104
|
+
@random = Random.new(random)
|
105
105
|
else
|
106
|
-
@
|
106
|
+
@random = random
|
107
107
|
end
|
108
108
|
|
109
109
|
if block_given?
|
@@ -123,7 +123,7 @@ module Calyx
|
|
123
123
|
def generate(*args)
|
124
124
|
start_symbol, rules_map = map_default_args(*args)
|
125
125
|
|
126
|
-
@registry.evaluate(start_symbol, @
|
126
|
+
@registry.evaluate(start_symbol, @random, rules_map).flatten.reject do |obj|
|
127
127
|
obj.is_a?(Symbol)
|
128
128
|
end.join(''.freeze)
|
129
129
|
end
|
@@ -137,7 +137,7 @@ module Calyx
|
|
137
137
|
def evaluate(*args)
|
138
138
|
start_symbol, rules_map = map_default_args(*args)
|
139
139
|
|
140
|
-
@registry.evaluate(start_symbol, @
|
140
|
+
@registry.evaluate(start_symbol, @random, rules_map)
|
141
141
|
end
|
142
142
|
|
143
143
|
private
|
@@ -34,9 +34,10 @@ module Calyx
|
|
34
34
|
|
35
35
|
# Evaluate the choice by randomly picking one of its possible options.
|
36
36
|
#
|
37
|
+
# @param [Random] random
|
37
38
|
# @return [Array]
|
38
|
-
def evaluate(
|
39
|
-
[:choice, @collection.sample(random:
|
39
|
+
def evaluate(random)
|
40
|
+
[:choice, @collection.sample(random: random).evaluate(random)]
|
40
41
|
end
|
41
42
|
end
|
42
43
|
end
|
@@ -50,10 +50,11 @@ module Calyx
|
|
50
50
|
# Evaluate all the child nodes of this node and concatenate them together
|
51
51
|
# into a single result.
|
52
52
|
#
|
53
|
+
# @param [Random] random
|
53
54
|
# @return [Array]
|
54
|
-
def evaluate(
|
55
|
+
def evaluate(random)
|
55
56
|
concat = @expansion.reduce([]) do |exp, atom|
|
56
|
-
exp << atom.evaluate(
|
57
|
+
exp << atom.evaluate(random)
|
57
58
|
end
|
58
59
|
|
59
60
|
[:concat, concat]
|
@@ -17,9 +17,10 @@ module Calyx
|
|
17
17
|
# terminal string, then passing it through the given modifier chain and
|
18
18
|
# returning the transformed result.
|
19
19
|
#
|
20
|
+
# @param [Random] random
|
20
21
|
# @return [Array]
|
21
|
-
def evaluate(
|
22
|
-
terminal = @production.evaluate(
|
22
|
+
def evaluate(random)
|
23
|
+
terminal = @production.evaluate(random).flatten.reject { |o| o.is_a?(Symbol) }.join(''.freeze)
|
23
24
|
expression = @methods.reduce(terminal) do |value, method|
|
24
25
|
@registry.transform(method, value)
|
25
26
|
end
|
@@ -15,9 +15,10 @@ module Calyx
|
|
15
15
|
|
16
16
|
# Evaluate the non-terminal, using the registry to handle the expansion.
|
17
17
|
#
|
18
|
+
# @param [Random] random
|
18
19
|
# @return [Array]
|
19
|
-
def evaluate(
|
20
|
-
[@symbol, @registry.expand(@symbol).evaluate(
|
20
|
+
def evaluate(random)
|
21
|
+
[@symbol, @registry.expand(@symbol).evaluate(random)]
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|
@@ -30,7 +30,6 @@ module Calyx
|
|
30
30
|
# Initialize a new choice with a list of child nodes.
|
31
31
|
#
|
32
32
|
# @param [Array] collection
|
33
|
-
# @param [Random] rng
|
34
33
|
def initialize(collection)
|
35
34
|
@collection = collection
|
36
35
|
end
|
@@ -41,13 +40,14 @@ module Calyx
|
|
41
40
|
# The method for selecting weighted probabilities is based on a snippet
|
42
41
|
# of code recommended in the Ruby standard library documentation.
|
43
42
|
#
|
43
|
+
# @param [Random] random
|
44
44
|
# @return [Array]
|
45
|
-
def evaluate(
|
45
|
+
def evaluate(random)
|
46
46
|
choice = @collection.max_by do |_, weight|
|
47
|
-
|
47
|
+
random.rand ** (1.0 / weight)
|
48
48
|
end.first
|
49
49
|
|
50
|
-
[:weighted_choice, choice.evaluate(
|
50
|
+
[:weighted_choice, choice.evaluate(random)]
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
data/lib/calyx/registry.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Calyx
|
2
2
|
# Lookup table of all the available rules in the grammar.
|
3
3
|
class Registry
|
4
|
-
attr_reader :
|
4
|
+
attr_reader :rules, :transforms, :modifiers
|
5
5
|
|
6
6
|
# Construct an empty registry.
|
7
7
|
def initialize
|
@@ -78,7 +78,7 @@ module Calyx
|
|
78
78
|
#
|
79
79
|
# @param [Symbol] symbol
|
80
80
|
def memoize_expansion(symbol)
|
81
|
-
memos[symbol] ||= expand(symbol).evaluate(@
|
81
|
+
memos[symbol] ||= expand(symbol).evaluate(@random)
|
82
82
|
end
|
83
83
|
|
84
84
|
# Merges the given registry instance with the target registry.
|
@@ -91,16 +91,19 @@ module Calyx
|
|
91
91
|
@rules = rules.merge(registry.rules)
|
92
92
|
end
|
93
93
|
|
94
|
+
|
95
|
+
|
94
96
|
# Evaluates the grammar defined in this registry, combining it with rules
|
95
97
|
# from the passed in context.
|
96
98
|
#
|
97
99
|
# Produces a syntax tree of nested list nodes.
|
98
100
|
#
|
99
101
|
# @param [Symbol] start_symbol
|
102
|
+
# @param [Random] random
|
100
103
|
# @param [Hash] rules_map
|
101
104
|
# @return [Array]
|
102
|
-
def evaluate(start_symbol=:start,
|
103
|
-
reset_evaluation_context(
|
105
|
+
def evaluate(start_symbol=:start, random=Random.new, rules_map={})
|
106
|
+
reset_evaluation_context(random)
|
104
107
|
|
105
108
|
rules_map.each do |key, value|
|
106
109
|
if rules.key?(key.to_sym)
|
@@ -117,7 +120,7 @@ module Calyx
|
|
117
120
|
expansion = expand(start_symbol)
|
118
121
|
|
119
122
|
if expansion.respond_to?(:evaluate)
|
120
|
-
[start_symbol, expansion.evaluate(
|
123
|
+
[start_symbol, expansion.evaluate(random)]
|
121
124
|
else
|
122
125
|
raise Errors::MissingRule.new(start_symbol)
|
123
126
|
end
|
@@ -125,10 +128,10 @@ module Calyx
|
|
125
128
|
|
126
129
|
private
|
127
130
|
|
128
|
-
attr_reader :
|
131
|
+
attr_reader :random, :memos, :context
|
129
132
|
|
130
|
-
def reset_evaluation_context(
|
131
|
-
@
|
133
|
+
def reset_evaluation_context(random)
|
134
|
+
@random = random
|
132
135
|
@context = {}
|
133
136
|
@memos = {}
|
134
137
|
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.12.
|
4
|
+
version: 0.12.1
|
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-
|
11
|
+
date: 2016-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|