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
use crate::datavalue::DataValue;
use crate::selector::{Offset, SelectorBuilder};
use crate::text::Text;
use crate::textselection::{ResultTextSelectionSet, TestTextSelection};
use crate::AnnotationBuilder;
use crate::StamError;
use crate::{api::*, ResultTextSelection};

use smallvec::SmallVec;

#[derive(Clone, Default)]
pub struct TranslateConfig {
    pub source_side: TranslationSide,

    /// Allow a simple translation as output, by default this is set to `false` as we usually want to have an transposed annotation
    pub allow_simple: bool,

    /// Modify existing annotations (changes them into simple translations)
    pub modify_existing: bool,

    /// Do not produce a translation annotation, only output the translated annotation (allow_simple must be set to false)
    /// This effectively throws away the provenance information.
    pub no_translation: bool,

    /// Identifier to assign to the newly outputted translation (if not set, a random one will be generated)
    pub translation_id: Option<String>,

    /// Identifier to assign to the newly outputted resegmentation (if any is created and this is not set, a random ID will be generated)
    pub resegmentation_id: Option<String>,

    /// Identifier to assign to the source annotation, if this is an existing one set existing_source_side as well.
    pub source_side_id: Option<String>,

    /// Indicates that the source part of the transposition is an existing annotation. This is usually set automatically after setting `source_side_id` to an existing ID.
    pub existing_source_side: bool,

    /// Do not produce a resegmentation annotation.
    /// This maps a translation directly and allows losing segmentation information.
    /// In doing so, it reduces complexity of the output annotations.
    /// If this is set, no resegmentations will be produced, but the resulting translations
    /// may lose some of its fine-grained information, which limits the ability to reuse them as a translation pivot
    /// for further translations.
    /// This only comes into play if `no_translation == false` , `existing_source_side == true` and `source_side_id.is_some()`.
    pub no_resegmentation: bool,

    /// Identifiers to assign to the annotation (if not set, random ones will be generated)
    /// The indices correspond to the various sides of the translation that is being translated over (in the same order, minus the source)
    pub target_side_ids: Vec<String>,

    /// Enable debug mode (to stderr)
    pub debug: bool,
}

#[derive(Clone)]
pub enum TranslationSide {
    /// Automatically determine the translation side
    Auto,
    /// The n'th item in a directional selector (0-indexed!)
    ByIndex(usize),
}

impl Default for TranslationSide {
    fn default() -> Self {
        TranslationSide::Auto
    }
}

pub trait Translatable<'store> {
    /// The translate function maps an annotation, textselection, or textselection set from
    /// one coordinate system to another. These mappings are defined in annotations called
    /// **translations** and are documented here: https://github.com/annotation/stam/blob/master/extensions/stam-translate/README.md
    /// translations link identical textual parts across resources, any annotations within
    /// the bounds of such a mapping can then be *translated* using this function to the other coordinate system.
    ///
    /// The `via` parameter expresses the translation that is being used.
    /// The result of a translate operation is itself again a translation.
    fn translate(
        &self,
        via: &ResultItem<'store, Annotation>,
        config: TranslateConfig,
    ) -> Result<Vec<AnnotationBuilder<'static>>, StamError>;
}

impl<'store> Translatable<'store> for ResultItem<'store, Annotation> {
    fn translate(
        &self,
        via: &ResultItem<'store, Annotation>,
        mut config: TranslateConfig,
    ) -> Result<Vec<AnnotationBuilder<'static>>, StamError> {
        if let Some(tset) = self.textselectionset() {
            if config.source_side_id.is_none() && self.id().is_some() {
                config.source_side_id = Some(
                    self.id()
                        .map(|x| x.to_string())
                        .unwrap_or_else(|| generate_id("", "")),
                );
                config.existing_source_side = true;
            }
            let modify_existing = config.modify_existing;
            Ok(tset
                .translate(via, config)?
                .into_iter()
                .map(|mut builder| {
                    if modify_existing {
                        //associate existing handle
                        builder = builder.with_handle(self.handle());
                        builder
                    } else if builder.data().is_empty() {
                        //target annotations will have empty data, the translation itself already has data (1), resegmentations already have data too (1):
                        //copy the data from the transalted annotation to the empty target annotations
                        for data in self.data() {
                            builder =
                                builder.with_existing_data(data.set().handle(), data.handle());
                        }
                        builder
                    } else {
                        builder
                    }
                })
                .collect())
        } else {
            Err(StamError::NoText(
                "Can not translate an annotation that references no text or text in multiple resources",
            ))
        }
    }
}

