calyx 0.3.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 271f3418b869df3fa19a8d8ec31782a64a798ea9
4
- data.tar.gz: 96b37bda20f6cc56c1ca9fba5899b4dd8beb9122
3
+ metadata.gz: 5ce625a176cd41a95aac8b5c2dcadc0ef95e3bf3
4
+ data.tar.gz: b0c911bdc364efe49b1c8d882823426eda62c23a
5
5
  SHA512:
6
- metadata.gz: f5083d4be511ecef3e04fff2527619e52f23377ac91e84acda2b0e9e8d945936b292f90da2ac92fbd133dadc6eef592bee1be9db74dd48dbb64e09e6ebbacb17
7
- data.tar.gz: 296b3f42152e2743f471b7c24752e7e6a0494dc6c12b70f9dfe4be73342848f9dd19fe771aa1aa5d2a7a41ddbd94a507726a3ed69221eca0b589d7491962e5d5
6
+ metadata.gz: d323c2e78271a53a8de7c8fe1b1925a35e0f0f20b85bc8fca503ac099f9517f3c2279a66e4cae765519acc58a9544d103e4bacfd34030b982eba05f363390e36
7
+ data.tar.gz: 348d17e64e1783360e947f9cca81d3770a1dc22c7cacc94f7977a6b2dc2fae472ecc677cf7e3c433968863704b6f7e097f50e09330d7f0024ea7783c4386a1d4
@@ -1,4 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.3
3
+ - 2.1
4
+ - 2.2
5
+ - rbx-2
6
+ - jruby-9
7
+ - ruby-head
8
+ - jruby-head
4
9
  before_install: gem install bundler -v 1.10.6
10
+ script: bundle exec rspec
@@ -21,7 +21,11 @@ module Calyx
21
21
  end
22
22
 
23
23
  def construct_rule(productions)
24
- Production::Choices.parse(productions)
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.chars.first == '{' && atom.chars.last == '}'
55
- NonTerminal.new(atom.slice(1, atom.length-2))
56
- else
57
- Terminal.new(atom)
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(production)
77
- choices = production.map do |choice|
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)
@@ -1,3 +1,3 @@
1
1
  module Calyx
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.4.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.3.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-13 00:00:00.000000000 Z
11
+ date: 2015-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler