stam 0.18.7

STAM is a powerful library for dealing with stand-off annotations on text. This is the Rust 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
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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
#![allow(dead_code)]
use stam::*;
use std::collections::BTreeMap;

pub fn setup_example_1() -> Result<AnnotationStore, StamError> {
    //instantiate with builder pattern
    let store = AnnotationStore::new(Config::default().with_debug(true))
        .with_id("test")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("testres")
                .with_text("Hello world"),
        )?
        .with_dataset(
            AnnotationDataSetBuilder::new()
                .with_id("testdataset")
                .with_key("pos")
                .with_key_value_id("pos", "noun", "D1"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A1")
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(6, 11),
                ))
                .with_existing_data("testdataset", "D1"),
        )?;
    Ok(store)
}

pub fn setup_example_2() -> Result<AnnotationStore, StamError> {
    //instantiate with builder pattern
    let store = AnnotationStore::default()
        .with_id("test")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("testres")
                .with_text("Hello world"),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A1")
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(6, 11),
                ))
                .with_data_with_id("testdataset", "pos", "noun", "D1"),
        )?;
    Ok(store)
}

pub fn setup_example_3() -> Result<AnnotationStore, StamError> {
    //this example includes a higher-order annotation with relative offset
    let store = AnnotationStore::default()
        .with_id("test")
        .with_resource(TextResourceBuilder::new().with_id("testres").with_text(
            "I have no special talent. I am only passionately curious. -- Albert Einstein",
        ))?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("sentence2")
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(26, 57),
                ))
                .with_data("testdataset", "type", "sentence"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("sentence2word2")
                .with_target(SelectorBuilder::annotationselector(
                    "sentence2",
                    Some(Offset::simple(2, 4)),
                ))
                .with_data("testdataset", "type", "word"),
        )?;
    Ok(store)
}

pub fn setup_example_4() -> Result<AnnotationStore, StamError> {
    //instantiate with builder pattern
    let store = AnnotationStore::default()
        .with_id("test")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("testres")
                .with_text("Hello world"),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A1")
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(6, 11),
                ))
                .with_data_with_id("testdataset", "pos", "noun", "D1"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A2")
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(0, 5),
                ))
                .with_data_with_id("testdataset", "pos", "interjection", "D2"),
        )?;
    Ok(store)
}
pub fn setup_example_multiselector_notranged() -> Result<AnnotationStore, StamError> {
    let store = AnnotationStore::default()
        .with_id("test")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("testres")
                .with_text("Hello world"),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(AnnotationBuilder::new().with_id("A1").with_target(
            SelectorBuilder::textselector("testres", Offset::simple(6, 11)),
        ))?
        .with_annotation(AnnotationBuilder::new().with_id("A2").with_target(
            SelectorBuilder::textselector("testres", Offset::simple(0, 5)),
        ))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("WordAnnotation")
                .with_target(SelectorBuilder::multiselector([
                    //because of the way we already constructed earlier annotations, this will NOT be an internal ranged selector
                    SelectorBuilder::textselector("testres", Offset::simple(0, 5)),
                    SelectorBuilder::textselector("testres", Offset::simple(6, 11)),
                ]))
                .with_data_with_id("testdataset", "type", "word", "WordAnnotationData"),
        )?;
    Ok(store)
}

pub fn setup_example_multiselector_ranged() -> Result<AnnotationStore, StamError> {
    let store = AnnotationStore::default()
        .with_id("test")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("testres")
                .with_text("Hello world"),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("WordAnnotation")
                .with_target(SelectorBuilder::multiselector([
                    //this translates to an internal ranged selector internally!
                    SelectorBuilder::textselector("testres", Offset::simple(0, 5)),
                    SelectorBuilder::textselector("testres", Offset::simple(6, 11)),
                ]))
                .with_data_with_id("testdataset", "type", "word", "WordAnnotationData"),
        )?;
    Ok(store)
}

