calyx 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +7 -1
- data/lib/calyx.rb +35 -7
- 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: 5ce625a176cd41a95aac8b5c2dcadc0ef95e3bf3
|
4
|
+
data.tar.gz: b0c911bdc364efe49b1c8d882823426eda62c23a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d323c2e78271a53a8de7c8fe1b1925a35e0f0f20b85bc8fca503ac099f9517f3c2279a66e4cae765519acc58a9544d103e4bacfd34030b982eba05f363390e36
|
7
|
+
data.tar.gz: 348d17e64e1783360e947f9cca81d3770a1dc22c7cacc94f7977a6b2dc2fae472ecc677cf7e3c433968863704b6f7e097f50e09330d7f0024ea7783c4386a1d4
|
data/.travis.yml
CHANGED
data/lib/calyx.rb
CHANGED
@@ -21,7 +21,11 @@ module Calyx
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def construct_rule(productions)
|
24
|
-
|
24
|
+
if productions.first.is_a?(Enumerable)
|
25
|
+
Production::WeightedChoices.parse(productions)
|
26
|
+
else
|
27
|
+
Production::Choices.parse(productions)
|
28
|
+
end
|
25
29
|
end
|
26
30
|
end
|
27
31
|
|
@@ -51,10 +55,14 @@ module Calyx
|
|
51
55
|
|
52
56
|
def self.parse(production)
|
53
57
|
expansion = production.split(DELIMITER).map do |atom|
|
54
|
-
if atom.
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
+
if atom.is_a?(String)
|
59
|
+
if atom.chars.first == '{' && atom.chars.last == '}'
|
60
|
+
NonTerminal.new(atom.slice(1, atom.length-2))
|
61
|
+
else
|
62
|
+
Terminal.new(atom)
|
63
|
+
end
|
64
|
+
elsif atom.is_a?(Symbol)
|
65
|
+
NonTerminal.new
|
58
66
|
end
|
59
67
|
end
|
60
68
|
|
@@ -72,9 +80,29 @@ module Calyx
|
|
72
80
|
end
|
73
81
|
end
|
74
82
|
|
83
|
+
class WeightedChoices
|
84
|
+
def self.parse(productions)
|
85
|
+
weights_sum = productions.reduce(0) do |memo, choice|
|
86
|
+
memo += choice.last
|
87
|
+
end
|
88
|
+
|
89
|
+
raise 'Weights must sum to 1' if weights_sum != 1.0
|
90
|
+
|
91
|
+
self.new(productions)
|
92
|
+
end
|
93
|
+
|
94
|
+
def initialize(collection)
|
95
|
+
@collection = collection
|
96
|
+
end
|
97
|
+
|
98
|
+
def evaluate(registry)
|
99
|
+
@collection.max_by { |_, weight| rand ** (1.0 / weight) }.first
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
75
103
|
class Choices
|
76
|
-
def self.parse(
|
77
|
-
choices =
|
104
|
+
def self.parse(productions)
|
105
|
+
choices = productions.map do |choice|
|
78
106
|
if choice.is_a?(String)
|
79
107
|
Concat.parse(choice)
|
80
108
|
elsif choice.is_a?(Symbol)
|
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.4.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-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|