annatto 0.50.1

Converts linguistic data formats based on the graphANNIS data model as intermediate representation and can apply consistency tests.
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
use std::{
    collections::{BTreeMap, BTreeSet, btree_map::Entry},
    fs,
    path::Path,
};

use facet::Facet;
use graphannis::{
    graph::{AnnoKey, Annotation},
    model::{AnnotationComponent, AnnotationComponentType},
    update::{GraphUpdate, UpdateEvent},
};
use graphannis_core::graph::ANNIS_NS;
use itertools::Itertools;
use pest::{Parser, iterators::Pair};
use pest_derive::Parser;
use serde::{Deserialize, Serialize};

use crate::{
    StepID,
    error::{AnnattoError, Result},
    importer::GenericImportConfiguration,
    progress::ProgressReporter,
    util::graphupdate::import_corpus_graph_from_files,
};

use super::Importer;

/// Import annotations provided in the fieldlinguist's toolbox text format.
#[derive(Facet, Deserialize, Serialize, Clone, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ImportToolBox {
    /// This attribute sets the annotation layer, that other annotations will point to.
    /// This needs to be set to avoid an invalid model.
    target: String,
    /// The annotation names named here are considered single-valued per line. Space values
    /// are not considered delimiters, but part of the annotation value. Such annotations
    /// rely on the existence of the target nodes, i. e. annotation lines without any other
    /// non-spanning annotation in the block will be dropped.
    #[serde(default)]
    span: BTreeSet<String>,
    /// Null values are represented as `-` in toolbox. If you want those to remain explicit
    /// annotations, set `explicit_null = true`.
    #[serde(default)]
    explicit_null: bool,
}

const FILE_EXTENSIONS: [&str; 1] = ["txt"];

impl Importer for ImportToolBox {
    fn import_corpus(
        &self,
        input_path: &std::path::Path,
        step_id: crate::StepID,
        config: GenericImportConfiguration,
        tx: Option<crate::workflow::StatusSender>,
    ) -> std::result::Result<graphannis::update::GraphUpdate, Box<dyn std::error::Error>> {
        let mut update = GraphUpdate::default();
        let paths_and_node_names =
            import_corpus_graph_from_files(&mut update, input_path, &config)?;
        let progress = ProgressReporter::new(tx, step_id.clone(), paths_and_node_names.len())?;
        for (path, doc_node_name) in paths_and_node_names {
            self.map_document(path.as_path(), &doc_node_name, &mut update, &step_id)?;
            progress.worked(1)?;
        }
        Ok(update)
    }

    fn default_file_extensions(&self) -> &[&str] {
        &FILE_EXTENSIONS
    }
}

impl ImportToolBox {
    fn map_document(
        &self,
        path: &Path,
        doc_node_name: &str,
        update: &mut GraphUpdate,
        step_id: &StepID,
    ) -> Result<()> {
        let data = fs::read_to_string(path)?;
        let mut pairs =
            ToolboxParser::parse(Rule::data, &data).map_err(|e| AnnattoError::Import {
                reason: format!("Failed to parse: {e}"),
                importer: step_id.module_name.clone(),
                path: path.to_path_buf(),
            })?;
        let next_pair = pairs.next();
        let mut start_id = 1;
        let mut ordering = BTreeMap::default();
        if let Some(pair) = next_pair
            && pair.as_rule() == Rule::data
        {
            for annotation_block in pair.into_inner() {
                if annotation_block.as_rule() == Rule::block {
                    start_id = self.map_annotation_block(
                        update,
                        doc_node_name,
                        annotation_block,
                        start_id,
                        &mut ordering,
                    )?;
                }
            }
        }
        for (ordering_name, node_specs) in ordering {
            let sorted_nodes = node_specs
                .into_iter()
                .sorted_by(|a, b| a.0.cmp(&b.0))
                .map(|(_, name)| name);
            for (source_node, target_node) in sorted_nodes.tuple_windows() {
                update.add_event(UpdateEvent::AddEdge {
                    source_node,
                    target_node,
                    layer: ANNIS_NS.to_string(),
                    component_type: AnnotationComponentType::Ordering.to_string(),
                    component_name: ordering_name.to_string(),
                })?;
            }
        }
        Ok(())
    }