pub fn setup_example_multiselector_2() -> Result<AnnotationStore, StamError> {
    let store = AnnotationStore::default()
        .with_id("test")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("testres")
                .with_text("Hello world"),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A1")
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(6, 11),
                ))
                .with_data_with_id("testdataset", "pos", "noun", "D1"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A2")
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(0, 5),
                ))
                .with_data_with_id("testdataset", "pos", "interjection", "D2"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("WordAnnotation")
                .with_target(SelectorBuilder::multiselector([
                    SelectorBuilder::textselector("testres", Offset::simple(0, 5)),
                    SelectorBuilder::textselector("testres", Offset::simple(6, 11)),
                ]))
                .with_data_with_id("testdataset", "type", "word", "WordAnnotationData"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("AllPosAnnotation")
                .with_target(SelectorBuilder::multiselector(vec![
                    SelectorBuilder::annotationselector("A1", Some(Offset::whole())),
                    SelectorBuilder::annotationselector("A2", Some(Offset::whole())),
                ]))
                .with_data_with_id("testdataset", "hastype", "pos", "AllPosAnnotationData"),
        )?;
    Ok(store)
}

const EXAMPLE5_TEXT: &str = "
Article 1

All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.

Article 2

Everyone is entitled to all the rights and freedoms set forth in this Declaration, without distinction of any kind, such as race, colour, sex, language, religion, political or other opinion, national or social origin, property, birth or other status. Furthermore, no distinction shall be made on the basis of the political, jurisdictional or international status of the country or territory to which a person belongs, whether it be independent, trust, non-self-governing or under any other limitation of sovereignty.

Article 3

Everyone has the right to life, liberty and security of person.

Article 4

No one shall be held in slavery or servitude; slavery and the slave trade shall be prohibited in all their forms.
";

pub fn setup_example_5() -> Result<AnnotationStore, StamError> {
    let store = AnnotationStore::default()
        .with_id("example5")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("humanrights")
                .with_text(EXAMPLE5_TEXT),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?;
    Ok(store)
}

pub fn setup_example_6() -> Result<AnnotationStore, StamError> {
    let store = AnnotationStore::default()
        .with_id("example6")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("humanrights")
                .with_text("All human beings are born free and equal in dignity and rights."),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("Sentence1")
                .with_target(SelectorBuilder::textselector(
                    "humanrights",
                    Offset::whole(),
                ))
                .with_data("myset", "type", "sentence"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("Phrase1")
                .with_target(SelectorBuilder::textselector(
                    "humanrights",
                    Offset::simple(17, 40), //"are born free and equal"
                ))
                .with_data("myset", "type", "phrase"),
        )?;

    Ok(store)
}

pub fn annotate_phrases_for_example_6(
    store: &mut AnnotationStore,
) -> Result<AnnotationHandle, StamError> {
    store.annotate(
        AnnotationBuilder::new()
            .with_id("Phrase2")
            .with_target(
                SelectorBuilder::textselector("humanrights", Offset::simple(4, 25)), //"human beings are born",
            )
            .with_data("myset", "type", "phrase"),
    )?;
    store.annotate(
        AnnotationBuilder::new()
            .with_id("Phrase3")
            .with_target(
                SelectorBuilder::textselector("humanrights", Offset::simple(44, 62)), //"dignity and rights",
            )
            .with_data("myset", "type", "phrase"),
    )
}

pub fn annotate_regex_for_example_6(store: &mut AnnotationStore) -> Result<(), StamError> {
    let resource = store.resource("humanrights").unwrap();
    let annotations: Vec<_> = resource
        .find_text_regex(&[Regex::new(r"Article \d").unwrap()], None, true)?
        .into_iter()
        .map(|foundmatch| {
            let offset: Offset = foundmatch.textselections().first().unwrap().into();
            AnnotationBuilder::new()
                .with_target(SelectorBuilder::textselector(resource.handle(), offset))
                .with_data("myset", "type", "header")
        })
        .collect();
    store.annotate_from_iter(annotations)?;
    Ok(())
}

