genex 0.6.4

Text-expansion library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Rust library implementing a custom text generation/templating system. Genex
//! is similar to [Tracery](https://tracery.io), but with some extra
//! functionality around using external data.
//!
//! # Usage
//!
//! First create a grammar, then generate an expansion or multiple expansions
//! from it.
//!
//! ```rust
//! use std::collections::HashSet;
//! use std::str::FromStr;
//! use maplit::hashmap;
//! use genex::Grammar;
//!
//! let grammar = Grammar::from_str(
//!     r#"
//!       RULES:
//!       top = The <adj> <noun> #action|ed# #object|a#?:[ with gusto] in <place>.
//!       adj = [glistening|#adj#]
//!       noun = key
//!       place = [the #room#|#city#]
//!
//!       WEIGHTS:
//!       room = 2
//!       city = 1
//!     "#,
//! )
//! .unwrap();
//!
//! let data = hashmap! {
//!     "action".to_string() => "pick".to_string(),
//!     "object".to_string() => "lizard".to_string(),
//!     "room".to_string() => "kitchen".to_string(),
//!     "city".to_string() => "New York".to_string(),
//! };
//!
//! // Now we find the top-scoring expansion. The score is the sum of the
//! // weights of all variables used in an expansion. We know that the top
//! // scoring expansion is going to end with "the kitchen" because we gave
//! // `room` a higher weight than `city`.
//!
//! let best_expansion = grammar.generate("top", &data).unwrap().unwrap();
//!
//! assert_eq!(
//!     best_expansion,
//!     "The glistening key picked a lizard in the kitchen.".to_string()
//! );
//!
//! // Now get all possible expansions:
//!
//! let all_expansions = grammar.generate_all("top", &data).unwrap();
//!
//! assert_eq!(
//!     HashSet::<_>::from_iter(all_expansions),
//!     HashSet::<_>::from_iter(vec![
//!         "The glistening key picked a lizard in New York.".to_string(),
//!         "The glistening key picked a lizard with gusto in New York.".to_string(),
//!         "The glistening key picked a lizard with gusto in the kitchen.".to_string(),
//!         "The glistening key picked a lizard in the kitchen.".to_string(),
//!     ])
//! );
//! ```
//!
//! # Features
//!
//! Genex tries to make it easy to generate text based on varying amounts of
//! external data. For example you can write a single expansion grammar that
//! works when all you know is the  name of an object, but uses the additional
//! information if you know the object's size, location, color, or other
//! qualities.
//!
//! The default behavior is for genex to try to find an expansion that uses the
//! most external data possible, but by changing the weights assigned to
//! variables you can prioritize which variables are used, even prioritizing the
//! use of a single important variable over the use of multiple, less important
//! variables.
//!
//! # Grammar syntax
//!
//! ## Rules
//!
//! "`RULES:`" indicates the rules section of the grammar. Rules are defined by
//! a left-hand side (LHS) and a right-hand side (RHS). The LHS is the name of
//! the rule. The RHS is a sequence of terms.
//!
//! Terms: 
//! * Sequence: `[term1 term2 ...]`
//! * Choice: `[term1|term2|...]` (You can put a newline after a `|` character.)
//! * Optional: `?:[term1 term2 ...]`
//! * Variable: `#variable#` or `#variable|modifier#`
//! * Non-terminal: `<rule-name>`
//! * Plain text: `I am some plain text. I hope I get expanded.`
//!
//! ## Weights
//! 
//! "`WEIGHTS:`" indicates the weights section of the grammar. Weights are of
//! the form &lt;_rule-name_&gt; = &lt;_number_&gt;.
//! 
//! ## Modifiers
//! 
//! Modifiers are used to transform variable values during expansion.
//! 
//! Modifiers:
//! * `capitalize`: Capitalizes the first letter of the value.
//! * `capitalizeAll`: Capitalizes the first letter of each word in the value.
//! * `inQuotes`: Surrounds the value with double quotes.
//! * `comma`: Adds a comma after the value, if it doesn't already end with punctuation.
//! * `s`: Pluralizes the value.
//! * `a`: Prefixes the value with an "a"/"an" article as appropriate.
//! * `ed`: Changes the first word of the value to be past tense.
//! 
pub mod error;
mod modifiers;
mod parser;
use std::{collections::HashMap, rc::Rc, str::FromStr};

pub use crate::error::Error;
use itertools::Itertools;
use ordered_float::OrderedFloat;
#[macro_use]
extern crate lazy_static;

