calyx 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +94 -0
- data/lib/calyx.rb +4 -1
- 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: 833328ded64a82edb7997b924f740a458d179215
|
4
|
+
data.tar.gz: bfca7e43959b79427ecfed6116344bd82aa79e95
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79b97a8dbb5ded857a14f61f687ff2e93ba6c65f75b2f9acbab74c5b34803d2bfd6f5c2b26eeda52640037ba60d56926d849dd0de2d7a1db84ecde9ffbe37c0c
|
7
|
+
data.tar.gz: 7f846571bdefe5c2e922e00218d7f64b79c343344cce9e0395dfe1f7ced7d8cf79ddd71c9be1d061d95d07de09c35ae8ce73954fbcdda000433b358eac6bec15
|
data/README.md
CHANGED
@@ -1,3 +1,97 @@
|
|
1
1
|
# Calyx
|
2
2
|
|
3
3
|
Calyx provides a simple API for generating text with declarative recursive grammars.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
### Command Line
|
8
|
+
|
9
|
+
```
|
10
|
+
gem install calyx
|
11
|
+
```
|
12
|
+
|
13
|
+
## Gemfile
|
14
|
+
|
15
|
+
```
|
16
|
+
gem 'calyx'
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Require the library and inherit from `Calyx::Grammar` to construct a set of rules to generate a text. All grammars require a `start` rule, which specifies the starting point for generating the text structure.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'calyx'
|
25
|
+
|
26
|
+
class HelloWorld < Calyx::Grammar
|
27
|
+
start 'Hello world.'
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
To generate the text itself, initialize the object and call the `generate` method.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
hello = HelloWorld.new
|
35
|
+
hello.generate
|
36
|
+
# > "Hello world."
|
37
|
+
```
|
38
|
+
|
39
|
+
Obviously, this hardcoded sentence isn’t very interesting by itself. Possible variations can be added to the text using the `rule` constructor to provide a named set of text strings and the rule delimiter syntax (`{}`) within the text strings to substitute the generated content of the rule.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class HelloWorld < Calyx::Grammar
|
43
|
+
start '{greeting} world.'
|
44
|
+
rule :greeting, 'Hello', 'Hi', 'Hey', 'Yo'
|
45
|
+
end
|
46
|
+
|
47
|
+
hello = HelloWorld.new
|
48
|
+
|
49
|
+
hello.generate
|
50
|
+
# > "Hi world."
|
51
|
+
|
52
|
+
hello.generate
|
53
|
+
# > "Hello world."
|
54
|
+
|
55
|
+
hello.generate
|
56
|
+
# > "Yo world."
|
57
|
+
```
|
58
|
+
|
59
|
+
Rules are recursive. They can be arbitrarily nested and connected to generate larger and more complex texts.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class HelloWorld < Calyx::Grammar
|
63
|
+
start '{greeting} {world_phrase}.'
|
64
|
+
rule :greeting, 'Hello', 'Hi', 'Hey', 'Yo'
|
65
|
+
rule :world_phrase, '{happy_adj} world', '{sad_adj} world', 'world'
|
66
|
+
rule :happy_adj, 'wonderful', 'amazing', 'bright', 'beautiful'
|
67
|
+
rule :sad_adj, 'cruel', 'miserable'
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
Nesting and hierarchy can be manipulated to balance consistency with variation. The exact same word atoms can be combined in different ways to produce strikingly different resulting texts.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
module HelloWorld
|
75
|
+
Sentiment < Calyx::Grammar
|
76
|
+
start '{happy_phrase}', '{sad_phrase}'
|
77
|
+
rule :happy_phrase, '{happy_greeting} {happy_adj} world.'
|
78
|
+
rule :happy_greeting, 'Hello', 'Hi', 'Hey', 'Yo'
|
79
|
+
rule :happy_adj, 'wonderful', 'amazing', 'bright', 'beautiful'
|
80
|
+
rule :sad_phrase, '{sad_greeting} {sad_adj} world.'
|
81
|
+
rule :sad_greeting, 'Goodbye', 'So long', 'Farewell'
|
82
|
+
rule :sad_adj, 'cruel', 'miserable'
|
83
|
+
end
|
84
|
+
|
85
|
+
Mixed < Calyx::Grammar
|
86
|
+
start '{greeting} {adj} world.'
|
87
|
+
rule :greeting, 'Hello', 'Hi', 'Hey', 'Yo', 'Goodbye', 'So long', 'Farewell'
|
88
|
+
rule :adj, 'wonderful', 'amazing', 'bright', 'beautiful', 'cruel', 'miserable'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
## License
|
94
|
+
|
95
|
+
Calyx is open source and provided under the terms of the MIT license. Copyright (c) 2015 Mark Rickerby
|
96
|
+
|
97
|
+
See the `LICENSE` file [included with the project distribution](https://github.com/maetl/calyx/blob/master/LICENSE) for more information.
|
data/lib/calyx.rb
CHANGED
@@ -104,7 +104,10 @@ module Calyx
|
|
104
104
|
end
|
105
105
|
|
106
106
|
def evaluate(registry)
|
107
|
-
choice = @collection.max_by
|
107
|
+
choice = @collection.max_by do |_, weight|
|
108
|
+
rand ** (1.0 / weight)
|
109
|
+
end.first
|
110
|
+
|
108
111
|
choice.evaluate(registry)
|
109
112
|
end
|
110
113
|
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.
|
4
|
+
version: 0.4.2
|
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-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|