pub fn setup_example_6b() -> Result<AnnotationStore, StamError> {
    // variant of example 6 (full version)
    // tree of annotations with larger annotations before smaller ones
    // using annotationselectors to point from smaller ones to relative offsets in the larger ones

    let mut store = AnnotationStore::default()
        .with_id("example6")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("humanrights")
                .with_text("All human beings are born free and equal in dignity and rights."),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("Sentence1")
                .with_target(SelectorBuilder::textselector(
                    "humanrights",
                    Offset::whole(),
                ))
                .with_data("myset", "type", "sentence"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("Phrase1")
                .with_target(SelectorBuilder::annotationselector(
                    "Sentence1",
                    Some(Offset::simple(17, 40)), //"are born free and equal"
                ))
                .with_data("myset", "type", "phrase"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("Phrase2")
                .with_target(
                    SelectorBuilder::annotationselector("Sentence1", Some(Offset::simple(4, 25))), //"human beings are born",
                )
                .with_data("myset", "type", "phrase"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("Phrase3")
                .with_target(
                    SelectorBuilder::annotationselector("Sentence1", Some(Offset::simple(44, 62))), //"dignity and rights",
                )
                .with_data("myset", "type", "phrase"),
        )?;

    let resource = store.resource("humanrights").expect("resource must exist");
    let annotations: Vec<_> = resource
        .split_text(" ")
        .filter_map(|word| {
            let word = word.trim_text_with(|x| x.is_ascii_punctuation()).unwrap();
            if !word.is_empty() {
                let sentence = store.annotation("Sentence1").unwrap();
                let builder = AnnotationBuilder::new()
                    .with_target(SelectorBuilder::annotationselector(
                        sentence.handle(),
                        word.relative_offset(
                            &sentence.textselections().next().unwrap(),
                            OffsetMode::default(),
                        ),
                    ))
                    .with_data("myset", "type", "word");
                Some(builder)
            } else {
                None
            }
        })
        .collect();

    store.annotate_from_iter(annotations)?;

    Ok(store)
}

pub fn setup_example_6c() -> Result<AnnotationStore, StamError> {
    // variant of example 6 (full version)
    // tree of annotations with smaller annotations before larger ones
    // using composite selectors to form larger ones from smaller ones

    //starts the same
    let mut store = AnnotationStore::default()
        .with_id("example6")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("humanrights")
                .with_text("All human beings are born free and equal in dignity and rights."),
        )?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?;

    //we first annotate the words before anything else, the handles are returned in a vector:
    let words = annotate_words(&mut store, "humanrights")?;

    //then we form a sentence over all the words (we have only one sentence here anyway)
    let sentence = AnnotationBuilder::new()
        .with_id("Sentence1")
        .with_data("myset", "type", "sentence")
        .with_target(SelectorBuilder::CompositeSelector({
            let mut tokens: Vec<_> = words
                .iter()
                .map(|annotation| {
                    SelectorBuilder::annotationselector(*annotation, Some(Offset::whole()))
                })
                .collect();
            //the final punctuation is added directly on the text, we're allowed to mix in a CompositeSelector
            tokens.push(SelectorBuilder::textselector(
                "humanrights",
                Offset::new(Cursor::EndAligned(-1), Cursor::EndAligned(0)),
            ));
            tokens
        }));
    store.annotate(sentence)?;

    //Now we add the phrases, they point to the words too
    let phrase1 = AnnotationBuilder::new()
        .with_id("Phrase1")
        .with_data("myset", "type", "phrase")
        .with_target(SelectorBuilder::CompositeSelector(
            words[3..8] //we know which words to select ("are born free and equal")
                .iter()
                .map(|annotation| {
                    SelectorBuilder::annotationselector(*annotation, Some(Offset::whole()))
                })
                .collect(),
        ));
    store.annotate(phrase1)?;

    let phrase2 = AnnotationBuilder::new()
        .with_id("Phrase2")
        .with_data("myset", "type", "phrase")
        .with_target(SelectorBuilder::CompositeSelector(
            words[1..5] //we know which words to select ("human beings are born")
                .iter()
                .map(|annotation| {
                    SelectorBuilder::annotationselector(*annotation, Some(Offset::whole()))
                })
                .collect(),
        ));
    store.annotate(phrase2)?;

    let phrase3 = AnnotationBuilder::new()
        .with_id("Phrase3")
        .with_data("myset", "type", "phrase")
        .with_target(SelectorBuilder::CompositeSelector(
            words[9..] //we know which words to select
                .iter()
                .map(|annotation| {
                    SelectorBuilder::annotationselector(*annotation, Some(Offset::whole()))
                })
                .collect(),
        ));
    store.annotate(phrase3)?;

    Ok(store)
}

pub fn annotate_words(
    store: &mut AnnotationStore,
    resource: impl Request<TextResource>,
) -> Result<Vec<AnnotationHandle>, StamError> {
    let resource = store.resource(resource).expect("resource");

    //this is a very basic tokeniser:
    let annotations: Vec<_> = resource
        .split_text(" ")
        .filter_map(|word| {
            let word = word.trim_text_with(|x| x.is_ascii_punctuation()).unwrap();
            if !word.is_empty() {
                let builder = AnnotationBuilder::new()
                    .with_target(SelectorBuilder::textselector(resource.handle(), &word))
                    .with_data("myset", "type", "word");
                Some(builder)
            } else {
                None
            }
        })
        .collect();

    store.annotate_from_iter(annotations)
}

pub fn setup_example_7(n: usize) -> Result<AnnotationStore, StamError> {
    //note: this is very slow when not compiled with optimisations if n gets large!!

    let mut store = AnnotationStore::new(Config::default()).with_id("test");

    //artificial text with 100,000 Xs
    let mut text = String::with_capacity(n);
    for _ in 0..n {
        text.push('X');
    }
    store.add_resource(
        TextResourceBuilder::new()
            .with_id("testres")
            .with_text(text),
    )?;
    store.add_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?;

    for i in 0..n {
        store.annotate(
            AnnotationBuilder::new()
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(i, i + 1),
                ))
                .with_id(format!("A{}", i))
                .with_data("testdataset", "type", "X")
                .with_data("testdataset", "n", i),
        )?;

        store.annotate(
            AnnotationBuilder::new()
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(i, i + 1),
                ))
                .with_id(format!("P{}", i))
                .with_data(
                    "testdataset",
                    "parity",
                    if i % 2 == 0 { "even" } else { "odd" },
                ),
        )?;
    }

    for i in 0..n - 1 {
        store.annotate(
            AnnotationBuilder::new()
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(i, i + 2),
                ))
                .with_id(format!("B{}", i))
                .with_data("testdataset", "type", "bigram"),
        )?;

        let left = store.annotation(format!("A{}", i)).unwrap().handle();
        let right = store.annotation(format!("A{}", i + 1)).unwrap().handle();

        store.annotate(
            AnnotationBuilder::new()
                .with_target(SelectorBuilder::compositeselector([
                    SelectorBuilder::annotationselector(left, Some(Offset::whole())),
                    SelectorBuilder::annotationselector(right, Some(Offset::whole())),
                ]))
                .with_id(format!("C{}", i))
                .with_data("testdataset", "type", "composite_bigram"),
        )?;
    }

    Ok(store)
}

