calyx 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/calyx.rb +43 -22
- 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: 3d9b72d66ec75106a9818da21e7f192c319dfa0d
|
4
|
+
data.tar.gz: 7f60dbb2787d4bf248a90b09bbf8639f8ce9afbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e686317e43831dfb82e03b387d396be5dc86d440d8238772cda8ccefb9da10991ddacbb49954cea3c16815207d5b3a501c2505df40dca57fad564ee767c326be
|
7
|
+
data.tar.gz: c1ceddb0cb1c347a7365c07c18f5978035132ac5242de1a063d4e564008b96f666431b5cf499bcb900f788d8c5d2b5c5ca18fd5da89b8fc721ccb8ffcc61035c
|
data/lib/calyx.rb
CHANGED
@@ -22,21 +22,22 @@ module Calyx
|
|
22
22
|
|
23
23
|
def construct_rule(productions)
|
24
24
|
if productions.first.is_a?(Enumerable)
|
25
|
-
Production::WeightedChoices.parse(productions)
|
25
|
+
Production::WeightedChoices.parse(productions, registry)
|
26
26
|
else
|
27
|
-
Production::Choices.parse(productions)
|
27
|
+
Production::Choices.parse(productions, registry)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
module Production
|
33
33
|
class NonTerminal
|
34
|
-
def initialize(expansion)
|
34
|
+
def initialize(expansion, registry)
|
35
35
|
@expansion = expansion.to_sym
|
36
|
+
@registry = registry
|
36
37
|
end
|
37
38
|
|
38
|
-
def evaluate
|
39
|
-
registry[@expansion].evaluate
|
39
|
+
def evaluate
|
40
|
+
@registry[@expansion].evaluate
|
40
41
|
end
|
41
42
|
end
|
42
43
|
|
@@ -45,24 +46,44 @@ module Calyx
|
|
45
46
|
@atom = atom
|
46
47
|
end
|
47
48
|
|
48
|
-
def evaluate
|
49
|
+
def evaluate
|
49
50
|
@atom
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
54
|
+
class Expression
|
55
|
+
def initialize(production, methods)
|
56
|
+
@production = production
|
57
|
+
@methods = methods.map { |m| m.to_sym }
|
58
|
+
end
|
59
|
+
|
60
|
+
def evaluate
|
61
|
+
@methods.reduce(@production.evaluate) do |value,method|
|
62
|
+
value.send(method)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
53
67
|
class Concat
|
54
68
|
DELIMITER = /(\{[A-Za-z0-9_]+\})/.freeze
|
69
|
+
DEREF = '.'.freeze
|
55
70
|
|
56
|
-
def self.parse(production)
|
71
|
+
def self.parse(production, registry)
|
57
72
|
expansion = production.split(DELIMITER).map do |atom|
|
58
73
|
if atom.is_a?(String)
|
59
74
|
if atom.chars.first == '{' && atom.chars.last == '}'
|
60
|
-
|
75
|
+
head, *tail = atom.slice(1, atom.length-2).split(DEREF)
|
76
|
+
rule = NonTerminal.new(head, registry)
|
77
|
+
unless tail.empty?
|
78
|
+
Expression.new(rule, tail)
|
79
|
+
else
|
80
|
+
rule
|
81
|
+
end
|
61
82
|
else
|
62
83
|
Terminal.new(atom)
|
63
84
|
end
|
64
85
|
elsif atom.is_a?(Symbol)
|
65
|
-
NonTerminal.new
|
86
|
+
NonTerminal.new(atom, registry)
|
66
87
|
end
|
67
88
|
end
|
68
89
|
|
@@ -73,15 +94,15 @@ module Calyx
|
|
73
94
|
@expansion = expansion
|
74
95
|
end
|
75
96
|
|
76
|
-
def evaluate
|
97
|
+
def evaluate
|
77
98
|
@expansion.reduce('') do |exp, atom|
|
78
|
-
exp << atom.evaluate
|
99
|
+
exp << atom.evaluate
|
79
100
|
end
|
80
101
|
end
|
81
102
|
end
|
82
103
|
|
83
104
|
class WeightedChoices
|
84
|
-
def self.parse(productions)
|
105
|
+
def self.parse(productions, registry)
|
85
106
|
weights_sum = productions.reduce(0) do |memo, choice|
|
86
107
|
memo += choice.last
|
87
108
|
end
|
@@ -90,9 +111,9 @@ module Calyx
|
|
90
111
|
|
91
112
|
choices = productions.map do |choice, weight|
|
92
113
|
if choice.is_a?(String)
|
93
|
-
[Concat.parse(choice), weight]
|
114
|
+
[Concat.parse(choice, registry), weight]
|
94
115
|
elsif choice.is_a?(Symbol)
|
95
|
-
[NonTerminal.new(choice), weight]
|
116
|
+
[NonTerminal.new(choice, registry), weight]
|
96
117
|
end
|
97
118
|
end
|
98
119
|
|
@@ -103,22 +124,22 @@ module Calyx
|
|
103
124
|
@collection = collection
|
104
125
|
end
|
105
126
|
|
106
|
-
def evaluate
|
127
|
+
def evaluate
|
107
128
|
choice = @collection.max_by do |_, weight|
|
108
129
|
rand ** (1.0 / weight)
|
109
130
|
end.first
|
110
131
|
|
111
|
-
choice.evaluate
|
132
|
+
choice.evaluate
|
112
133
|
end
|
113
134
|
end
|
114
135
|
|
115
136
|
class Choices
|
116
|
-
def self.parse(productions)
|
137
|
+
def self.parse(productions, registry)
|
117
138
|
choices = productions.map do |choice|
|
118
139
|
if choice.is_a?(String)
|
119
|
-
Concat.parse(choice)
|
140
|
+
Concat.parse(choice, registry)
|
120
141
|
elsif choice.is_a?(Symbol)
|
121
|
-
NonTerminal.new(choice)
|
142
|
+
NonTerminal.new(choice, registry)
|
122
143
|
end
|
123
144
|
end
|
124
145
|
self.new(choices)
|
@@ -128,8 +149,8 @@ module Calyx
|
|
128
149
|
@collection = collection
|
129
150
|
end
|
130
151
|
|
131
|
-
def evaluate
|
132
|
-
@collection.sample.evaluate
|
152
|
+
def evaluate
|
153
|
+
@collection.sample.evaluate
|
133
154
|
end
|
134
155
|
end
|
135
156
|
end
|
@@ -145,7 +166,7 @@ module Calyx
|
|
145
166
|
end
|
146
167
|
|
147
168
|
def generate
|
148
|
-
registry[:start].evaluate
|
169
|
+
registry[:start].evaluate
|
149
170
|
end
|
150
171
|
end
|
151
172
|
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.5.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: 2015-11-
|
11
|
+
date: 2015-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|