panini 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +3 -1
- data/VERSION +1 -1
- data/lib/derivation_strategy/exhaustive.rb +22 -21
- data/panini.gemspec +2 -2
- data/spec/derivation_strategy/exhaustive_spec.rb +37 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -82,10 +82,12 @@ This will return a different sentence each time it is called. It may (and proba
|
|
82
82
|
|
83
83
|
This strategy is used to exhaustively create all of the sentences that may be created by a grammar.
|
84
84
|
|
85
|
-
derivator = Panini::DerivationStrategy::RandomDampened.new(grammar)
|
85
|
+
derivator = Panini::DerivationStrategy::RandomDampened.new(grammar, length_limit)
|
86
86
|
|
87
87
|
This will return a new sentence with each call. If there are no additional sentences to be created it will return nil. The same sentence may be returned multiple times if the grammar can derive the sentence in multiple ways.
|
88
88
|
|
89
|
+
You can optionally pass a limit for the size of sentences to be generated.
|
90
|
+
|
89
91
|
=== Generating a Sentence
|
90
92
|
|
91
93
|
To generate a sentence, call the derivator's sentence method like thus:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
@@ -10,6 +10,10 @@ class Array
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
def fully_derived?
|
14
|
+
not has_nonterminals?
|
15
|
+
end
|
16
|
+
|
13
17
|
end
|
14
18
|
|
15
19
|
|
@@ -18,19 +22,20 @@ module Panini
|
|
18
22
|
|
19
23
|
class Exhaustive < Base
|
20
24
|
|
21
|
-
def initialize(grammar)
|
25
|
+
def initialize(grammar, length_limit = nil)
|
22
26
|
super(grammar)
|
23
27
|
@in_progress_work = [].push([@grammar.start])
|
28
|
+
@length_limit = length_limit
|
24
29
|
end
|
25
30
|
|
26
31
|
# Generates a sentence.
|
27
32
|
def sentence
|
28
|
-
unless @in_progress_work.empty?
|
29
|
-
|
30
|
-
|
31
|
-
end
|
32
|
-
@in_progress_work.pop
|
33
|
+
# unless @in_progress_work.empty?
|
34
|
+
while !@in_progress_work.empty? && @in_progress_work.top.has_nonterminals?
|
35
|
+
generate_work(@in_progress_work.pop)
|
33
36
|
end
|
37
|
+
@in_progress_work.pop
|
38
|
+
# end
|
34
39
|
end
|
35
40
|
|
36
41
|
def generate_work(derived_sentence)
|
@@ -55,26 +60,22 @@ module Panini
|
|
55
60
|
term
|
56
61
|
end
|
57
62
|
end
|
58
|
-
|
63
|
+
|
64
|
+
# If we're limiting lengths see if completed derivations should
|
65
|
+
# be discarded.
|
66
|
+
unless (limiting_lengths? && newly_derived_sentence.fully_derived? && newly_derived_sentence.size > @length_limit)
|
67
|
+
@in_progress_work.push(newly_derived_sentence)
|
68
|
+
end
|
69
|
+
|
59
70
|
end
|
60
71
|
|
61
72
|
end
|
62
73
|
private :generate_work
|
63
74
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
# if !substituted && (term.class == Nonterminal)
|
69
|
-
# substituted = true
|
70
|
-
# @production_proxies[term].production
|
71
|
-
# else
|
72
|
-
# term
|
73
|
-
# end
|
74
|
-
# end
|
75
|
-
# return derived_sentence, substituted
|
76
|
-
# end
|
77
|
-
# private :substitution_pass
|
75
|
+
def limiting_lengths?
|
76
|
+
not @length_limit.nil?
|
77
|
+
end
|
78
|
+
private :limiting_lengths?
|
78
79
|
|
79
80
|
end
|
80
81
|
|
data/panini.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "panini"
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["mjbellantoni"]
|
12
|
-
s.date = "2011-09-
|
12
|
+
s.date = "2011-09-16"
|
13
13
|
s.description = "Panini allows you to generate sentences from a context-free grammar, also known as a CFG."
|
14
14
|
s.email = "mjbellantoni@yahoo.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -116,4 +116,41 @@ describe "Grammar with the production S -> xAyBz, A -> 'a' | 'aa', B -> 'b' | 'b
|
|
116
116
|
d.sentence.should == ['x', 'aa', 'y', 'b', 'z']
|
117
117
|
end
|
118
118
|
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
describe "Grammar with the production S -> xAyBz, A -> eps | 'a', B -> eps | 'b', and length limit of 4 " do
|
123
|
+
|
124
|
+
before (:each) do
|
125
|
+
@g = Panini::Grammar.new
|
126
|
+
|
127
|
+
@n_s = @g.add_nonterminal
|
128
|
+
@n_a = @g.add_nonterminal
|
129
|
+
@n_b = @g.add_nonterminal
|
130
|
+
|
131
|
+
@n_s.add_production(['x', @n_a, 'y', @n_b, 'z'])
|
132
|
+
@n_a.add_production([])
|
133
|
+
@n_a.add_production(['a'])
|
134
|
+
@n_b.add_production([])
|
135
|
+
@n_b.add_production(['b'])
|
136
|
+
end
|
137
|
+
|
138
|
+
it "generates the sentence ['x', 'y', 'z'] first" do
|
139
|
+
d = Panini::DerivationStrategy::Exhaustive.new(@g, 4)
|
140
|
+
d.sentence.should == ['x', 'y', 'z']
|
141
|
+
end
|
142
|
+
|
143
|
+
it "generates the sentence ['x', 'y', 'b', 'z'] next" do
|
144
|
+
d = Panini::DerivationStrategy::Exhaustive.new(@g, 4)
|
145
|
+
d.sentence.should
|
146
|
+
d.sentence.should == ['x', 'y', 'b', 'z']
|
147
|
+
end
|
148
|
+
|
149
|
+
it "generates the sentence ['x', 'a', 'y', 'z'] next" do
|
150
|
+
d = Panini::DerivationStrategy::Exhaustive.new(@g, 4)
|
151
|
+
d.sentence.should
|
152
|
+
d.sentence.should
|
153
|
+
d.sentence.should == ['x', 'a', 'y', 'z']
|
154
|
+
end
|
155
|
+
|
119
156
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: panini
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- mjbellantoni
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-09-
|
13
|
+
date: 2011-09-16 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rspec
|
@@ -105,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
105
105
|
requirements:
|
106
106
|
- - ">="
|
107
107
|
- !ruby/object:Gem::Version
|
108
|
-
hash:
|
108
|
+
hash: -174120655585110477
|
109
109
|
segments:
|
110
110
|
- 0
|
111
111
|
version: "0"
|