pub fn setup_example_8() -> Result<AnnotationStore, StamError> {
    //simple transposition
    let store = AnnotationStore::default()
        .with_id("example8")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("humanrights")
                .with_text("all human beings are born free and equal in dignity and rights."),
        )?
        .with_resource(
            TextResourceBuilder::new()
                .with_id("warhol")
                .with_text("human beings are born solitary, but everywhere they are in chains."),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("SimpleTransposition1")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::textselector("humanrights", Offset::simple(4, 25)), //"human beings are born",
                    SelectorBuilder::textselector("warhol", Offset::simple(0, 21)), //"human beings are born",
                ]))
                .with_data(
                    "https://w3id.org/stam/extensions/stam-transpose/",
                    "Transposition",
                    DataValue::Null,
                ),
        )?;
    Ok(store)
}

pub fn setup_example_8b() -> Result<AnnotationStore, StamError> {
    //complex transposition

    let store = AnnotationStore::default()
        .with_id("example8")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("humanrights")
                .with_text("all human beings are born free and equal in dignity and rights."),
        )?
        .with_resource(
            TextResourceBuilder::new()
                .with_id("warhol")
                .with_text("human beings are born solitary, but everywhere they are in chains."),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A1")
                .with_target(
                    SelectorBuilder::textselector("humanrights", Offset::simple(4, 25)), //"human beings are born",
                )
                .with_data("testdataset", "type", "phrase"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A2")
                .with_target(
                    SelectorBuilder::textselector("warhol", Offset::simple(0, 21)), //"human beings are born",
                )
                .with_data("testdataset", "type", "phrase"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("ComplexTransposition1")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::annotationselector("A1", None),
                    SelectorBuilder::annotationselector("A2", None),
                ]))
                .with_data(
                    "https://w3id.org/stam/extensions/stam-transpose/",
                    "Transposition",
                    DataValue::Null,
                ),
        )?;
    Ok(store)
}