    fn map_annotation_block(
        &self,
        update: &mut GraphUpdate,
        doc_node_name: &str,
        data: Pair<Rule>,
        start_id: usize,
        ordering: &mut BTreeMap<String, BTreeSet<(NodeSpec, String)>>,
    ) -> Result<usize> {
        let mut end_ids = BTreeSet::default();
        end_ids.insert(start_id); // to guarantee an option of a maximal value
        let lines = data.into_inner();
        let mut block_annos = Vec::with_capacity(lines.len());
        for line in lines {
            if line.as_rule() == Rule::line {
                let (end_id, block_anno, comment) =
                    self.map_annotation_line(update, doc_node_name, line, start_id, ordering)?;
                end_ids.insert(end_id);
                if let Some(anno) = block_anno {
                    block_annos.push(anno);
                }
                if let Some(comment_anno) = comment {
                    block_annos.push(comment_anno);
                }
            }
        }
        if let Some(max_end) = end_ids.into_iter().max() {
            for (i, anno) in block_annos.into_iter().enumerate() {
                let tokens = (start_id..max_end).map(NodeSpec::Terminal).collect_vec();
                self.annotate(
                    update,
                    doc_node_name,
                    (start_id, i as u8, "span".to_string()),
                    tokens,
                    (&anno.key.ns, &anno.key.name, anno.val.trim()),
                    ordering,
                )?;
            }
            Ok(max_end)
        } else {
            Err(AnnattoError::Import {
                reason: "Could not determine end of span.".to_string(),
                importer: "toolbox".to_string(),
                path: Path::new(doc_node_name).to_path_buf(),
            })
        }
    }

    fn map_annotation_line(
        &self,
        update: &mut GraphUpdate,
        doc_node_name: &str,
        data: Pair<Rule>,
        start_id: usize,
        ordering: &mut BTreeMap<String, BTreeSet<(NodeSpec, String)>>,
    ) -> Result<(usize, Option<Annotation>, Option<Annotation>)> {
        let mut layer_name = String::new();
        let mut end_id = start_id;
        let mut span_anno = None;
        let mut comment = None;
        for pair in data.into_inner() {
            match pair.as_rule() {
                Rule::entries => {
                    let (final_id, block_anno) = self.map_line_entries(
                        update,
                        doc_node_name,
                        pair,
                        &layer_name,
                        start_id,
                        ordering,
                    )?;
                    if let Some(anno_val) = block_anno {
                        span_anno = Some(Annotation {
                            key: AnnoKey {
                                ns: "".into(),
                                name: layer_name.to_string(),
                            },
                            val: anno_val,
                        });
                    }
                    end_id = final_id;
                }
                Rule::anno_field => {
                    layer_name.push_str(pair.as_str().trim());
                    if &layer_name == "ref" {
                        "".to_string();
                    }
                }
                Rule::proc_field => return Ok((end_id, None, None)), // TODO make configurable, for now internal markers ("\_...") are not processed
                Rule::comment => {
                    comment = Some(Annotation {
                        key: AnnoKey {
                            name: "inline_comment".to_string(),
                            ns: "toolbox".to_string(),
                        },
                        val: pair.as_str().to_string(),
                    });
                }
                _ => {}
            }
        }
        Ok((end_id, span_anno, comment)) // if this is ever executed, something went wrong
    }

    fn map_line_entries(
        &self,
        update: &mut GraphUpdate,
        doc_node_name: &str,
        data: Pair<Rule>,
        anno_name: &str,
        id: usize,
        ordering: &mut BTreeMap<String, BTreeSet<(NodeSpec, String)>>,
    ) -> Result<(usize, Option<String>)> {
        let inner = data.into_inner();
        // annotate nodes
        let mut timeline_id = id;
        let mut join_list = Vec::new();
        let build_joint = self.span.contains(anno_name);
        let use_tokens = self.target == anno_name;
        for entry_or_space in inner {
            match (entry_or_space.as_rule(), self.explicit_null) {
                (Rule::entry, _) | (Rule::null, true) => {
                    if build_joint {
                        join_list.push(entry_or_space.as_str());
                    } else {
                        let inner = if matches!(entry_or_space.as_rule(), Rule::null) {
                            Some(entry_or_space.clone())
                        } else {
                            entry_or_space.clone().into_inner().next()
                        };
                        let entry_node = if let Some(nxt) = inner {
                            nxt
                        } else {
                            continue;
                        };
                        match entry_node.as_rule() {
                            Rule::complex => {
                                let sub_entries = entry_node.into_inner();
                                for (sub_index, sub_entry) in sub_entries.enumerate() {
                                    match sub_entry.as_rule() {
                                        Rule::default | Rule::pro_clitic | Rule::en_clitic => {
                                            self.annotate(
                                                update,
                                                doc_node_name,
                                                (
                                                    timeline_id,
                                                    sub_index as u8,
                                                    anno_name.to_string(),
                                                ),
                                                vec![if use_tokens {
                                                    NodeSpec::Terminal(timeline_id)
                                                } else {
                                                    NodeSpec::NonTerminal((
                                                        timeline_id,
                                                        0,
                                                        self.target.to_string(),
                                                    ))
                                                }],
                                                ("", anno_name, sub_entry.as_str()),
                                                ordering,
                                            )?;
                                        }
                                        _ => continue,
                                    };
                                }
                                timeline_id += 1;
                            }
                            _ => {
                                // with_dashes OR default OR redub
                                let (node_index, target_index) = (
                                    (timeline_id, 0_u8, anno_name.to_string()),
                                    if use_tokens {
                                        NodeSpec::Terminal(timeline_id)
                                    } else {
                                        NodeSpec::NonTerminal((
                                            timeline_id,
                                            0_u8,
                                            self.target.to_string(),
                                        ))
                                    },
                                );
                                self.annotate(
                                    update,
                                    doc_node_name,
                                    node_index,
                                    vec![target_index],
                                    ("", anno_name, entry_or_space.as_str()),
                                    ordering,
                                )?;
                                timeline_id += 1;
                            }
                        };
                    }
                }
                (Rule::spaces, _) => {
                    if build_joint {
                        join_list.push(entry_or_space.as_str());
                    }
                }
                (Rule::null, false) => {
                    timeline_id += 1;
                }
                _ => {}
            }
        }
        let block_annotation = if build_joint && !join_list.is_empty() {
            Some(join_list.join(""))
        } else {
            None
        };
        Ok((timeline_id, block_annotation))
    }

