tracery 0.7.4 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +23 -0
- data/LICENSE.txt +202 -0
- data/Rakefile +12 -0
- data/lib/tracery.rb +17 -2
- data/lib/version.rb +1 -1
- data/readme.md +144 -0
- data/test/tracery_test.rb +58 -16
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fc6be58e9cfc5484a72b215bb1b503c1a61f77fa
|
4
|
+
data.tar.gz: 0e615f4130ccd03df0b33f87abeed9fb294feb2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ac5e3d659c35f1e313e305a86b87c9f2a403040e92656249209a1564700bf17cbf645b7d2ee374d5349617638e266740e7182fe94e0b225b8d5272b5e82a4ee4
|
7
|
+
data.tar.gz: 43fafcfc0ba52adc1f69f62b03eeccaeed35d2ed9b2537bbba5f3522e10d8020e8e20e04ab58f3f52358b96fd029d1c09277f94f0245b82d735e2b7cf632089e
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
|
+
|
5
|
+
## 0.7.6 - 2016-04-29
|
6
|
+
### Added
|
7
|
+
- This CHANGELOG.md file
|
8
|
+
- [Ability to use `:symbolic` hashes](https://github.com/elib/tracery/pull/13)
|
9
|
+
- Using Travis CI to do basic testing
|
10
|
+
- [Ability to override random function](https://github.com/elib/tracery/issues/3)
|
11
|
+
|
12
|
+
### Changed
|
13
|
+
- Rearranged repo (removed all javascript sources, add version file, etc.)
|
14
|
+
- Improve tests
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
- [Escaping brackets break rule replacements](https://github.com/elib/tracery/issues/1)
|
18
|
+
|
19
|
+
## 0.7.0 - 2016-02-27
|
20
|
+
### Added
|
21
|
+
- Initial version
|
22
|
+
- Handles most basic grammars
|
23
|
+
- Published gem
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright 2016 Kate Compton
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/Rakefile
ADDED
data/lib/tracery.rb
CHANGED
@@ -6,6 +6,21 @@ module Tracery
|
|
6
6
|
def createGrammar(raw)
|
7
7
|
return Grammar.new(raw)
|
8
8
|
end
|
9
|
+
|
10
|
+
#handle random and replacement random
|
11
|
+
@@internal_rnd = Random.new
|
12
|
+
@@rnd_proc = lambda { @@internal_rnd.rand }
|
13
|
+
def self.setRnd(lambdaproc)
|
14
|
+
@@rnd_proc = lambdaproc
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.resetRnd
|
18
|
+
setRnd(lambda { @@internal_rnd.rand })
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.random
|
22
|
+
return @@rnd_proc.call
|
23
|
+
end
|
9
24
|
|
10
25
|
def parseTag(tagContents)
|
11
26
|
parsed = {
|
@@ -405,7 +420,6 @@ class RuleSet
|
|
405
420
|
@raw = raw
|
406
421
|
@grammar = grammar
|
407
422
|
@falloff = 1
|
408
|
-
@random = Random.new
|
409
423
|
|
410
424
|
@defaultUses = {}
|
411
425
|
|
@@ -444,7 +458,7 @@ class RuleSet
|
|
444
458
|
when "falloff" then
|
445
459
|
@errors << "Falloff distribution not yet implemented"
|
446
460
|
else
|
447
|
-
index = ((
|
461
|
+
index = ((Tracery.random ** @falloff) * @defaultRules.size).floor
|
448
462
|
end
|
449
463
|
|
450
464
|
@defaultUses[index] = (@defaultUses[index] || 0) + 1
|
@@ -527,6 +541,7 @@ class Grammar
|
|
527
541
|
end
|
528
542
|
|
529
543
|
def loadFromRawObj(raw)
|
544
|
+
raw = Hash[raw.collect{|k,v| [k.to_s, v]}]
|
530
545
|
@raw = raw
|
531
546
|
@symbols = {}
|
532
547
|
@subgrammars = []
|
data/lib/version.rb
CHANGED
data/readme.md
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
# Tracery
|
2
|
+
|
3
|
+
## About
|
4
|
+
Tracery was developed by Kate Compton, beginning March 2013 as a class assignment.
|
5
|
+
This is version 0.7
|
6
|
+
(This is the first numbered version of Tracery, chosen arbitrarily. All basic syntax is stable, but some advanced features like nested rules, if-statements, and modifiers with parameters are still in flux)
|
7
|
+
|
8
|
+
## Tracery for Ruby
|
9
|
+
|
10
|
+
Tracery for Ruby is ported and maintained by Eli Brody. Javascript tracery can be found [here](https://github.com/galaxykate/tracery). (This text has been adapted to the Ruby syntax, but it is based on the original readme file.)
|
11
|
+
|
12
|
+
[![Gem Version](https://badge.fury.io/rb/tracery.svg)](https://badge.fury.io/rb/tracery)
|
13
|
+
|
14
|
+
[![Build Status](https://travis-ci.org/elib/tracery.svg)](https://travis-ci.org/elib/tracery)
|
15
|
+
|
16
|
+
## Basic usage
|
17
|
+
|
18
|
+
### Create a grammar
|
19
|
+
|
20
|
+
Using the ruby port is very similar to the Javascript version. First, install the tracery gem: `gem install tracery`.
|
21
|
+
|
22
|
+
Create an empty grammar:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'tracery'
|
26
|
+
include Tracery
|
27
|
+
grammar = createGrammar();
|
28
|
+
```
|
29
|
+
|
30
|
+
Create a grammar from a Tracery-formatted object:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
grammar = createGrammar({origin: "foo"});
|
34
|
+
```
|
35
|
+
|
36
|
+
Add modifiers to the grammar (`mods-eng-basic.rb` for basic English modifiers, or write your own)
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require 'mods-eng-basic'
|
40
|
+
grammar.addModifiers(Modifiers.baseEngModifiers);
|
41
|
+
```
|
42
|
+
|
43
|
+
### Expand rules
|
44
|
+
Get the fully-expanded string from a rule
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
expanded_text = grammar.flatten("#origin#")
|
48
|
+
```
|
49
|
+
|
50
|
+
Get the fully-expanded node from a rule, this will return a root node containing a full expanded tree with many potentially interesting properties, including "finishedText" for each node.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
expanded_node = grammar.expand("#origin#")
|
54
|
+
```
|
55
|
+
|
56
|
+
### Making Tracery deterministic
|
57
|
+
|
58
|
+
By default, Tracery uses `Random#rand` to generate random numbers. If you need Tracery to be deterministic, you can make it use your own random number generator using:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
Tracery.setRng(rng_lambda)
|
62
|
+
```
|
63
|
+
|
64
|
+
where `rng_lambda` is a lambda that, [like Random#rand](http://ruby-doc.org/core-2.0.0/Random.html#method-i-rand), returns a floating-point, pseudo-random number in the range `[0, 1)`. By using a local random number generator that takes a seed and controlling this seed, you can make Tracery's behavior completely deterministic.
|
65
|
+
|
66
|
+
Usage example:
|
67
|
+
```ruby
|
68
|
+
Tracery.setRng(lambda { return 0.5 })
|
69
|
+
```
|
70
|
+
|
71
|
+
Note: Beware, this lambda is set *globally*, for all Tracery expansions.
|
72
|
+
|
73
|
+
## Library Concepts
|
74
|
+
### Grammar
|
75
|
+
|
76
|
+
A Grammar is
|
77
|
+
|
78
|
+
* *a dictionary of symbols*: a key-value object matching keys (the names of symbols) to expansion rules
|
79
|
+
* optional metadata such as a title, edit data, and author
|
80
|
+
* optional connectivity graphs describing how symbols call each other
|
81
|
+
|
82
|
+
*clearState*: symbols and rulesets have state (the stack, and possible ruleset state recording recently called rules). This function clears any state, returning the dictionary to its original state;
|
83
|
+
|
84
|
+
Grammars are usually created by feeding in a raw JSON grammar, which is then parsed into symbols and rules. You can also build your own Grammar objects from scratch, without using this utility function, and can always edit the grammar after creating it.
|
85
|
+
|
86
|
+
### Symbol
|
87
|
+
A symbol is a **key** (usually a short human-readable string) and a set of expansion rules
|
88
|
+
* the key
|
89
|
+
* rulesetStack: the stack of expansion **rulesets** for this symbol. This stack records the previous, inactive rulesets, and the current one.
|
90
|
+
* optional connectivity data, such as average depth and average expansion length
|
91
|
+
|
92
|
+
Putting a **key** in hashtags, in a Tracery syntax object, will create a expansion node for that symbol within the text.
|
93
|
+
|
94
|
+
Each top-level key-value pair in the raw JSON object creates a **symbol**. The symbol's *key* is set from the key, and the *value* determines the **ruleset**.
|
95
|
+
|
96
|
+
### Modifier
|
97
|
+
A function that takes a string (and optionally parameters) and returns a string. A set of these is included in `mods-eng-basic.rb`. Modifiers are applied, in order, after a tag is fully expanded.
|
98
|
+
|
99
|
+
To apply a modifier, add its name after a period, after the tag's main symbol:
|
100
|
+
#animal.capitalize#
|
101
|
+
#booktitle.capitalizeAll#
|
102
|
+
Hundreds of #animal.s#
|
103
|
+
|
104
|
+
Modifiers can have parameters, too! (soon they will can have parameter that contain tags, which will be expanded when applying the modifier, but not yet)
|
105
|
+
#story.replace(he,she).replace(him,her).replace(his,hers)#
|
106
|
+
|
107
|
+
### Action
|
108
|
+
An action that occurs when its node is expanded. Built-in actions are
|
109
|
+
* Generating some rules "[key:#rule#]" and pushing them to the "key" symbol's rule stack. If that symbol does not exist, it creates it.
|
110
|
+
* Popping rules off of a rule stack, "[key:POP]"
|
111
|
+
* Other functions
|
112
|
+
|
113
|
+
TODO: figure out syntax and implementation for generating *arrays* of rules, or other complex rulesets to push onto symbols' rulestacks
|
114
|
+
|
115
|
+
TODO: figure out syntax and storage for calling other functions, especially for async APIs.
|
116
|
+
|
117
|
+
### Ruleset
|
118
|
+
A ruleset is an object that defines a *getRule* function. Calling this function may change the internal state of the ruleset, such as annotating which rules were most recently returned, or drawing and removing a rule from a shuffled list of available rules.
|
119
|
+
|
120
|
+
#### Basic ruleset
|
121
|
+
A basic ruleset is just an array of options.
|
122
|
+
|
123
|
+
They can be created by raw JSON by having an *array* or a *string* as the value, like this:
|
124
|
+
"someKey":["rule0", "rule1", "some#complicated#rule"]
|
125
|
+
If there is only one rule, it is acceptable short hand to leave off the array, but this only works with Strings.
|
126
|
+
"someKey":"just one rule"
|
127
|
+
|
128
|
+
These use the default distribution of the Grammar that owns them, which itself defaults to regular stateless pseudo-randomness.
|
129
|
+
|
130
|
+
#### Rulesets with conditions, distributions, or ranked fallbacks
|
131
|
+
### **this feature is under development, coming soon
|
132
|
+
These rulesets are created when the raw JSON has an *object* rather than an *array* as the value.
|
133
|
+
|
134
|
+
Some attributes of this object can be:
|
135
|
+
|
136
|
+
* baseRules: a single ruleset,
|
137
|
+
* ruleRanking: an array of rulesets, call *getRule* on each in order until one returns a value, if none do, return *baseRules*.*getRule*,
|
138
|
+
* distribution: a new distribution to override the default)
|
139
|
+
* conditionRule: a rule to expand
|
140
|
+
* conditionValue: a value to match the expansion against
|
141
|
+
* conditionSuccess: a ruleset to use if expanding *conditionRule* returns *conditionValue*, otherwise use *baseRules*
|
142
|
+
|
143
|
+
|
144
|
+
These can be nested, so it is possible to make a ruleset
|
data/test/tracery_test.rb
CHANGED
@@ -7,6 +7,7 @@ require "mods-eng-basic"
|
|
7
7
|
class TraceryTest < Test::Unit::TestCase
|
8
8
|
include Tracery
|
9
9
|
def setup
|
10
|
+
Tracery.resetRnd
|
10
11
|
@grammar = createGrammar({
|
11
12
|
"deepHash" => ["\\#00FF00", "\\#FF00FF"],
|
12
13
|
"deeperHash" => ["#deepHash#"],
|
@@ -32,16 +33,19 @@ class TraceryTest < Test::Unit::TestCase
|
|
32
33
|
|
33
34
|
def test_main_expansion_tests
|
34
35
|
tests = {
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
plaintextShort: {
|
37
|
+
src: "a",
|
38
|
+
expected: "a"
|
39
|
+
},
|
40
|
+
plaintextLong: {
|
41
|
+
src: "Emma Woodhouse, handsome, clever, and rich, with a comfortable home and happy disposition, seemed to unite some of the best blessings of existence; and had lived nearly twenty-one years in the world with very little to distress or vex her.",
|
42
|
+
expected: "Emma Woodhouse, handsome, clever, and rich, with a comfortable home and happy disposition, seemed to unite some of the best blessings of existence; and had lived nearly twenty-one years in the world with very little to distress or vex her."
|
43
|
+
},
|
41
44
|
|
42
45
|
# Escape chars
|
43
46
|
escapeCharacter: {
|
44
|
-
src: "\\#escape hash\\# and escape slash\\\\"
|
47
|
+
src: "\\#escape hash\\# and escape slash\\\\",
|
48
|
+
expected: "#escape hash# and escape slash\\"
|
45
49
|
},
|
46
50
|
|
47
51
|
escapeDeep: {
|
@@ -49,32 +53,40 @@ class TraceryTest < Test::Unit::TestCase
|
|
49
53
|
},
|
50
54
|
|
51
55
|
escapeQuotes: {
|
52
|
-
src: "\"test\" and \'test\'"
|
56
|
+
src: "\"test\" and \'test\'",
|
57
|
+
expected: "\"test\" and \'test\'"
|
53
58
|
},
|
54
59
|
escapeBrackets: {
|
55
|
-
src: "\\[\\]"
|
60
|
+
src: "\\[\\]",
|
61
|
+
expected: "[]"
|
56
62
|
},
|
57
63
|
escapeHash: {
|
58
|
-
src: "\\#"
|
64
|
+
src: "\\#",
|
65
|
+
expected: "#"
|
59
66
|
},
|
60
67
|
unescapeCharSlash: {
|
61
|
-
src: "\\\\"
|
68
|
+
src: "\\\\",
|
69
|
+
expected: "\\"
|
62
70
|
},
|
63
71
|
escapeMelange1: {
|
64
|
-
src: "An action can have inner tags:
|
72
|
+
src: "An action can have inner tags: \\[key:\\#rule\\#\\]",
|
73
|
+
expected: "An action can have inner tags: [key:#rule#]"
|
65
74
|
},
|
66
75
|
escapeMelange2: {
|
67
|
-
src: "A tag can have inner actions: \"\\#\\[myName:\\#name\\#\\]story\\[myName:POP\\]\\#\""
|
76
|
+
src: "A tag can have inner actions: \"\\#\\[myName:\\#name\\#\\]story\\[myName:POP\\]\\#\"",
|
77
|
+
expected: "A tag can have inner actions: \"#[myName:#name#]story[myName:POP]#\""
|
68
78
|
},
|
69
79
|
escapeBroken: {
|
70
80
|
src: "\"#name#\" should expand to a name. But \\[ this and \\] should be literal square brackets. And again, just another name: \"#name#\". But for some reason it gets put here at the end! Fixed now....."
|
71
81
|
},
|
72
82
|
emoji: {
|
73
|
-
src: "💻🐋🌙🏄🍻"
|
83
|
+
src: "💻🐋🌙🏄🍻",
|
84
|
+
expected: "💻🐋🌙🏄🍻"
|
74
85
|
},
|
75
86
|
|
76
87
|
unicode: {
|
77
|
-
src: "&\\#x2665; &\\#x2614; &\\#9749; &\\#x2665;"
|
88
|
+
src: "&\\#x2665; &\\#x2614; &\\#9749; &\\#x2665;",
|
89
|
+
expected: "♥ ☔ ☕ ♥"
|
78
90
|
},
|
79
91
|
|
80
92
|
svg: {
|
@@ -144,7 +156,13 @@ class TraceryTest < Test::Unit::TestCase
|
|
144
156
|
is_error = v[:error].nil? ? false : v[:error]
|
145
157
|
root = @grammar.expand(source)
|
146
158
|
all_errors = root.allErrors
|
147
|
-
|
159
|
+
|
160
|
+
if(!v[:expected].nil?) then
|
161
|
+
assert(root.finishedText == v[:expected], "\"#{root.finishedText}\" did not match \"#{v[:expected]}\"")
|
162
|
+
else
|
163
|
+
puts "\t#{root.finishedText}"
|
164
|
+
end
|
165
|
+
|
148
166
|
if(is_error) then
|
149
167
|
puts "\tErrors: #{all_errors}"
|
150
168
|
refute(all_errors.empty?, "Expected errors.")
|
@@ -155,4 +173,28 @@ class TraceryTest < Test::Unit::TestCase
|
|
155
173
|
}
|
156
174
|
end
|
157
175
|
|
176
|
+
def test_symbolic_hash_grammars
|
177
|
+
grammar = createGrammar({origin: "here's the origin, with an #extra#.", extra: "extra piece of text"})
|
178
|
+
root = grammar.expand("#origin#")
|
179
|
+
all_errors = root.allErrors
|
180
|
+
assert(all_errors.empty?, "No errors should be generated")
|
181
|
+
assert(root.finishedText == "here's the origin, with an extra piece of text.")
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_set_rnd
|
185
|
+
Tracery.setRnd(lambda { return 0 })
|
186
|
+
assert(Tracery.random == 0)
|
187
|
+
|
188
|
+
Tracery.setRnd(lambda { return 1 })
|
189
|
+
assert(Tracery.random == 1)
|
190
|
+
|
191
|
+
Tracery.resetRnd
|
192
|
+
refute(Tracery.random == 1)
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_deterministic_rules
|
196
|
+
Tracery.setRnd(lambda { return 0 })
|
197
|
+
text = @grammar.flatten("#origin#")
|
198
|
+
assert(text == "Cheri was a great baker, and this song tells of their adventure. Cheri baked bread, then they baked bread, then they went home to read a book.")
|
199
|
+
end
|
158
200
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tracery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kate Compton
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-04-
|
12
|
+
date: 2016-04-29 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: |
|
15
15
|
Tracery is a library for text generation.
|
@@ -23,6 +23,10 @@ files:
|
|
23
23
|
- lib/mods-eng-basic.rb
|
24
24
|
- lib/tracery.rb
|
25
25
|
- lib/version.rb
|
26
|
+
- Rakefile
|
27
|
+
- CHANGELOG.md
|
28
|
+
- LICENSE.txt
|
29
|
+
- readme.md
|
26
30
|
- test/tracery_test.rb
|
27
31
|
homepage: https://github.com/elib/tracery
|
28
32
|
licenses:
|