pub fn setup_example_8c() -> Result<AnnotationStore, StamError> {
    //complex transposition (we'll use this to test the word 'human' which is split over two textselections in this transposition)

    let store =
        AnnotationStore::default()
            .with_id("example8")
            .with_resource(
                TextResourceBuilder::new()
                    .with_id("humanrights")
                    .with_text("all human beings are born free and equal in dignity and rights."),
            )?
            .with_resource(TextResourceBuilder::new().with_id("warhol").with_text(
                "hu-\nman beings are born solitary, but everywhere they are in chains.",
            ))?
            .with_annotation(
                AnnotationBuilder::new()
                    .with_id("A1")
                    .with_target(SelectorBuilder::DirectionalSelector(vec![
                        SelectorBuilder::textselector("humanrights", Offset::simple(4, 6)), //"hu",
                        SelectorBuilder::textselector("humanrights", Offset::simple(6, 25)), //"man beings are born",
                    ]))
                    .with_data("testdataset", "type", "phrase"),
            )?
            .with_annotation(
                AnnotationBuilder::new()
                    .with_id("A2")
                    .with_target(SelectorBuilder::DirectionalSelector(vec![
                        SelectorBuilder::textselector("warhol", Offset::simple(1, 3)), //"hu",
                        SelectorBuilder::textselector("warhol", Offset::simple(5, 24)), //"man beings are born",
                    ]))
                    .with_data("testdataset", "type", "phrase"),
            )?
            .with_annotation(
                AnnotationBuilder::new()
                    .with_id("ComplexTransposition1")
                    .with_target(SelectorBuilder::DirectionalSelector(vec![
                        SelectorBuilder::annotationselector("A1", None),
                        SelectorBuilder::annotationselector("A2", None),
                    ]))
                    .with_data(
                        "https://w3id.org/stam/extensions/stam-transpose/",
                        "Transposition",
                        DataValue::Null,
                    ),
            )?;
    Ok(store)
}

pub fn setup_example_9() -> Result<AnnotationStore, StamError> {
    let store = AnnotationStore::default()
        .with_id("example9")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("example9")
                .with_text("the big dog licks the tiny cat"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("np1")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(0, 11)), //"the big dog",
                )
                .with_data("testdataset", "pos", "np"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("np2")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(18, 30)), //"the tiny cat",
                )
                .with_data("testdataset", "pos", "np"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("det1")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(0, 3)), //"the",
                )
                .with_data("testdataset", "pos", "det"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("det2")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(18, 21)), //"the",
                )
                .with_data("testdataset", "pos", "det"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("adj1")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(4, 7)), //"big",
                )
                .with_data("testdataset", "pos", "adj"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("adj2")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(22, 26)), //"tiny",
                )
                .with_data("testdataset", "pos", "adj"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("n1")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(8, 11)), //"dog",
                )
                .with_data("testdataset", "pos", "n"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("n2")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(27, 30)), //"cat",
                )
                .with_data("testdataset", "pos", "n"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("v1")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(12, 17)), //"licks",
                )
                .with_data("testdataset", "pos", "v"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("vp1")
                .with_target(
                    SelectorBuilder::textselector("example9", Offset::simple(12, 30)), //"licks the tiny cat",
                )
                .with_data("testdataset", "pos", "vp"),
        )?;
    Ok(store)
}

pub fn setup_example_10() -> Result<AnnotationStore, StamError> {
    //this example includes a map
    let store = AnnotationStore::default()
        .with_id("test")
        .with_resource(TextResourceBuilder::new().with_id("testres").with_text(
            "I have no special talent. I am only passionately curious. -- Albert Einstein",
        ))?
        .with_dataset(AnnotationDataSetBuilder::new().with_id("testdataset"))?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("einstein")
                .with_target(SelectorBuilder::textselector(
                    "testres",
                    Offset::simple(61, 76),
                ))
                .with_data(
                    "testdataset",
                    "person",
                    BTreeMap::from([
                        ("firstname".to_string(), "Albert".into()),
                        ("lastname".to_string(), "Einstein".into()),
                    ]),
                ),
        )?;
    Ok(store)
}

