annatomic 0.4.0

The Annatomic annotation editor is intended to be used for the [RIDGES corpus](https://www.linguistik.hu-berlin.de/en/institut-en/professuren-en/korpuslinguistik/research/ridges-projekt). It is based on [graphANNIS](https://github.com/korpling/graphANNIS) and thus is internal data model is in principle suitable for a wide range of annotation concepts. "
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
use anyhow::{Context, Result, anyhow};
use graphannis::{AnnotationGraph, graph::GraphStorage, model::AnnotationComponentType};
use graphannis_core::{
    annostorage::NodeAnnotationStorage,
    dfs::CycleSafeDFS,
    graph::{ANNIS_NS, DEFAULT_NS, storage::union::UnionEdgeContainer},
    types::{AnnoKey, Component, NodeID},
};

use itertools::Itertools;
use lazy_static::lazy_static;
use std::{
    cmp::Ordering,
    collections::{BTreeMap, BTreeSet, HashSet},
    sync::Arc,
};

#[derive(Clone)]
pub struct TokenHelper<'a> {
    node_annos: &'a dyn NodeAnnotationStorage,
    cov_edges: Vec<Arc<dyn GraphStorage>>,
    ordering_gs: BTreeMap<String, Arc<dyn GraphStorage>>,
    part_of_gs: Arc<dyn GraphStorage>,
}

lazy_static! {
    static ref COMPONENT_LEFT: Component<AnnotationComponentType> = {
        Component::new(
            AnnotationComponentType::LeftToken,
            ANNIS_NS.into(),
            "".into(),
        )
    };
    static ref COMPONENT_RIGHT: Component<AnnotationComponentType> = {
        Component::new(
            AnnotationComponentType::RightToken,
            ANNIS_NS.into(),
            "".into(),
        )
    };
    pub static ref TOKEN_KEY: Arc<AnnoKey> = Arc::from(AnnoKey {
        ns: ANNIS_NS.into(),
        name: "tok".into(),
    });
}