/// A convenience type for a `Result` of `T` or [`Error`]
///
/// [`Error`]: enum.Error.html
pub type Result<T> = ::std::result::Result<T, Error>;

#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
struct Expansion {
    varrefs: Vec<String>,
    text: String,
}

impl Expansion {
    fn concat(self, expansion: Expansion) -> Self {
        let mut varrefs = self.varrefs.clone();
        varrefs.extend(expansion.varrefs);
        let mut text = self.text;
        text.push_str(&expansion.text);
        Expansion { varrefs, text }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct VarRef {
    var: String,
    modifier: Option<String>,
}

impl VarRef {
    #[allow(dead_code)]
    fn with_variable(var: &str) -> Self {
        VarRef {
            var: var.to_string(),
            modifier: None,
        }
    }

    #[allow(dead_code)]
    fn with_variable_and_modifier(var: &str, modifier: &str) -> Self {
        VarRef {
            var: var.to_string(),
            modifier: Some(modifier.to_string()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Node {
    Sequence(Vec<Node>),
    Optional(Box<Node>),
    Choice(Vec<Node>),
    Text(String),
    VarRef(VarRef),
    NonTerminal(String),
}

impl Node {
    fn expand(&self, grammar: &Grammar, data: &HashMap<String, String>) -> Result<Vec<Expansion>> {
        match self {
            Node::Text(text) => Ok(vec![Expansion {
                varrefs: vec![],
                text: text.clone(),
            }]),
            Node::VarRef(var) => match data.get(&var.var) {
                Some(value) => {
                    let text = match &var.modifier {
                        Some(modifier) => match grammar.get_modifier(modifier) {
                            Some(modifier) => Ok(modifier(value)),
                            None => Err(Error::UnknownModifierError(modifier.to_string())),
                        },
                        None => Ok(value.clone()),
                    }?;
                    Ok(vec![Expansion {
                        varrefs: vec![var.var.clone()],
                        text,
                    }])
                }
                None => Ok(vec![]),
            },
            Node::NonTerminal(lhs) => match grammar.rules.get(lhs) {
                Some(rhs) => rhs.expand(grammar, data),
                None => Err(Error::UnknownNonTerminalError(lhs.clone())),
            },
            Node::Sequence(nodes) => {
                let x: Vec<Vec<Expansion>> = nodes
                    .iter()
                    .map(|n| n.expand(grammar, data))
                    .collect::<Result<Vec<_>>>()?;
                let y: Vec<Expansion> = x
                    .iter()
                    .multi_cartesian_product()
                    .map(|c| {
                        c.into_iter()
                            .fold(Expansion::default(), |a, b| a.concat(b.clone()))
                    })
                    .collect();
                Ok(y)
            }
            Node::Optional(node) => {
                let mut expansions = node.expand(grammar, data)?;
                expansions.push(Expansion::default());
                Ok(expansions)
            }
            Node::Choice(nodes) => {
                let expansions: Vec<Expansion> = nodes
                    .iter()
                    // See https://stackoverflow.com/a/59852696/122762, "How to
                    // handle Result in flat_map"
                    .map(|n| n.expand(grammar, data))
                    .flat_map(|result| match result {
                        Ok(vec) => vec.into_iter().map(Ok).collect(),
                        Err(e) => vec![Err(e)],
                    })
                    .collect::<Result<Vec<_>>>()?;
                Ok(expansions)
            }
        }
    }
}

impl ToString for Node {
    fn to_string(&self) -> String {
        match self {
            Node::Text(text) => text.to_string(),
            Node::Sequence(children) => {
                format!("[{}]", children.iter().map(|n| n.to_string()).join(""))
            }
            Node::VarRef(var) => match &var.modifier {
                Some(modifier) => format!("#{}|{}#", var.var, modifier),
                None => format!("#{}#", var.var),
            },
            Node::NonTerminal(id) => format!("<{}>", id),
            Node::Optional(ref node) => format!("?:[{}]", node.to_string()),
            Node::Choice(nodes) => {
                format!("[{}]", nodes.iter().map(|n| n.to_string()).join("|"))
            }
        }
    }
}

/// A grammar is a set of expansion rules.
#[derive(Clone)]
pub struct Grammar {
    rules: HashMap<String, Node>,
    modifiers: HashMap<String, Rc<dyn Fn(&str) -> String>>,
    default_weights: HashMap<String, f64>,
}

impl Grammar {
    fn new() -> Grammar {
        Grammar {
            rules: HashMap::new(),
            modifiers: HashMap::new(),
            default_weights: HashMap::new(),
        }
    }

    fn add_rule(&mut self, name: &str, node: Node) {
        self.rules.insert(name.to_string(), node);
    }

    fn get_rule(&self, name: &str) -> Option<&Node> {
        self.rules.get(name)
    }

    fn get_modifier(&self, modifier: &str) -> Option<&dyn Fn(&str) -> String> {
        self.modifiers.get(modifier).map(|x| x.as_ref())
    }

    /// Returns the top-scoring expansion of the given rule, using the supplied
    /// data.
    pub fn generate(&self, name: &str, data: &HashMap<String, String>) -> Result<Option<String>> {
        self.generate_with_weights(name, data, &self.default_weights)
    }

    /// Generates all possible expansions of the given rule, using the supplied
    /// data.
    ///
    /// Returns expansions in descending order by score.
    pub fn generate_all(&self, name: &str, data: &HashMap<String, String>) -> Result<Vec<String>> {
        self.generate_all_with_weights(name, data, &self.default_weights)
    }

    /// Generates the top-scoring expansion of the given rule, using the
    /// supplied data and weights.
    pub fn generate_with_weights(
        &self,
        name: &str,
        data: &HashMap<String, String>,
        weights: &HashMap<String, f64>,
    ) -> Result<Option<String>> {
        let node = self.get_rule(name).unwrap();
        let mut expansions = node.expand(self, data)?;
        expansions.sort_by_cached_key(|e| OrderedFloat(score_by_varref_weights(e, weights)));
        Ok(expansions.last().map(|e| e.text.clone()))
    }

    /// Generates all possible expansions of the given rule, using the supplied
    /// data and weights.
    ///
    /// Returns expansions in descending order by score.
    pub fn generate_all_with_weights(
        &self,
        name: &str,
        data: &HashMap<String, String>,
        weights: &HashMap<String, f64>,
    ) -> Result<Vec<String>> {
        let node = self
            .get_rule(name)
            .ok_or_else(|| Error::UnknownNonTerminalError(name.to_string()))?;
        let mut expansions = node.expand(self, data)?;
        expansions.sort_by_cached_key(|e| OrderedFloat(score_by_varref_weights(e, weights)));
        Ok(expansions.into_iter().rev().map(|e| e.text).collect())
    }
}

fn score_by_varref_weights(expansion: &Expansion, weights: &HashMap<String, f64>) -> f64 {
    expansion
        .varrefs
        .iter()
        .map(|varref| weights.get(varref).unwrap_or(&1.0))
        .sum()
}

impl Default for Grammar {
    fn default() -> Self {
        let mut grammar = Grammar::new();
        grammar.modifiers = modifiers::get_default_modifiers();
        grammar
    }
}

impl ToString for Grammar {
    fn to_string(&self) -> String {
        let mut s = String::new();
        for (id, node) in &self.rules {
            // If the RHS is a sequence, we take advantage of the fact that
            // RHSes are an implicit sequence, and do not print the brackets
            // around it.
            match node {
                Node::Sequence(children) => {
                    s.push_str(&format!(
                        "{} = {}\n",
                        id,
                        children.iter().map(|n| n.to_string()).join("")
                    ));
                }
                _ => {
                    s.push_str(&format!("{} = {}\n", id, node.to_string()));
                }
            }
        }
        s
    }
}

impl FromStr for Grammar {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let mut grammar = parser::parse_grammar(s)?;
        grammar.modifiers = modifiers::get_default_modifiers();
        Ok(grammar)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::*;
    use maplit::hashmap;

    fn grammar_and_data() -> (Grammar, HashMap<String, String>) {
        let mut grammar = Grammar::default();
        grammar.add_rule(
            "location",
            Node::VarRef(VarRef::with_variable_and_modifier("city", "capitalize")),
        );
        let data = hashmap! {
            "name".to_string() => "John".to_string(),
            "city".to_string() => "london".to_string(),
        };
        (grammar, data)
    }

    #[test]
    fn test_expand_text() {
        let (grammar, data) = grammar_and_data();
        let node = Node::Text("hello".to_string());
        let expansions = node.expand(&grammar, &data).unwrap();
        assert_eq!(
            expansions,
            vec![Expansion {
                varrefs: vec![],
                text: "hello".to_string(),
            }]
        );
    }

    #[test]
    fn test_expand_varref() {
        let (grammar, data) = grammar_and_data();
        let node = Node::VarRef(VarRef::with_variable("name"));
        let expansions = node.expand(&grammar, &data).unwrap();
        assert_eq!(
            expansions,
            vec![Expansion {
                varrefs: vec!["name".to_string()],
                text: "John".to_string(),
            }]
        );
    }

    #[test]
    fn test_expand_nonterminal() {
        let (grammar, data) = grammar_and_data();
        let node = Node::NonTerminal("location".to_string());
        let expansions = node.expand(&grammar, &data).unwrap();
        assert_eq!(
            expansions,
            vec![Expansion {
                varrefs: vec!["city".to_string()],
                text: "London".to_string(),
            }]
        );
    }

    #[test]
    fn test_expand_sequence() {
        let (grammar, data) = grammar_and_data();
        let c1 = Node::Text("in ".to_string());
        let c2 = Node::NonTerminal("location".to_string());
        let node = Node::Sequence(vec![c1, c2]);
        let expansions = node.expand(&grammar, &data).unwrap();
        assert_eq!(
            expansions,
            vec![Expansion {
                varrefs: vec!["city".to_string()],
                text: "in London".to_string(),
            }]
        );
    }

    #[test]
    fn test_expand_optional() {
        let (grammar, data) = grammar_and_data();
        let hello = Node::Text("Hello ".to_string());
        let dear = Node::Text("dear ".to_string());
        let maybe_dear = Node::Optional(Box::new(dear));
        let friend = Node::Text("friend".to_string());
        let seq = Node::Sequence(vec![hello, maybe_dear, friend]);
        let expansions = seq.expand(&grammar, &data).unwrap();
        assert_eq!(
            HashSet::<_>::from_iter(expansions),
            HashSet::from_iter(vec![
                Expansion {
                    varrefs: vec![],
                    text: "Hello friend".to_string(),
                },
                Expansion {
                    varrefs: vec![],
                    text: "Hello dear friend".to_string(),
                }
            ])
        );
    }

    #[test]
    fn test_expand_choice() {
        let (grammar, data) = grammar_and_data();
        let snoopy = Node::Text("Snoopy".to_string());
        let name = Node::VarRef(VarRef::with_variable("name"));
        let linus = Node::Text("Linus".to_string());
        let choice = Node::Choice(vec![snoopy, name, linus]);
        let expansions = choice.expand(&grammar, &data).unwrap();
        assert_eq!(
            HashSet::<_>::from_iter(expansions),
            HashSet::from_iter(vec![
                Expansion {
                    varrefs: vec![],
                    text: "Snoopy".to_string(),
                },
                Expansion {
                    varrefs: vec!["name".to_string()],
                    text: "John".to_string(),
                },
                Expansion {
                    varrefs: vec![],
                    text: "Linus".to_string(),
                },
            ])
        );
    }

    #[test]
    fn test_to_string() {
        let mut grammar = Grammar::default();
        grammar.add_rule(
            "top",
            Node::Sequence(vec![
                Node::Text("hi ".to_string()),
                Node::VarRef(VarRef::with_variable("name")),
                Node::Text(" in ".to_string()),
                Node::NonTerminal("location".to_string()),
            ]),
        );
        grammar.add_rule(
            "location",
            Node::Sequence(vec![
                Node::Text("city of ".to_string()),
                Node::VarRef(VarRef::with_variable("city")),
            ]),
        );
        assert_eq!(
            HashSet::<_>::from_iter(grammar.to_string().split('\n').filter(|s| !s.is_empty())),
            HashSet::from_iter(vec![
                "top = hi #name# in <location>",
                "location = city of #city#",
            ])
        );
    }

    #[test]
    fn test_generate() {
        let grammar = Grammar::from_str(
            r#"
            top = Hi <name>?:[, my dear #gender#,] in <location>.
            name = #name#
            location = [city of #city#|#city# in #county# county]
            "#,
        )
        .unwrap();
        let data = hashmap! {
            "name".to_string() => "John".to_string(),
            "city".to_string() => "Janesville".to_string(),
            "county".to_string() => "Rock".to_string(),
        };
        let r = grammar.generate("top", &data).unwrap().unwrap();
        assert_eq!(r, "Hi John in Janesville in Rock county.");

        let exps = HashSet::<_>::from_iter(grammar.generate_all("top", &data).unwrap());
        assert_eq!(
            exps,
            HashSet::from_iter(vec![
                "Hi John in Janesville in Rock county.".to_string(),
                "Hi John in city of Janesville.".to_string(),
            ])
        );
    }
}