synox 0.1.0

Program synthesis of string transformations from input-output examples.
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
use super::input_data_graph::{Id, InputDataGraph};
use super::language::{
    ColumnIndex, Direction, Occurrence, Position, StringExpression, StringIndex,
    SubstringExpression,
};
use crate::graph;
use std::collections::{BTreeMap, BTreeSet, HashMap};

const EPSILON: usize = 1;
const KAPPA: usize = 15; // BlinkFill Section 7.3

type Node = usize;
type Edge = (Node, Node);

#[derive(Debug)]
pub struct Dag {
    start: Node,
    finish: Node,
    substrings: BTreeMap<Edge, Vec<SubstringExpressionSet>>,
    num_examples: usize,
}

impl Dag {
    fn new(input: &[&str], output: &str, graph: &InputDataGraph, row: usize) -> Self {
        let mut substrings = BTreeMap::new();
        let n = output.len();

        for i in 0..n {
            for j in i + 1..n + 1 {
                let s = &output[i..j];
                // learn the constant string
                let mut exprs = vec![ConstantString(String::from(s))];
                // learn all substring expressions
                for (ci, input_str) in input.iter().enumerate() {
                    // find all instances of s (including overlapping ones) in input_str
                    let id = Id { row, col: ci };
                    let mut offset = 0;
                    while offset < input_str.len() {
                        match input_str[offset..].find(&s) {
                            None => {
                                break;
                            }
                            Some(start) => {
                                let l = offset + start;
                                let r = l + s.len();
                                let (l, r) = (StringIndex(l + 1), StringIndex(r + 1));
                                let substring_exprs =
                                    SubstringExpressionSet::generate_substring_set(
                                        id, l, r, &graph,
                                    );
                                exprs.push(substring_exprs);
                                offset = offset + start + 1;
                            }
                        }
                    }
                }
                substrings.insert((i, j), exprs);
            }
        }

        Self {
            start: 0,
            finish: n,
            substrings,
            num_examples: 1,
        }
    }

    fn intersection(&self, other: &Self) -> Self {
        // uses roughly the same renumbering technique as InputDataGraph::intersection()
        let mut renumber: HashMap<Edge, Node> = HashMap::new();
        let mut curr = 0;
        let mut number = |n1, n2| -> Node {
            *renumber.entry((n1, n2)).or_insert_with(|| {
                let v = curr;
                curr += 1;
                v
            })
        };

        let mut substrings = BTreeMap::new();
        for ((v1s, v1f), s1) in &self.substrings {
            for ((v2s, v2f), s2) in &other.substrings {
                let vs = number(*v1s, *v2s);
                let vf = number(*v1f, *v2f);
                let mut exprs = vec![];
                for e1 in s1 {
                    for e2 in s2 {
                        if let Some(e) = e1.intersection(&e2) {
                            exprs.push(e);
                        }
                    }
                }
                if !exprs.is_empty() {
                    substrings.insert((vs, vf), exprs);
                }
            }
        }

        Self {
            start: number(self.start, other.start),
            finish: number(self.finish, other.finish),
            substrings,
            num_examples: self.num_examples + other.num_examples,
        }
    }

    pub fn learn(paired: &[(Vec<&str>, &str)], graph: &InputDataGraph) -> Self {
        paired
            .iter()
            .enumerate()
            .map(|(row, (input, output))| Self::new(&input, output, &graph, row))
            .fold(None, |acc, x| -> Option<Self> {
                match acc {
                    Some(acc) => Some(acc.intersection(&x)),
                    None => Some(x),
                }
            })
            .unwrap()
    }