impl<'a> TokenHelper<'a> {
    pub fn new(graph: &'a AnnotationGraph) -> anyhow::Result<TokenHelper<'a>> {
        let cov_edges: Vec<Arc<dyn GraphStorage>> = graph
            .get_all_components(Some(AnnotationComponentType::Coverage), None)
            .into_iter()
            .filter_map(|c| graph.get_graphstorage(&c))
            .filter(|gs| {
                if let Some(stats) = gs.get_statistics() {
                    stats.nodes > 0
                } else {
                    true
                }
            })
            .collect();
        let mut ordering_gs = BTreeMap::new();

        for c in graph.get_all_components(Some(AnnotationComponentType::Ordering), None) {
            if let Some(gs) = graph.get_graphstorage(&c) {
                ordering_gs.insert(c.name.to_string(), gs);
            }
        }

        let part_of_component =
            Component::new(AnnotationComponentType::PartOf, ANNIS_NS.into(), "".into());
        let part_of_gs = graph
            .get_graphstorage(&part_of_component)
            .ok_or_else(|| anyhow!("Missing PartOf component"))?;

        Ok(TokenHelper {
            node_annos: graph.get_node_annos(),
            cov_edges,
            ordering_gs,
            part_of_gs,
        })
    }

    pub fn is_token(&self, id: NodeID) -> anyhow::Result<bool> {
        if self.node_annos.has_value_for_item(&id, &TOKEN_KEY)? {
            // check if there is no outgoing edge in any of the coverage components
            let has_outgoing = self.has_outgoing_coverage_edges(id)?;
            Ok(!has_outgoing)
        } else {
            Ok(false)
        }
    }

    pub fn is_segmentation_token(&self, id: NodeID, seg: &str) -> anyhow::Result<bool> {
        if self.node_annos.has_value_for_item(&id, &TOKEN_KEY)? {
            let mut part_of_seg_component = false;
            let mut ordering_component_is_empty = true;
            if let Some(gs_ordering) = self.ordering_gs.get(seg) {
                part_of_seg_component =
                    gs_ordering.has_outgoing_edges(id)? || gs_ordering.has_ingoing_edges(id)?;
                ordering_component_is_empty = gs_ordering.source_nodes().next().is_none();
            }
            let has_seg_label = !self
                .node_annos
                .get_all_keys_for_item(&id, None, Some(seg))?
                .is_empty();
            return Ok(part_of_seg_component || (has_seg_label && ordering_component_is_empty));
        }
        Ok(false)
    }

    /// Returns the name of the segmentation component if the given node is a segmentation node.
    pub fn get_segmentation_name(&self, id: NodeID) -> anyhow::Result<Option<String>> {
        if self.node_annos.has_value_for_item(&id, &TOKEN_KEY)? {
            for (seg, gs_ordering) in self.ordering_gs.iter() {
                if !seg.is_empty()
                    && (gs_ordering.has_outgoing_edges(id)? || gs_ordering.has_ingoing_edges(id)?)
                {
                    return Ok(Some(seg.to_string()));
                }
            }
            // If the node is not part of any known ordering components, it
            // might be a single node not connected to another one.
            // Still, it needs to cover a base token to be a valid segmentation token.
            if self.has_outgoing_coverage_edges(id)? {
                let anno_keys: BTreeSet<AnnoKey> = self
                    .node_annos
                    .get_all_keys_for_item(&id, None, None)?
                    .into_iter()
                    .map(|k| k.as_ref().clone())
                    .collect();
                let potential_seg_names: Vec<_> = anno_keys
                    .iter()
                    .filter(|key| key.ns != ANNIS_NS)
                    .map(|key| key.name.clone())
                    .collect();
                // If there is only one key, directly return it.
                if potential_seg_names.len() == 1 {
                    return Ok(Some(potential_seg_names[0].to_string()));
                }
                // If there is more than one annotation for the node, it is ambigous which of these is the segmentation node.
                // There are different strategies that identify segmentation labels and wir try these in order.

                // Check if namespace matches the segmentation name.
                for seg in potential_seg_names.iter() {
                    let key_with_same_namespace = AnnoKey {
                        ns: seg.clone(),
                        name: seg.clone(),
                    };
                    if anno_keys.contains(&key_with_same_namespace) {
                        return Ok(Some(seg.to_string()));
                    }
                }
                // Check if there is a key with the segmentation name and the "annis" namespace
                for seg in potential_seg_names.iter() {
                    let key_with_annis_namespace = AnnoKey {
                        ns: ANNIS_NS.into(),
                        name: seg.clone(),
                    };
                    if anno_keys.contains(&key_with_annis_namespace) {
                        return Ok(Some(seg.to_string()));
                    }
                }
                // Check if there is a key with the segmentation name and the "default_ns" namespace
                for seg in potential_seg_names.iter() {
                    let key_with_default_namespace = AnnoKey {
                        ns: DEFAULT_NS.into(),
                        name: seg.clone(),
                    };
                    if anno_keys.contains(&key_with_default_namespace) {
                        return Ok(Some(seg.to_string()));
                    }
                }
                // Check if there is a key with the segmentation name and an empty namespace
                for seg in potential_seg_names.iter() {
                    let key_with_empty_namespace = AnnoKey {
                        ns: "".into(),
                        name: seg.clone(),
                    };
                    if anno_keys.contains(&key_with_empty_namespace) {
                        return Ok(Some(seg.to_string()));
                    }
                }
                // Do not return any value if this is still ambiguous
            }
        }
        Ok(None)
    }

    pub fn has_outgoing_coverage_edges(&self, id: NodeID) -> anyhow::Result<bool> {
        for c in self.cov_edges.iter() {
            if c.has_outgoing_edges(id)? {
                return Ok(true);
            }
        }
        Ok(false)
    }

    pub fn get_ordered_token(
        &self,
        parent_name: &str,
        segmentation: Option<&str>,
    ) -> Result<Vec<NodeID>> {
        let parent_id = self.node_annos.get_node_id_from_name(parent_name)?;
        let segmentation = segmentation.unwrap_or("");
        let ordering_gs = &self
            .ordering_gs
            .get(segmentation)
            .ok_or_else(|| anyhow!("Missing ordering component for segmentation {segmentation}"))?;

        // Find all token roots
        let mut roots: HashSet<_> = HashSet::new();
        for n in ordering_gs.source_nodes() {
            let n = n?;
            if !ordering_gs.has_ingoing_edges(n)? {
                // Filter the roots by checking the parent node in the corpus structure
                if let Some(parent_id) = parent_id {
                    if self
                        .part_of_gs
                        .is_connected(n, parent_id, 1, std::ops::Bound::Unbounded)?
                    {
                        roots.insert(n);
                    }
                } else {
                    roots.insert(n);
                }
            }
        }

        if roots.is_empty() {
            if let Some(parent_id) = parent_id {
                // Get all nodes that are part of this parent and check for the
                // annis:tok label and no outgoing coverage edges
                for connected in
                    self.part_of_gs
                        .find_connected_inverse(parent_id, 1, std::ops::Bound::Unbounded)
                {
                    let connected = connected?;
                    if self.is_token(connected)? {
                        roots.insert(connected);
                    }
                }
            } else {
                // Find all token that have the "annis:tok" label
                for n in self.node_annos.exact_anno_search(
                    Some(ANNIS_NS),
                    "tok",
                    graphannis_core::annostorage::ValueSearch::Any,
                ) {
                    let n = n?.node;
                    if !self.has_outgoing_coverage_edges(n)? {
                        roots.insert(n);
                    }
                }
            }
        }

        // Follow the ordering edges from the roots to reconstruct the token in their correct order
        let mut result = Vec::default();
        for r in roots {
            let mut token = Some(r);
            while let Some(current_token) = token {
                result.push(current_token);
                // Get next token
                if let Some(next_token) = ordering_gs.get_outgoing_edges(current_token).next() {
                    let next_token = next_token?;
                    token = Some(next_token);
                } else {
                    token = None;
                }
            }
        }

        Ok(result)
    }

    #[cfg(test)]
    pub fn spanned_text(&self, token_ids: &[NodeID]) -> Result<String> {
        use graphannis_core::errors::GraphAnnisCoreError;
        use itertools::Itertools;

        let anno_values: std::result::Result<Vec<_>, GraphAnnisCoreError> = token_ids
            .iter()
            .map(|t| self.node_annos.get_value_for_item(t, &TOKEN_KEY))
            .collect();
        // TODO: support whitespace after/before annotations
        let anno_values = anno_values?.into_iter().flatten().collect_vec();
        let result = anno_values.join(" ");
        Ok(result)
    }

    /// Find all token covered by the given node and sort the result according to the token order.
    pub fn covered_token(&self, node_id: NodeID) -> Result<Vec<NodeID>> {
        let mut result = Vec::default();
        let coverage = UnionEdgeContainer::new(
            self.cov_edges
                .iter()
                .map(|gs| gs.as_edgecontainer())
                .collect_vec(),
        );
        let it = CycleSafeDFS::new(&coverage, node_id, 1, usize::MAX);
        for step in it {
            let step = step?;
            if self.is_token(step.node)? {
                result.push(step.node);
            }
        }

        // Sort token by their order
        self.sort_token(&mut result, None)?;
        Ok(result)
    }

    pub fn get_ordering_gs(&self, segmentation: Option<&str>) -> Option<Arc<dyn GraphStorage>> {
        self.ordering_gs
            .get(segmentation.unwrap_or_default())
            .cloned()
    }

    pub fn get_coverage_gs(&self) -> &[Arc<dyn GraphStorage>] {
        &self.cov_edges
    }

    pub fn sort_token(&self, token_ids: &mut [NodeID], segmentation: Option<&str>) -> Result<()> {
        if let Some(gs) = self.ordering_gs.get(segmentation.unwrap_or_default()) {
            token_ids.sort_by(|a, b| {
                if a == b {
                    Ordering::Equal
                } else if let Ok(connected) = gs.is_connected(*a, *b, 1, std::ops::Bound::Unbounded)
                {
                    if connected {
                        Ordering::Less
                    } else {
                        Ordering::Greater
                    }
                } else {
                    Ordering::Less
                }
            });
        }
        Ok(())
    }

    /// Gets the token that comes before the given `node_id`. If a
    /// `segmentation` is given, this will not be a base token but a
    /// segmentation node that comes directly before the given node.
    pub fn get_token_before(
        &self,
        node_id: NodeID,
        segmentation: Option<&str>,
    ) -> Result<Option<NodeID>> {
        // Get all sorted covered token for this node
        let covered_token = if self.is_token(node_id)? {
            vec![node_id]
        } else {
            self.covered_token(node_id)?
        };

        // Find the token node before the left-most covered token
        if let Some(first_covered_token) = covered_token.first() {
            let gs_tok = self
                .ordering_gs
                .get("")
                .context("Missing base token graph storage component")?;
            let dfs = CycleSafeDFS::new_inverse(
                gs_tok.as_edgecontainer(),
                *first_covered_token,
                1,
                usize::MAX,
            );
            for step in dfs {
                let step = step?;
                let token_before = step.node;

                if let Some(segmentation) = segmentation {
                    // If a segmentation node is requested as result, find the one covering this token
                    let mut segmentation_nodes = Vec::new();
                    for gs_cov in &self.cov_edges {
                        for n in gs_cov.get_ingoing_edges(token_before) {
                            let n = n?;
                            if self.is_segmentation_token(n, segmentation)? {
                                segmentation_nodes.push(n);
                            }
                        }
                    }

                    if !segmentation_nodes.is_empty() {
                        self.sort_token(&mut segmentation_nodes, Some(segmentation))?;
                        return Ok(segmentation_nodes.last().copied());
                    }
                } else {
                    return Ok(Some(token_before));
                }
            }

            Ok(None)
        } else {
            Ok(None)
        }
    }

    /// Gets the token that comes after the given `node_id`. If a `segmentation`
    /// is given, this will not be a base token but a segmentation node that
    /// comes directly after the given node.
    pub fn get_token_after(
        &self,
        node_id: NodeID,
        segmentation: Option<&str>,
    ) -> Result<Option<NodeID>> {
        // Get all sorted covered token for this node
        let covered_token = if self.is_token(node_id)? {
            vec![node_id]
        } else {
            self.covered_token(node_id)?
        };

        // Find the token node after the right-most covered token
        if let Some(last_covered_token) = covered_token.last() {
            let gs_tok = self
                .ordering_gs
                .get("")
                .context("Missing base token graph storage component")?;

            let dfs = CycleSafeDFS::new(
                gs_tok.as_edgecontainer(),
                *last_covered_token,
                1,
                usize::MAX,
            );
            for step in dfs {
                let step = step?;
                let token_after = step.node;

                if let Some(segmentation) = segmentation {
                    // If a segmentation node is requested as result, find the one covering this token
                    let mut segmentation_nodes = Vec::new();
                    for gs_cov in &self.cov_edges {
                        for n in gs_cov.get_ingoing_edges(token_after) {
                            let n = n?;
                            if self.is_segmentation_token(n, segmentation)? {
                                segmentation_nodes.push(n);
                            }
                        }
                    }
                    if !segmentation_nodes.is_empty() {
                        self.sort_token(&mut segmentation_nodes, Some(segmentation))?;
                        return Ok(segmentation_nodes.first().copied());
                    }
                } else {
                    return Ok(Some(token_after));
                }
            }
        }
        Ok(None)
    }

    /// Creates a sequence of start/end pairs of token nodes that are adjacent to each other.
    pub(crate) fn continuous_segmentation_spans(
        &self,
        nodes: &[NodeID],
        segmentation: Option<&str>,
    ) -> anyhow::Result<Vec<(NodeID, NodeID)>> {
        // Filter the and sort the given node IDs, to include only the ones that belong to gigven segmentation;
        let mut token_ids = Vec::with_capacity(nodes.len());
        for n in nodes {
            if let Some(seg) = segmentation {
                if self.is_segmentation_token(*n, seg)? {
                    token_ids.push(*n);
                }
            } else if self.is_token(*n)? {
                token_ids.push(*n);
            }
        }
        self.sort_token(&mut token_ids, segmentation)?;

        // Create an inital sequence of spans, where all elements are a span item
        let segmentation = segmentation.unwrap_or("");
        let ordering_gs = &self.ordering_gs.get(segmentation).ok_or_else(|| {
            anyhow!("Missing ordering component for segmentation '{segmentation}'")
        })?;
        let mut spans: Vec<_> = token_ids.into_iter().map(|n| (n, n)).collect();
        let mut idx = 0;
        while let Some(current_span) = spans.get(idx).copied()
            && let Some(next_span) = spans.get(idx + 1).copied()
        {
            // check if the current and next span are adjacent and should be joined
            let outgoing: Result<Vec<_>, _> =
                ordering_gs.get_outgoing_edges(current_span.1).collect();
            if outgoing?.into_iter().any(|o| o == next_span.0) {
                // Merge boths spans
                spans.remove(idx);
                spans[idx].0 = current_span.0;
                spans[idx].1 = next_span.1;
            } else {
                idx += 1;
            }
        }
        Ok(spans)
    }
}

#[cfg(test)]
mod tests;