#[derive(Copy, Clone, Debug)]
enum RefMatchMode {
    /// Normal full match against reference textsel
    Normal,
    /// Zero width item matching at begin of reference textsel
    Begin,
    /// Zero width item matching at end of reference textsel
    End,
}

impl<'store> Translatable<'store> for ResultTextSelectionSet<'store> {
    fn translate(
        &self,
        via: &ResultItem<'store, Annotation>,
        config: TranslateConfig,
    ) -> Result<Vec<AnnotationBuilder<'static>>, StamError> {
        via.valid_translation()?; //check if the pivot is valid
        if self.inner().is_empty() {
            return Err(StamError::NoText(
                "Can not translate empty TextSelectionSet",
            ));
        }

        let mut builders: Vec<AnnotationBuilder<'static>> = Vec::with_capacity(3);
        // Keeps track of which side of the translation the source is found
        let mut source_side: Option<usize> = if let TranslationSide::ByIndex(i) = config.source_side
        {
            Some(i)
        } else {
            None
        };
        let mut refseqnrs: Vec<(usize, RefMatchMode)> = Vec::new(); //the sequence number of the covered text selections (in a particular side) and the match mode (full, begin, end)
                                                                    // Found (source) or mapped (target) text selections per side, the first index corresponds to a side
        let mut selectors_per_side: SmallVec<[Vec<SelectorBuilder<'static>>; 2]> = SmallVec::new();

        let resource = self.resource();
        let mut simple_translation = true; //falsify,  simple translation are not suitable as pivot (no-op)
        let mut resegment = false; //resegmentations are produced when the translated annotation covers multiple source text selections, and when users do not want to lose this segmentation (!no_resegmentation)

        if config.debug {
            eprintln!(
                "[stam translate] -------------- Translating via {:?} --------------",
                via.id()
            );
        }

        //pre-compute and store the pivot's (=via) annotations and underlying textselections so we don't have to do it multiple times
        let sides: SmallVec<
            [(
                SmallVec<[ResultTextSelection<'store>; 1]>, //textselections
                ResultItem<'store, Annotation>,             //annotation
            ); 2],
        > = via
            .annotations_in_targets(AnnotationDepth::One)
            .map(|annotation| (annotation.textselections().collect(), annotation))
            .collect();

        let mut sourcecoverage = 0;
        // match the current textselectionset against all the sides in a complex translation (or ascertain
        // that we are dealing with a simple translation instead) the source side that matches
        // can never be the same as the target side that is mapped to
        for tsel in self.inner().iter() {
            // iterate over all the sides of the pivot
            for (side_i, (textselections, annotation)) in sides.iter().enumerate() {
                simple_translation = false;
                if selectors_per_side.len() <= side_i {
                    selectors_per_side.push(Vec::new());
                }

                if config.debug {
                    let tsel = ResultTextSelection::Unbound(
                        self.rootstore(),
                        resource.as_ref(),
                        tsel.clone(),
                    );
                    eprintln!(
                        "[stam translate] Looking for source fragment \"{}\" in side {}",
                        tsel.text().replace("\n", "\\n"),
                        side_i
                    );
                }

                // We may have multiple text selections (tsel) to translate (all must be found)
                let mut remainder = Some(tsel.clone());

                //prevent needless reallocations if we have lots of textselections
                refseqnrs.reserve(annotation.as_ref().target().len());

                for (refseqnr, reftsel) in textselections.iter().enumerate() {
                    if reftsel.resource() == resource
                        && (source_side.is_none() || source_side == Some(side_i))
                    //source side check
                    {
                        //first handle the edge-case of zero-width elements
                        if remainder.is_some()
                            && remainder.unwrap().begin() == remainder.unwrap().end()
                        {
                            //We have a zero-width element to match, we can take some shortcuts now
                            let mut pos = 0;
                            let matchmode = if remainder.unwrap().begin() == reftsel.begin() {
                                pos = reftsel.begin();
                                Some(RefMatchMode::Begin)
                            } else if remainder.unwrap().begin() == reftsel.end() {
                                pos = reftsel.end();
                                Some(RefMatchMode::End)
                            } else {
                                None
                            };
                            if let Some(matchmode) = matchmode {
                                if config.debug {
                                    eprintln!("[stam translate] Found zero-width source fragment #{} at {} ({:?})",
                                            refseqnr,
                                            pos,
                                            matchmode
                                    );
                                }
                                refseqnrs.push((refseqnr, matchmode));
                                selectors_per_side[side_i].push(SelectorBuilder::TextSelector(
                                    resource.handle().into(),
                                    Offset::simple(reftsel.begin(), reftsel.end()),
                                ));
                                source_side = Some(side_i);
                                remainder = None;
                                break;
                            } else {
                                continue;
                            }
                        }

                        // normal behaviour:
                        // get the all reference text selections that are embedded in our text selection (tsel)
                        // we must have full coverage for a translation to be valid
                        while remainder.is_some()
                            && remainder.unwrap().test(
                                &TextSelectionOperator::embeds(),
                                reftsel.inner(),
                                resource.as_ref(),
                            )
                        {
                            refseqnrs.push((refseqnr, RefMatchMode::Normal));
                            selectors_per_side[side_i].push(SelectorBuilder::TextSelector(
                                resource.handle().into(),
                                reftsel.inner().into(),
                            ));
                            if let Some((_, new_remainder, _)) =
                                remainder.unwrap().intersection(reftsel.inner())
                            {
                                remainder = new_remainder;
                                if config.debug {
                                    if remainder.is_some() {
                                        eprintln!("[stam translate] Found source fragment #{}: \"{}\" (with remainder)",
                                            refseqnr,
                                            &reftsel.text().replace("\n", "\\n")
                                        );
                                    } else {
                                        eprintln!("[stam translate] Found source fragment #{}: \"{}\"  (no remainder)",
                                            refseqnr,
                                            &reftsel.text().replace("\n", "\\n")
                                        );
                                    }
                                }
                                if remainder.is_none() {
                                    //everything from the source is covered by the references
                                    source_side = Some(side_i);
                                    break;
                                }
                            } else {
                                unreachable!("[stam translate] Unexpected error: if text selections are embedded, there must be an intersection: remainder={:?} embedded={:?}", remainder, reftsel.inner());
                            }
                        }
                    }
                }
                if remainder.is_none() {
                    sourcecoverage += 1;
                    if refseqnrs.len() > 1 {
                        //one textselection from the annotation needs to be resegmented into multiple text selections from the source side
                        resegment = true;
                    }
                    break;
                } else {
                    //no full match
                    refseqnrs.clear();
                    selectors_per_side[side_i].clear();
                }
            }
            if simple_translation {
                break;
            }
        }

        if simple_translation {
            //translating over a simple translation is a no-op, as it can only
            //produce the pivot as output
            // We may have multiple text selections to translate (all must be found)
            return Err(StamError::TranslateError(
                format!("Can not translate over a simple translation, pivot has to be complex. Pivot ID={}", via.id().unwrap_or("unknown") ),
                "",
            ));
        } else {
            //complex translation
            if sourcecoverage != self.inner().len() {
                if sourcecoverage == 0 {
                    return Err(StamError::TranslateError(
                        format!(
                            "Source fragments ({} in total) could not be found in the complex translation pivot {})",
                            self.inner().len(),
                            via.id().unwrap_or("(no-id)"),
                        ),
                        "",
                    ));
                } else {
                    return Err(StamError::TranslateError(
                        format!(
                            "Not all source fragments were found in the complex translation pivot {} (only {} out of {} found)",
                            via.id().unwrap_or("(no-id)"),
                            sourcecoverage,
                            self.inner().len()
                        ),
                        "",
                    ));
                }
            }

            // now map the targets (there may be multiple target sides)
            for (side_i, (textselections, _)) in sides.iter().enumerate() {
                if selectors_per_side.len() <= side_i {
                    selectors_per_side.push(Vec::new());
                }
                if source_side != Some(side_i) {
                    for (refseqnr, matchmode) in refseqnrs.iter() {
                        //select the text selection we seek
                        let reftsel = textselections.get(*refseqnr).expect("element must exist");
                        let mapped_selector: SelectorBuilder<'static> = match matchmode {
                            RefMatchMode::Normal => SelectorBuilder::TextSelector(
                                reftsel.resource().handle().into(),
                                reftsel.inner().into(),
                            ),
                            RefMatchMode::Begin => SelectorBuilder::TextSelector(
                                reftsel.resource().handle().into(),
                                Offset::simple(reftsel.begin(), reftsel.begin()),
                            ),
                            RefMatchMode::End => SelectorBuilder::TextSelector(
                                reftsel.resource().handle().into(),
                                Offset::simple(reftsel.end(), reftsel.end()),
                            ),
                        };
                        selectors_per_side[side_i].push(mapped_selector);
                    }
                }
            }
        }

        if source_side.is_none() {
            return Err(StamError::TranslateError(
                    format!(
                        "No source fragments were found in the complex translation pivot {}, source side could not be identified, unable to translate",
                        via.id().unwrap_or("(no-id)"),
                    ),
                    "",
            ));
        }

        if (config.allow_simple || config.no_resegmentation || config.modify_existing) && resegment
        {
            //try to simplify the translation by joining adjacent selectors
            selectors_per_side =
                merge_selectors(selectors_per_side, source_side.unwrap(), config.debug);
            resegment = false;
        }

        match selectors_per_side[source_side.expect("source side must exist at this point")].len() {
            0 =>
                Err(StamError::TranslateError(
                    format!(
                        "No source fragments were found in the complex translation {}, source side has 0 fragments, unable to translate",
                        via.id().unwrap_or("(no-id)"),
                    ),
                    "",
                )),
            1 if config.modify_existing => {
                //turn existing input annotation into a simple translation (the caller will handle this further)
                if config.debug {
                    eprintln!("[stam translate] turning existing input annotation into simple translation");
                }
                builders.push(
                    AnnotationBuilder::new()
                        .with_data(
                            "https://w3id.org/stam/extensions/stam-translate/",
                            "Translation",
                            DataValue::Null,
                        )
                        .with_target(SelectorBuilder::DirectionalSelector(
                            selectors_per_side.into_iter().flatten().collect(),
                        )),
                );
                Ok(builders)
            }
            _ if config.modify_existing => {
                Err(StamError::TranslateError(
                    "When modify_existing is set, only annotations that reference a single text selection can be translated".into(),
                    "",
                ))
            }
            1 if config.allow_simple && !config.no_translation => {
                //output is a simple translation
                let translation_id = config
                    .translation_id
                    .clone()
                    .unwrap_or_else(|| generate_id("translation-", ""));
                if config.debug {
                    eprintln!("[stam translate] outputting simple translation {}", &translation_id);
                }
                builders.push(
                    AnnotationBuilder::new()
                        .with_id(translation_id)
                        .with_data(
                            "https://w3id.org/stam/extensions/stam-translate/",
                            "Translation",
                            DataValue::Null,
                        )
                        .with_target(SelectorBuilder::DirectionalSelector(
                            selectors_per_side.into_iter().flatten().collect(),
                        )),
                );
                Ok(builders)
            }
            _ => {
                //output is a complex translation
                let translation_id = config
                    .translation_id
                    .clone()
                    .unwrap_or_else(|| generate_id("translation-", ""));
                if config.debug {
                    eprintln!("[stam translate] outputting complex translation {}", &translation_id);
                }
                let mut translation_selectors: Vec<SelectorBuilder<'static>> = Vec::new();
                let mut target_id_iter = config.target_side_ids.into_iter();

                // ID for the source annotation in the translation
                let source_id = if resegment {
                    generate_id("", "-translationsource")
                } else {
                    config
                        .source_side_id
                        .clone()
                        .unwrap_or_else(|| generate_id("", "-translationsource"))
                };
                for (side_i, selectors) in selectors_per_side.into_iter().enumerate() {
                    if source_side != Some(side_i) || !config.existing_source_side {
                        //create an annotation for this side of the translation
                        let side_id = if source_side == Some(side_i) {
                            source_id.clone()
                        } else {
                            target_id_iter
                                .next()
                                .unwrap_or_else(|| generate_id("", "-translated"))
                        };
                        if selectors.len() == 1 {
                            builders.push(
                                AnnotationBuilder::new()
                                    .with_id(side_id.clone())
                                    .with_target(selectors.into_iter().next().expect("a selector must exist")),
                            );
                        } else {
                            builders.push(
                                AnnotationBuilder::new()
                                    .with_id(side_id.clone())
                                    .with_target(SelectorBuilder::DirectionalSelector(selectors)),
                            );
                        }
                        if !config.no_translation {
                            translation_selectors
                                .push(SelectorBuilder::AnnotationSelector(side_id.into(), None));
                        }
                    } else {
                        //we are handling the source side
                        if resegment {
                            //resegmentation annotation needed (input annotation covers multiple text selections from the reference source)
                            let resegmentation_id = config
                                .resegmentation_id
                                .clone()
                                .unwrap_or_else(|| generate_id("resegmentation-", ""));
                            if config.debug {
                                eprintln!("[stam translate] outputting resegmentated annotation {}", &source_id);
                            }

                            //add the resegmented annotation (will be the source side of the translation):
                            if selectors.len() == 1 {
                                builders.push(
                                    AnnotationBuilder::new()
                                        .with_id(source_id.clone())
                                        .with_target(selectors.into_iter().next().expect("a selector must exist")),
                                );
                            } else {
                                builders.push(
                                    AnnotationBuilder::new()
                                        .with_id(source_id.clone())
                                        .with_target(SelectorBuilder::DirectionalSelector(selectors)),
                                );
                            }

                            if !config.no_resegmentation
                                && config.existing_source_side
                                && config.source_side_id.is_some()
                            {
                                // create an extra resegmentation annotation
                                // linking the resegmented annotation to the real source
                                let real_source_id = config.source_side_id.clone().unwrap();
                                if config.debug {
                                    eprintln!("[stam translate] outputting resegmentation {} ({} -> {})", &resegmentation_id, &real_source_id, &source_id);
                                }
                                builders.push(
                                    AnnotationBuilder::new()
                                        .with_id(resegmentation_id)
                                        .with_data(
                                            "https://w3id.org/stam/extensions/stam-translate/",
                                            "Resegmentation",
                                            DataValue::Null,
                                        )
                                        .with_target(SelectorBuilder::DirectionalSelector(vec![
                                            SelectorBuilder::AnnotationSelector(
                                                real_source_id.into(),
                                                None,
                                            ),
                                            SelectorBuilder::AnnotationSelector(
                                                source_id.clone().into(),
                                                None,
                                            ),
                                        ])),
                                );
                            }
                        } else {
                            //no resegmentation needed
                            if !self.rootstore().annotation(source_id.as_str()).is_some() {
                                if !config.existing_source_side {
                                    //add a copied source annotation (will be the source side of the translation):
                                    //this occurs if the original one has no ID to link to
                                    if config.debug {
                                        eprintln!("[stam translate] adding a copy of the source annotation (because original has no ID to link to)");
                                    }
                                    if selectors.len() == 1 {
                                        builders.push(
                                            AnnotationBuilder::new()
                                                .with_id(source_id.clone())
                                                .with_target(selectors.into_iter().next().expect("a selector must exist")),
                                        );
                                    } else {
                                        builders.push(
                                            AnnotationBuilder::new()
                                                .with_id(source_id.clone())
                                                .with_target(SelectorBuilder::DirectionalSelector(selectors)),
                                        );
                                    }
                                } else {
                                    return Err(StamError::TranslateError(
                                        format!(
                                            "Expected existing source annotation with ID {}",
                                            source_id.as_str(),
                                        ),
                                        "",
                                    ));
                                }
                            }
                        }
                        translation_selectors.push(SelectorBuilder::AnnotationSelector(
                            source_id.clone().into(),
                            None,
                        ));
                    }
                }
                if !config.no_translation {
                    if translation_selectors.len() < 2 {
                        return Err(StamError::TranslateError(
                            format!(
                                "Expected two sides for the translation, got only {}",
                                translation_selectors.len(),
                            ),
                            "",
                        ));
                    }
                    builders.push(
                        AnnotationBuilder::new()
                            .with_id(translation_id)
                            .with_data(
                                "https://w3id.org/stam/extensions/stam-translate/",
                                "Translation",
                                DataValue::Null,
                            )
                            .with_target(SelectorBuilder::DirectionalSelector(translation_selectors)),
                    );
                }
                if config.debug {
                    eprintln!("[stam translate] returning {} annotations", builders.len());
                }
                Ok(builders)
            }
        }
    }
}