    pub fn top_ranked_expression(&self, graph: &InputDataGraph) -> Option<StringExpression> {
        let distances = graph.distances();
        let ranks = graph.rank_nodes(&distances);
        let mut best_by_edge: BTreeMap<Edge, (usize, SubstringExpression)> = BTreeMap::new();
        // compute distances for edges
        let idg_adj = graph::adjacency_map(graph.edges());
        let idg_inv = graph::invert_adjacency_map(&idg_adj);
        for (edge, expr_set_set) in &self.substrings {
            let mut best: Option<SubstringExpression> = None;
            let mut best_score = 0;
            for expr_set in expr_set_set {
                let expr;
                let score;
                match expr_set {
                    ConstantString(s) => {
                        expr = Some(SubstringExpression::ConstantString(s.clone()));
                        score = s.len() * s.len() * EPSILON;
                    }
                    SubstringSet(ci, p_l, p_r) => {
                        let key = |p: &'_ &PositionSet| -> usize {
                            match p {
                                ConstantPosition(_) => 0,
                                GraphNode(v) => ranks[&v],
                            }
                        };
                        let p_l = p_l.iter().max_by_key(key).unwrap();
                        let p_r = p_r.iter().max_by_key(key).unwrap();

                        // NOTE all of the programs captured in the dag are consistent with the
                        // input-output examples, but they aren't necessarily even valid for the
                        // rest of the inputs.
                        //
                        // XXX Maybe we should find all the consistent (p_l, p_r) pairs first
                        // instead of taking the max by key above, and then after that take the max
                        // by key, so we don't accidentally throw away a good example and then be
                        // left with one that doesn't work. The BlinkFill paper isn't very clear
                        // about avoiding generating invalid programs for the input strings that
                        // don't have output examples.

                        let sample = |p: &PositionSet| -> Position {
                            match p {
                                ConstantPosition(k) => Position::ConstantPosition(*k),
                                GraphNode(v) => {
                                    // check in-edges
                                    let mut best: Option<Position> = None;
                                    let mut best_weight = (0, 0);
                                    // weight is (token.weight, -abs(occurence)), to favor certain
                                    // tokens, and then matches near the start or end
                                    if let Some(vss) = idg_inv.get(v) {
                                        for vs in vss {
                                            if let Some(toks) = graph.tokens.get(&(*vs, *v)) {
                                                for (tok, occ) in toks {
                                                    let weight = (tok.weight(), occ.weight());
                                                    if best == None || weight > best_weight {
                                                        best_weight = weight;
                                                        best = Some(Position::Match(
                                                            tok.clone(),
                                                            *occ,
                                                            Direction::End,
                                                        ));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    // check out-edges
                                    if let Some(vfs) = idg_adj.get(v) {
                                        for vf in vfs {
                                            if let Some(toks) = graph.tokens.get(&(*v, *vf)) {
                                                for (tok, occ) in toks {
                                                    let weight = (tok.weight(), occ.weight());
                                                    if best == None || weight > best_weight {
                                                        best_weight = weight;
                                                        best = Some(Position::Match(
                                                            tok.clone(),
                                                            *occ,
                                                            Direction::Start,
                                                        ));
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    // we should never get here; if we did, it means our
                                    // PositionSet was invalid
                                    best.expect("top_ranked_expression: no tokens for graph node")
                                }
                            }
                        };

                        let len: usize;
                        let mut bad = false;
                        match (p_l, p_r) {
                            (ConstantPosition(k1), ConstantPosition(k2)) => {
                                len = k2.0 as usize - k1.0 as usize;
                            }
                            (ConstantPosition(k), GraphNode(v)) => {
                                let k = k.0 as usize;
                                let mut sum = 0;
                                for (id, si) in &graph.labels[v] {
                                    if si.0 > k {
                                        // NOTE the paper isn't super clear about how to measure
                                        // the length of token-based matches (it depends on which
                                        // string we are matching); we could do it based on all the
                                        // strings (including ones we don't have input-output
                                        // examples for), or we could do it just for the ones
                                        // included in the examples; doing the former might make more
                                        // sense, so that is what we do here
                                        if id.row < self.num_examples {
                                            sum += si.0 - k;
                                        }
                                    } else {
                                        bad = true;
                                        break;
                                    }
                                }
                                len = sum / self.num_examples;
                            }
                            (GraphNode(v), ConstantPosition(k)) => {
                                // similar to the above case
                                // note: difference direction is opposite the above case
                                let k = k.0 as usize;
                                let mut sum = 0;
                                for (id, si) in &graph.labels[v] {
                                    if k > si.0 {
                                        if id.row < self.num_examples {
                                            sum += k - si.0;
                                        }
                                    } else {
                                        bad = true;
                                        break;
                                    }
                                }
                                len = sum / self.num_examples;
                            }
                            (GraphNode(v1), GraphNode(v2)) => {
                                let mut sum = 0;
                                for (id, si1) in &graph.labels[v1] {
                                    let si2 = graph.labels[v2][id];
                                    if si2.0 > si1.0 {
                                        if id.row < self.num_examples {
                                            sum += si2.0 - si1.0;
                                        }
                                    } else {
                                        bad = true;
                                        break;
                                    }
                                }
                                len = sum / self.num_examples;
                            }
                        }
                        expr = if !bad {
                            Some(SubstringExpression::Substring(
                                *ci,
                                sample(p_l),
                                sample(p_r),
                            ))
                        } else {
                            None
                        };
                        score = len * len * KAPPA;
                    }
                }
                if let Some(expr) = expr {
                    if best == None || score > best_score {
                        best = Some(expr);
                        best_score = score;
                    }
                }
            }
            if let Some(best) = best {
                best_by_edge.insert(*edge, (best_score, best));
            }
        }
        // find shortest path
        let adj = graph::adjacency_map(best_by_edge.keys());
        let path = graph::shortest_path_dag(&self.start, &self.finish, &adj, |v1, v2| {
            // negating because graph finds lowest cost path, we want highest score
            -(best_by_edge[&(*v1, *v2)].0 as isize)
        })?;
        Some(StringExpression(
            path.iter()
                .map(|e| best_by_edge.remove(e).unwrap().1)
                .collect(),
        ))
    }
}

#[derive(Debug, PartialEq, Eq)]
enum SubstringExpressionSet {
    ConstantString(String),
    SubstringSet(ColumnIndex, BTreeSet<PositionSet>, BTreeSet<PositionSet>),
}

use SubstringExpressionSet::*;

impl SubstringExpressionSet {
    // returns a SubstringSet
    fn generate_substring_set(
        id: Id,
        l: StringIndex,
        r: StringIndex,
        graph: &InputDataGraph,
    ) -> Self {
        let mut v_l = BTreeSet::new();
        let mut v_r = BTreeSet::new();
        for (v, labels) in &graph.labels {
            if labels.get(&id) == Some(&l) {
                v_l.insert(GraphNode(*v));
            } else if labels.get(&id) == Some(&r) {
                v_r.insert(GraphNode(*v));
            }
        }
        v_l.insert(ConstantPosition(Occurrence(l.0 as isize)));
        v_r.insert(ConstantPosition(Occurrence(r.0 as isize)));
        SubstringSet(ColumnIndex(id.col), v_l, v_r)
    }

    #[cfg(test)]
    fn denote(&self, graph: &InputDataGraph) -> BTreeSet<SubstringExpression> {
        let mut set: BTreeSet<SubstringExpression> = BTreeSet::new();
        match self {
            ConstantString(s) => {
                set.insert(SubstringExpression::ConstantString(s.clone()));
            }
            SubstringSet(ci, p_l, p_r) => {
                for p_l in p_l.iter().flat_map(|p_l| p_l.denote(graph)) {
                    for p_r in p_r.iter().flat_map(|p_r| p_r.denote(graph)) {
                        set.insert(SubstringExpression::Substring(
                            *ci,
                            p_l.clone(),
                            p_r.clone(),
                        ));
                    }
                }
            }
        }
        set
    }

    fn intersection(&self, other: &Self) -> Option<Self> {
        match (self, other) {
            (ConstantString(s1), ConstantString(s2)) if s1 == s2 => {
                Some(ConstantString(s1.clone()))
            }
            (SubstringSet(c1, p1_l, p1_r), SubstringSet(c2, p2_l, p2_r)) if c1 == c2 => {
                // return None if either intersection is empty; this is not necessary for
                // correctness but it's a performance optimization
                let p_l: BTreeSet<_> = p1_l.intersection(p2_l).cloned().collect();
                if p_l.is_empty() {
                    return None;
                }
                let p_r: BTreeSet<_> = p1_r.intersection(p2_r).cloned().collect();
                if p_r.is_empty() {
                    return None;
                }
                Some(SubstringSet(*c1, p_l, p_r))
            }
            _ => None,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
enum PositionSet {
    ConstantPosition(Occurrence),
    GraphNode(Node),
}

use PositionSet::*;

impl PositionSet {
    #[cfg(test)]
    fn denote(&self, graph: &InputDataGraph) -> BTreeSet<Position> {
        let mut set: BTreeSet<Position> = BTreeSet::new();
        match self {
            ConstantPosition(k) => {
                set.insert(Position::ConstantPosition(*k));
            }
            GraphNode(v) => {
                // find all edges that end at v or start at v
                for ((vs, vf), tok_occs) in &graph.tokens {
                    if vs == v {
                        for (tok, occ) in tok_occs {
                            set.insert(Position::Match(tok.clone(), *occ, Direction::Start));
                        }
                    } else if vf == v {
                        for (tok, occ) in tok_occs {
                            set.insert(Position::Match(tok.clone(), *occ, Direction::End));
                        }
                    }
                }
            }
        }
        set
    }
}

#[cfg(test)]
mod tests {
    use super::super::token::Token;
    use super::*;
    use crate::StringProgram;

    #[test]
    fn generate_substring_set() {
        // generate the graph from BlinkFill Fig. 14
        let strs = vec![
            vec!["Mumbai, India"],
            vec!["Los Angeles, United States of America"],
            vec!["Newark, United States"],
            vec!["New York, United States of America"],
            vec!["Wellington, New Zealand"],
            vec!["New Delhi, India"],
        ];
        let graph = InputDataGraph::new(&strs);
        // find the substring expression set that generates "India" from the 1st string
        let sub = SubstringExpressionSet::generate_substring_set(
            Id::new(0, 0),
            StringIndex(9),
            StringIndex(14),
            &graph,
        );
        let sub_denote = sub.denote(&graph);

        // spot check a couple things
        assert!(sub_denote.contains(&SubstringExpression::Substring(
            ColumnIndex(0),
            Position::Match(Token::ProperCase, Occurrence(2), Direction::Start),
            Position::Match(Token::ProperCase, Occurrence(2), Direction::End)
        )));
        assert!(sub_denote.contains(&SubstringExpression::Substring(
            ColumnIndex(0),
            Position::Match(Token::Caps, Occurrence(2), Direction::Start),
            Position::Match(Token::ProperCase, Occurrence(2), Direction::End)
        )));
        assert!(sub_denote.contains(&SubstringExpression::Substring(
            ColumnIndex(0),
            Position::Match(Token::Alphabets, Occurrence(-1), Direction::Start),
            Position::Match(Token::End, Occurrence(1), Direction::Start)
        )));
        // even though this next pattern would occur if the graph were built from only the first
        // string, this pattern doesn't match in the other strings, so it does not appear
        assert!(!sub_denote.contains(&SubstringExpression::Substring(
            ColumnIndex(0),
            Position::Match(
                Token::Literal(String::from("Mumbai, ")),
                Occurrence(1),
                Direction::End
            ),
            Position::Match(Token::End, Occurrence(1), Direction::Start)
        )));
        assert!(sub_denote.contains(&SubstringExpression::Substring(
            ColumnIndex(0),
            Position::Match(
                Token::Literal(String::from(", ")),
                Occurrence(1),
                Direction::End
            ),
            Position::Match(Token::End, Occurrence(1), Direction::Start)
        )));

        // make sure all the string programs generate the right string
        for prog in sub_denote {
            let output = prog.run(&vec!["Mumbai, India"]);
            assert_eq!(output.unwrap(), "India");
        }
    }

    #[test]
    fn generate_substring_set_single() {
        // similar to the negated case from above, with a different graph, should appear
        let strs = vec![vec!["Shrewsbury, MA"], vec!["Shrewsbury, United Kingdom"]];
        let graph = InputDataGraph::new(&strs);
        // find the substring expression set that generates "MA" from the 1st string
        let sub = SubstringExpressionSet::generate_substring_set(
            Id::new(0, 0),
            StringIndex(13),
            StringIndex(15),
            &graph,
        );
        let sub_denote = sub.denote(&graph);
        assert!(sub_denote.contains(&SubstringExpression::Substring(
            ColumnIndex(0),
            Position::Match(
                Token::Literal(String::from("Shrewsbury, ")),
                Occurrence(1),
                Direction::End
            ),
            Position::Match(Token::End, Occurrence(1), Direction::Start)
        )));
    }

    fn all_for(
        dag: &Dag,
        graph: &InputDataGraph,
        n1: Node,
        n2: Node,
    ) -> BTreeSet<SubstringExpression> {
        dag.substrings
            .get(&(n1, n2))
            .unwrap()
            .iter()
            .flat_map(|e| e.denote(&graph))
            .collect()
    }

    #[test]
    fn generate_dag() {
        let strs = vec![
            vec!["Mumbai, India"],
            vec!["Los Angeles, United States of America"],
            vec!["Newark, United States"],
            vec!["New York, United States of America"],
            vec!["Wellington, New Zealand"],
            vec!["New Delhi, India"],
        ];
        let graph = InputDataGraph::new(&strs);
        let dag = Dag::new(&strs[0], "India", &graph, 0);
        // some spot checks
        assert!(all_for(&dag, &graph, 0, 3)
            .contains(&SubstringExpression::ConstantString(String::from("Ind"))));
        assert!(
            all_for(&dag, &graph, 0, 1).contains(&SubstringExpression::Substring(
                ColumnIndex(0),
                Position::Match(Token::Caps, Occurrence(2), Direction::Start),
                Position::ConstantPosition(Occurrence(10))
            ))
        );
        assert!(
            all_for(&dag, &graph, 0, 5).contains(&SubstringExpression::Substring(
                ColumnIndex(0),
                Position::Match(Token::ProperCase, Occurrence(-1), Direction::Start),
                Position::Match(Token::End, Occurrence(1), Direction::Start),
            ))
        );
    }

    #[test]
    fn learn() {
        let strs = vec![
            vec!["Mumbai, India"],
            vec!["Los Angeles, United States of America"],
            vec!["Newark, United States"],
            vec!["New York, United States of America"],
            vec!["Wellington, New Zealand"],
            vec!["New Delhi, India"],
        ];
        let graph = InputDataGraph::new(&strs);
        let examples = vec![
            (strs[0].clone(), "India"),
            (strs[1].clone(), "United States of America"),
        ];
        let dag = Dag::learn(&examples, &graph);
        // check all expressions that extract output in one go
        let exprs = all_for(&dag, &graph, dag.start, dag.finish);
        for e in &exprs {
            for ex in &examples {
                assert_eq!(e.run(&ex.0).unwrap(), ex.1);
            }
        }
        // spot-check
        assert!(!exprs.contains(&SubstringExpression::Substring(
            ColumnIndex(0),
            Position::Match(Token::ProperCase, Occurrence(-1), Direction::Start),
            Position::Match(Token::End, Occurrence(1), Direction::Start),
        )));
        assert!(exprs.contains(&SubstringExpression::Substring(
            ColumnIndex(0),
            Position::Match(
                Token::Literal(String::from(", ")),
                Occurrence(1),
                Direction::End
            ),
            Position::Match(Token::End, Occurrence(1), Direction::Start),
        )));
        // check final program
        let best = dag.top_ranked_expression(&graph).unwrap();
        let expected = vec![
            "United States",
            "United States of America",
            "New Zealand",
            "India",
        ];
        for (i, s) in strs[2..].iter().enumerate() {
            assert_eq!(best.run(s).unwrap(), expected[i]);
        }
    }

    #[test]
    fn learn_2() {
        let strs = vec![
            vec!["323-708-7700"],
            vec!["(425).706.7709"],
            vec!["510.220.5586"],
            vec!["(471)-378-3829"],
        ];
        let graph = InputDataGraph::new(&strs);
        let examples = vec![
            (strs[0].clone(), "323-708-7700"),
            (strs[1].clone(), "425-706-7709"),
        ];
        let dag = Dag::learn(&examples, &graph);
        let best = dag.top_ranked_expression(&graph).unwrap();
        let expected = vec!["510-220-5586", "471-378-3829"];
        for (i, s) in strs[2..].iter().enumerate() {
            assert_eq!(best.run(s).unwrap(), expected[i]);
        }
    }

    #[test]
    fn learn_3() {
        let strs = vec![
            vec!["Brandon Henry Saunders"],
            vec!["Dafna Q. Chen"],
            vec!["William Lee"],
            vec!["Danelle D. Saunders"],
            vec!["Emilio William Conception"],
        ];
        let graph = InputDataGraph::new(&strs);
        let examples = vec![(strs[0].clone(), "B.S."), (strs[1].clone(), "D.C.")];
        let dag = Dag::learn(&examples, &graph);
        let best = dag.top_ranked_expression(&graph).unwrap();
        let expected = vec!["W.L.", "D.S.", "E.C."];
        for (i, s) in strs[2..].iter().enumerate() {
            assert_eq!(best.run(s).unwrap(), expected[i]);
        }
    }

    #[test]
    fn learn_4() {
        let strs = vec![
            vec!["GOPR0365.MP4.mp4"],
            vec!["GOPR0411.MP4.mp4"],
            vec!["GOPR0329.MP4.mp4"],
        ];
        let graph = InputDataGraph::new(&strs);
        let examples = vec![(strs[0].clone(), "GOPR0365.mp4")];
        let dag = Dag::learn(&examples, &graph);
        let best = dag.top_ranked_expression(&graph).unwrap();
        let expected = vec!["GOPR0411.mp4", "GOPR0329.mp4"];
        for (i, s) in strs[1..].iter().enumerate() {
            assert_eq!(best.run(s).unwrap(), expected[i]);
        }
    }

    #[test]
    fn learn_5() {
        let strs = vec![
            vec!["IMG_3246.JPG"],
            vec!["GOPR0411.MP4"],
            vec!["DSC_0324.jpg"],
            vec!["DSC0324.jpg"],
            vec!["RD392.HEIC"],
        ];
        let graph = InputDataGraph::new(&strs);
        let examples = vec![(strs[0].clone(), "IMG_3246")];
        let dag = Dag::learn(&examples, &graph);
        let best = dag.top_ranked_expression(&graph).unwrap();
        let expected = vec!["GOPR0411", "DSC_0324", "DSC0324", "RD392"];
        for (i, s) in strs[1..].iter().enumerate() {
            assert_eq!(best.run(s).unwrap(), expected[i]);
        }
    }

    #[test]
    fn learn_multi_column() {
        let strs = vec![
            vec!["1", "IMG_3246.JPG"],
            vec!["2", "GOPR0411.MP4"],
            vec!["3", "DSC_0324.jpg"],
            vec!["4", "DSC0324.jpg"],
            vec!["5", "RD392.HEIC"],
        ];
        let graph = InputDataGraph::new(&strs);
        let examples = vec![
            (strs[0].clone(), "1_IMG_3246"),
            (strs[1].clone(), "2_GOPR0411"),
        ];
        let dag = Dag::learn(&examples, &graph);
        let best = dag.top_ranked_expression(&graph).unwrap();
        let expected = vec!["3_DSC_0324", "4_DSC0324", "5_RD392"];
        for (i, s) in strs[2..].iter().enumerate() {
            assert_eq!(best.run(s).unwrap(), expected[i]);
        }
    }
}