pub fn setup_example_11() -> Result<AnnotationStore, StamError> {
    //simple translation
    let store = AnnotationStore::default()
        .with_id("example11")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("source")
                .with_text("Я благодарю вас"),
        )?
        .with_resource(
            TextResourceBuilder::new()
                .with_id("translit")
                .with_text("Ya blagodaryu vas"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("SimpleTranslation1")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::textselector("source", Offset::simple(2, 11)), //"благодарю",
                    SelectorBuilder::textselector("translit", Offset::simple(3, 13)), //"blagodaryu",
                ]))
                .with_data(
                    "https://w3id.org/stam/extensions/stam-translate/",
                    "Translation",
                    DataValue::Null,
                ),
        )?;
    Ok(store)
}

pub fn setup_example_11b() -> Result<AnnotationStore, StamError> {
    //complex translation

    let store = AnnotationStore::default()
        .with_config(Config::default().with_debug(true))
        .with_id("example11b")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("source")
                .with_text("Я благодарю вас"),
        )?
        .with_resource(
            TextResourceBuilder::new()
                .with_id("translit")
                .with_text("Ya blagodaryu vas"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A1")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::textselector("source", Offset::simple(0, 1)), //"Я",
                    SelectorBuilder::textselector("source", Offset::simple(1, 2)), //" ",
                    SelectorBuilder::textselector("source", Offset::simple(2, 11)), //"благодарю",
                    SelectorBuilder::textselector("source", Offset::simple(11, 12)), //" ",
                    SelectorBuilder::textselector("source", Offset::simple(12, 15)), //"вас",
                ]))
                .with_data("testdataset", "type", "phrase"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A2")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::textselector("translit", Offset::simple(0, 2)), //"Ya",
                    SelectorBuilder::textselector("translit", Offset::simple(2, 3)), //" ",
                    SelectorBuilder::textselector("translit", Offset::simple(3, 13)), //"blagodaryu",
                    SelectorBuilder::textselector("translit", Offset::simple(13, 14)), //" ",
                    SelectorBuilder::textselector("translit", Offset::simple(14, 17)), //"vas",
                ]))
                .with_data("testdataset", "type", "phrase"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("ComplexTranslation1")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::annotationselector("A1", None),
                    SelectorBuilder::annotationselector("A2", None),
                ]))
                .with_data(
                    "https://w3id.org/stam/extensions/stam-translate/",
                    "Translation",
                    DataValue::Null,
                ),
        )?;
    Ok(store)
}

pub fn setup_example_11c() -> Result<AnnotationStore, StamError> {
    //complex translation with deletion

    let store = AnnotationStore::default()
        .with_id("example11c")
        .with_resource(
            TextResourceBuilder::new()
                .with_id("physical")
                .with_text("Do not inter-\nrupt writers!"),
        )?
        .with_resource(
            TextResourceBuilder::new()
                .with_id("logical")
                .with_text("Do not interrupt writers!"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A1")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::textselector("physical", Offset::simple(7, 12)), //"inter",
                    SelectorBuilder::textselector("physical", Offset::simple(12, 14)), //"-\n",
                    SelectorBuilder::textselector("physical", Offset::simple(14, 18)), //"rupt",
                ]))
                .with_data("testdataset", "type", "phrase"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("A2")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::textselector("logical", Offset::simple(7, 12)), //"inter",
                    SelectorBuilder::textselector("logical", Offset::simple(12, 12)), //"",
                    SelectorBuilder::textselector("logical", Offset::simple(12, 16)), //"rupt",
                ]))
                .with_data("testdataset", "type", "phrase"),
        )?
        .with_annotation(
            AnnotationBuilder::new()
                .with_id("ComplexTranslation1")
                .with_target(SelectorBuilder::DirectionalSelector(vec![
                    SelectorBuilder::annotationselector("A1", None),
                    SelectorBuilder::annotationselector("A2", None),
                ]))
                .with_data(
                    "https://w3id.org/stam/extensions/stam-translate/",
                    "Translation",
                    DataValue::Null,
                ),
        )?;
    Ok(store)
}