/// Merges adjacent selectors
/// Used when doing translations with lose_segmentation
/// Leads to simpler output (but less powerful)
fn merge_selectors(
    selectors_per_side: SmallVec<[Vec<SelectorBuilder<'static>>; 2]>,
    source_side: usize,
    debug: bool,
) -> SmallVec<[Vec<SelectorBuilder<'static>>; 2]> {
    let mut mergable = Vec::new();
    let mut need_merge = false;

    // scan the source side for adjacent selectors to be merged:
    let mut cursor: Option<isize> = None;
    // index of the begin selector
    let mut begin_index: usize = 0;
    if let Some(selectors) = selectors_per_side.get(source_side) {
        for (i, selector) in selectors.iter().enumerate() {
            if cursor.is_some() && Some(selector.offset().unwrap().begin.into()) != cursor {
                mergable.push((begin_index, i));
                begin_index = i + 1;
            }
            cursor = Some(selector.offset().unwrap().end.into());
        }
        mergable.push((begin_index, selectors.len() - 1)); //last one
        need_merge = selectors.len() > mergable.len()
    }

    if !need_merge {
        //nothing needs to be merged, just return input as is and save some time
        if debug {
            eprintln!("[stam translate] no merges needed");
        }
        return selectors_per_side;
    } else if debug {
        eprintln!(
            "[stam translate] merging selectors (indices): {:?}",
            mergable
        );
    }

    // merge the selectors
    let mut merged_selectors: SmallVec<[Vec<SelectorBuilder<'static>>; 2]> = SmallVec::new();
    for (_side, selectors) in selectors_per_side.into_iter().enumerate() {
        let mut new_selectors: Vec<SelectorBuilder<'static>> = Vec::new();
        let resource = selectors.get(0).unwrap().resource().unwrap();
        for (begin_index, end_index) in mergable.iter() {
            let begin: isize = selectors
                .get(*begin_index)
                .unwrap()
                .offset()
                .unwrap()
                .begin
                .into();
            let end: isize = selectors
                .get(*end_index)
                .unwrap()
                .offset()
                .unwrap()
                .end
                .into();
            new_selectors.push(SelectorBuilder::textselector(
                resource.clone(),
                Offset::simple(begin as usize, end as usize),
            ));
        }
        merged_selectors.push(new_selectors);
    }

    merged_selectors
}

impl<'store> ResultItem<'store, Annotation> {
    /// Tests if this annotaton is valid translation
    /// This is not an extensive test
    fn valid_translation(&self) -> Result<(), StamError> {
        if !self.as_ref().target().is_complex() {
            Err(StamError::TranslateError(
                format!(
                    "Annotation {} is not a valid translation. It must have a complex selector.",
                    self.id().unwrap_or("(no id)")
                ),
                "",
            ))
        } else {
            Ok(())
        }
    }
}