    fn annotate(
        &self,
        update: &mut GraphUpdate,
        doc_node_name: &str,
        node_index: AnnotationNode,
        targets: Vec<NodeSpec>,
        anno: (&str, &str, &str),
        ordering: &mut BTreeMap<String, BTreeSet<(NodeSpec, String)>>,
    ) -> Result<()> {
        let node_name = format!(
            "{doc_node_name}#{}{}.{}",
            node_index.2, node_index.0, node_index.1
        );
        update.add_event(UpdateEvent::AddNode {
            node_name: node_name.to_string(),
            node_type: "node".to_string(),
        })?;
        update.add_event(UpdateEvent::AddNodeLabel {
            node_name: node_name.to_string(),
            anno_ns: anno.0.to_string(),
            anno_name: anno.1.to_string(),
            anno_value: anno.2.to_string(),
        })?;
        update.add_event(UpdateEvent::AddNodeLabel {
            node_name: node_name.to_string(),
            anno_ns: ANNIS_NS.to_string(),
            anno_name: "layer".to_string(),
            anno_value: "default_layer".to_string(),
        })?;
        let pointing_component =
            AnnotationComponent::new(AnnotationComponentType::Pointing, "".into(), anno.1.into());
        let coverage_component = AnnotationComponent::new(
            AnnotationComponentType::Coverage,
            ANNIS_NS.into(),
            "virtual".into(),
        );
        for target in targets {
            let (target_name, component) = match target {
                NodeSpec::NonTerminal((id, sub_id, prefix)) => (
                    format!("{doc_node_name}#{prefix}{id}.{sub_id}"),
                    &pointing_component,
                ),
                NodeSpec::Terminal(tok_i) => {
                    let target_name = format!("{doc_node_name}#{tok_i}");
                    match ordering.entry("".to_string()) {
                        Entry::Vacant(e) => {
                            update.add_event(UpdateEvent::AddNode {
                                node_name: target_name.to_string(),
                                node_type: "node".to_string(),
                            })?;
                            update.add_event(UpdateEvent::AddNodeLabel {
                                node_name: target_name.to_string(),
                                anno_ns: ANNIS_NS.to_string(),
                                anno_name: "tok".to_string(),
                                anno_value: " ".to_string(),
                            })?;
                            update.add_event(UpdateEvent::AddNodeLabel {
                                node_name: target_name.to_string(),
                                anno_ns: ANNIS_NS.to_string(),
                                anno_name: "layer".to_string(),
                                anno_value: "default_layer".to_string(),
                            })?;
                            update.add_event(UpdateEvent::AddEdge {
                                source_node: target_name.to_string(),
                                target_node: doc_node_name.to_string(),
                                layer: ANNIS_NS.to_string(),
                                component_type: AnnotationComponentType::PartOf.to_string(),
                                component_name: "".to_string(),
                            })?;
                            let mut v = BTreeSet::default();
                            v.insert((target, target_name.to_string()));
                            e.insert(v);
                        }
                        Entry::Occupied(mut e) => {
                            let k = (target, target_name.to_string());
                            let v = e.get_mut();
                            if !v.contains(&k) {
                                update.add_event(UpdateEvent::AddNode {
                                    node_name: target_name.to_string(),
                                    node_type: "node".to_string(),
                                })?;
                                update.add_event(UpdateEvent::AddNodeLabel {
                                    node_name: target_name.to_string(),
                                    anno_ns: ANNIS_NS.to_string(),
                                    anno_name: "tok".to_string(),
                                    anno_value: " ".to_string(),
                                })?;
                                update.add_event(UpdateEvent::AddNodeLabel {
                                    node_name: target_name.to_string(),
                                    anno_ns: ANNIS_NS.to_string(),
                                    anno_name: "layer".to_string(),
                                    anno_value: "default_layer".to_string(),
                                })?;
                                update.add_event(UpdateEvent::AddEdge {
                                    source_node: target_name.to_string(),
                                    target_node: doc_node_name.to_string(),
                                    layer: ANNIS_NS.to_string(),
                                    component_type: AnnotationComponentType::PartOf.to_string(),
                                    component_name: "".to_string(),
                                })?;
                                v.insert(k);
                            }
                        }
                    }
                    (target_name, &coverage_component)
                }
            };
            update.add_event(UpdateEvent::AddEdge {
                source_node: node_name.to_string(),
                target_node: target_name,
                layer: component.layer.to_string(),
                component_type: component.get_type().to_string(),
                component_name: component.name.to_string(),
            })?;
        }
        update.add_event(UpdateEvent::AddEdge {
            source_node: node_name.to_string(),
            target_node: doc_node_name.to_string(),
            layer: ANNIS_NS.to_string(),
            component_type: AnnotationComponentType::PartOf.to_string(),
            component_name: "".to_string(),
        })?;
        let ordered_node_spec =
            NodeSpec::NonTerminal((node_index.0, node_index.1, node_index.2.to_string()));
        match ordering.entry(anno.1.to_string()) {
            Entry::Vacant(e) => {
                let mut v = BTreeSet::default();
                v.insert((ordered_node_spec, node_name));
                e.insert(v);
            }
            Entry::Occupied(mut e) => {
                e.get_mut().insert((ordered_node_spec, node_name));
            }
        };
        Ok(())
    }
}

type AnnotationNode = (usize, u8, String);
type Tok = usize;

#[derive(PartialOrd, PartialEq, Eq, Ord)]
enum NodeSpec {
    NonTerminal(AnnotationNode),
    Terminal(Tok),
}

/// This implements the Pest parser for the given grammar.
#[derive(Parser)]
#[grammar = "importer/toolbox/toolbox.pest"]
struct ToolboxParser;

#[cfg(test)]
mod tests {
    use std::{fs, path::Path};

    use insta::assert_snapshot;

    use crate::importer::toolbox::ImportToolBox;

    #[test]
    fn serialize_custom() {
        let module = ImportToolBox {
            span: vec!["sentence".to_string(), "clause".to_string()]
                .into_iter()
                .collect(),
            target: "tx".to_string(),
            explicit_null: false,
        };
        let serialization = toml::to_string(&module);
        assert!(
            serialization.is_ok(),
            "Serialization failed: {:?}",
            serialization.err()
        );
        assert_snapshot!(serialization.unwrap());
    }

    #[test]
    fn core_functionality() {
        let ts = fs::read_to_string("tests/data/import/toolbox/build.toml");
        assert!(ts.is_ok(), "Could not read workflow: {:?}", ts.err());
        let toml_str = ts.unwrap();
        let imp: Result<ImportToolBox, _> = toml::from_str(toml_str.as_str());
        assert!(imp.is_ok(), "Error occurred: {:?}", imp.err());
        let importer = imp.unwrap();
        let graphml_is = crate::test_util::import_as_graphml_string(
            importer,
            Path::new("tests/data/import/toolbox/"),
            None,
        );
        assert!(
            graphml_is.is_ok(),
            "Failed to import test file: {:?}",
            graphml_is.err()
        );
        assert_snapshot!(graphml_is.unwrap());
    }

    #[test]
    fn explicit_null() {
        let ts = fs::read_to_string("tests/data/import/toolbox/build-explicit-null.toml");
        assert!(ts.is_ok(), "Could not read workflow: {:?}", ts.err());
        let toml_str = ts.unwrap();
        let imp: Result<ImportToolBox, _> = toml::from_str(toml_str.as_str());
        assert!(imp.is_ok(), "Error occurred: {:?}", imp.err());
        let importer = imp.unwrap();
        let graphml_is = crate::test_util::import_as_graphml_string(
            importer,
            Path::new("tests/data/import/toolbox/"),
            None,
        );
        assert!(
            graphml_is.is_ok(),
            "Failed to import test file: {:?}",
            graphml_is.err()
        );
        assert_snapshot!(graphml_is.unwrap());
    }
}