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
use std::{collections::HashMap, io::Read, path::Path};

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

use super::Importer;
use encoding_rs::Encoding;
use encoding_rs_io::DecodeReaderBytesBuilder;
use facet::Facet;
use graphannis::{
    graph::AnnoKey,
    model::AnnotationComponentType,
    update::{GraphUpdate, UpdateEvent},
};
use graphannis_core::graph::{ANNIS_NS, DEFAULT_NS};
use itertools::Itertools;
use pest::{Parser, iterators::Pairs};
use pest_derive::Parser;
use serde::Serialize;
use serde_derive::Deserialize;

const FILE_ENDINGS: [&str; 5] = ["treetagger", "tab", "tt", "txt", "xml"];

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

struct MapperParams {
    column_names: Vec<AnnoKey>,
    attribute_decoding: AttributeDecoding,
}

#[derive(Debug)]
struct TagStackEntry {
    anno_name: String,
    covered_token: Vec<String>,
    attributes: HashMap<String, String>,
    was_first_line: bool,
}

struct DocumentMapper<'a> {
    doc_path: String,
    text_node_name: String,
    last_token_id: Option<String>,
    number_of_token: usize,
    number_of_spans: usize,
    tag_stack: Vec<TagStackEntry>,
    params: &'a MapperParams,
}

impl<'a> DocumentMapper<'a> {
    fn map(&mut self, u: &mut GraphUpdate, mut tt: Pairs<'a, Rule>) -> anyhow::Result<()> {
        // Add a subcorpus like node for the text
        u.add_event(UpdateEvent::AddNode {
            node_name: self.text_node_name.clone(),
            node_type: "datasource".to_string(),
        })?;
        u.add_event(UpdateEvent::AddEdge {
            source_node: self.text_node_name.clone(),
            target_node: self.doc_path.clone(),
            layer: ANNIS_NS.to_string(),
            component_type: "PartOf".to_string(),
            component_name: "".to_string(),
        })?;

        if let Some(tt) = tt.next()
            && tt.as_rule() == Rule::treetagger
        {
            let tt = tt.into_inner();
            self.map_tt_rule(u, tt)?;
        }
        Ok(())
    }

    fn map_tt_rule(&mut self, u: &mut GraphUpdate, mut tt: Pairs<'a, Rule>) -> anyhow::Result<()> {
        let mut was_first_line = true;
        while let Some(line) = tt.next() {
            match line.as_rule() {
                Rule::token_line => {
                    let token_line = line.into_inner();
                    self.consume_token_line(u, token_line)?;
                }
                Rule::start_tag => {
                    let start_tag = line.into_inner();
                    self.consume_start_tag(start_tag, was_first_line)?;
                }
                Rule::end_tag => {
                    let end_tag = line.into_inner();
                    self.consume_end_tag(
                        u,
                        end_tag,
                        tt.peek().is_some_and(|next| next.as_rule() == Rule::EOI),
                    )?;
                }
                _ => {}
            };
            was_first_line = false;
        }
        Ok(())
    }

    fn consume_token_line(
        &mut self,
        u: &mut GraphUpdate,
        mut token_line: Pairs<'a, Rule>,
    ) -> anyhow::Result<()> {
        // Create a token node for this column
        let id = self.number_of_token + 1;
        let tok_id = format!("{}#t{id}", self.doc_path);
        u.add_event(UpdateEvent::AddNode {
            node_name: tok_id.clone(),
            node_type: "node".to_string(),
        })?;

        u.add_event(UpdateEvent::AddNodeLabel {
            node_name: tok_id.clone(),
            anno_ns: ANNIS_NS.to_string(),
            anno_name: "layer".to_string(),
            anno_value: "default_layer".to_string(),
        })?;
        u.add_event(UpdateEvent::AddEdge {
            source_node: tok_id.clone(),
            target_node: self.text_node_name.clone(),
            layer: ANNIS_NS.to_string(),
            component_type: AnnotationComponentType::PartOf.to_string(),
            component_name: "".to_string(),
        })?;

        // Remember this token as covered for all spans on the stack
        for e in self.tag_stack.iter_mut() {
            e.covered_token.push(tok_id.clone());
        }

        if let Some(last_token_id) = &self.last_token_id {
            u.add_event(UpdateEvent::AddNodeLabel {
                node_name: tok_id.clone(),
                anno_ns: ANNIS_NS.to_string(),
                anno_name: "tok-whitespace-before".to_string(),
                anno_value: " ".to_string(),
            })?;
            u.add_event(UpdateEvent::AddEdge {
                source_node: last_token_id.clone(),
                target_node: tok_id.clone(),
                layer: ANNIS_NS.to_string(),
                component_type: AnnotationComponentType::Ordering.to_string(),
                component_name: "".to_string(),
            })?;
        }
        self.number_of_token += 1;
        self.last_token_id = Some(tok_id.clone());

        for column_key in &self.params.column_names {
            if let Some(column_value) = token_line.next()
                && column_value.as_rule() == Rule::column_value
            {
                if column_key.name.as_str() == "tok"
                    && (column_key.ns.is_empty() || column_key.ns.as_str() == ANNIS_NS)
                {
                    u.add_event(UpdateEvent::AddNodeLabel {
                        node_name: tok_id.to_string(),
                        anno_ns: ANNIS_NS.to_string(),
                        anno_name: "tok".to_string(),
                        anno_value: column_value.as_str().to_string(),
                    })?;
                } else {
                    let ns = if column_key.ns.is_empty() {
                        DEFAULT_NS
                    } else {
                        column_key.ns.as_str()
                    };
                    u.add_event(UpdateEvent::AddNodeLabel {
                        node_name: tok_id.to_string(),
                        anno_ns: ns.to_string(),
                        anno_name: column_key.name.to_string(),
                        anno_value: column_value.as_str().to_string(),
                    })?;
                }
            }
        }

        Ok(())
    }

    fn consume_start_tag(
        &mut self,
        mut start_tag: Pairs<'a, Rule>,
        was_first_line: bool,
    ) -> anyhow::Result<()> {
        if let Some(tag_name) = start_tag.next()
            && tag_name.as_rule() == Rule::tag_name
        {
            let attributes = self.consume_tag_attribute(start_tag)?;
            self.tag_stack.push(TagStackEntry {
                anno_name: tag_name.as_str().to_string(),
                covered_token: Vec::new(),
                was_first_line,
                attributes,
            });
        }

        Ok(())
    }

    fn consume_tag_attribute(
        &mut self,
        mut start_tag: Pairs<'a, Rule>,
    ) -> anyhow::Result<HashMap<String, String>> {
        let mut result = HashMap::new();
        // All tag attributes must be tuples of attribute IDs and string values
        while let (Some(attr_id), Some(string_value)) = (start_tag.next(), start_tag.next()) {
            if attr_id.as_rule() == Rule::attr_id && string_value.as_rule() == Rule::string_value {
                let unescaped_string = match self.params.attribute_decoding {
                    AttributeDecoding::Entities => {
                        quick_xml::escape::unescape(string_value.as_str())?
                    }
                    AttributeDecoding::None => string_value.as_str().into(),
                };

                result.insert(attr_id.as_str().to_string(), unescaped_string.to_string());
            }
        }
        Ok(result)
    }

    fn consume_end_tag(
        &mut self,
        u: &mut GraphUpdate,
        mut end_tag: Pairs<'a, Rule>,
        is_last_line: bool,
    ) -> anyhow::Result<()> {
        // Get the tag name and the nearest matching tag from stack
        if let Some(tag_name) = end_tag.next()
            && tag_name.as_rule() == Rule::tag_name
        {
            let tag_name = tag_name.as_str();

            if let Some(idx) = self.tag_stack.iter().position(|t| t.anno_name == tag_name) {
                let entry = self.tag_stack.remove(idx);

                let is_meta = entry.was_first_line && is_last_line;

                let node_id = if is_meta {
                    // This is a meta annotation for the whole document
                    self.doc_path.clone()
                } else {
                    // Add a node update for the new span
                    self.number_of_spans += 1;
                    let span_id = format!("{}#span{}", self.doc_path, self.number_of_spans);
                    u.add_event(UpdateEvent::AddNode {
                        node_name: span_id.clone(),
                        node_type: "node".into(),
                    })?;
                    // TODO: support namespaces in span annotation name
                    u.add_event(UpdateEvent::AddNodeLabel {
                        node_name: span_id.clone(),
                        anno_ns: "".into(),
                        anno_name: tag_name.into(),
                        anno_value: tag_name.into(),
                    })?;

                    u.add_event(UpdateEvent::AddNodeLabel {
                        node_name: span_id.clone(),
                        anno_ns: ANNIS_NS.to_string(),
                        anno_name: "layer".to_string(),
                        anno_value: "default_layer".to_string(),
                    })?;
                    // Add coverage edges for all covered token
                    for t in entry.covered_token {
                        u.add_event(UpdateEvent::AddEdge {
                            source_node: span_id.clone(),
                            target_node: t,
                            layer: ANNIS_NS.into(),
                            component_type: "Coverage".into(),
                            component_name: "".into(),
                        })?;
                    }
                    span_id
                };

                // Add all attributes as node annotations
                for (anno_name, anno_value) in entry.attributes {
                    // TODO: allow to configure not to prepend the tag name to the annotation

                    let anno_name = if is_meta {
                        anno_name
                    } else {
                        format!("{tag_name}_{anno_name}")
                    };
                    // TODO: support namespaces as annotation names
                    u.add_event(UpdateEvent::AddNodeLabel {
                        node_name: node_id.clone(),
                        anno_ns: "".into(),
                        anno_name,
                        anno_value,
                    })?;
                }
            }
        }
        Ok(())
    }
}

/// Importer for the file format used by the TreeTagger.
///
/// Example:
/// ```toml
/// [[import]]
/// format = "treetagger"
/// path = "..."
///
/// [import.config]
/// column_names = ["tok", "custom_pos", "custom_lemma"]
/// ```
///
/// This imports the second and third column of your treetagger files
/// as `custom_pos` and `custom_lemma`.
///
/// You can use namespaces in some or all of the columns. The default
/// namespace for the first column if "tok" is provided is "annis".
/// For the following columns the namespace defaults to "default_ns" if
/// nothing is provided. If the first column is not "tok" or "annis::tok", "default_ns"
/// will also be the namespace if none is specified.
///
/// Example:
/// ```toml
/// [import.config]
/// column_names = ["tok", "norm::custom_pos", "norm::custom_lemma"]
/// ```
#[derive(Facet, Deserialize, Serialize, Clone, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ImportTreeTagger {
    /// Provide annotation names for the columns of the files. If you want the first column to be `annis::tok`,
    /// you can use "tok" or "annis::tok". For all following columns, if you do not provide a namespace, "default_ns"
    /// will be used automatically.
    #[serde(
        default = "default_column_names",
        with = "crate::estarde::anno_key::in_sequence"
    )]
    column_names: Vec<AnnoKey>,
    /// The encoding to use when for the input files. Defaults to UTF-8.
    #[serde(default)]
    file_encoding: Option<String>,
    /// Whether or not attributes should be decoded as entities (true, default) or read as bare string (false).
    #[serde(default = "default_attribute_decoding")]
    attribute_decoding: AttributeDecoding,
}

fn default_attribute_decoding() -> AttributeDecoding {
    AttributeDecoding::Entities
}

fn default_column_names() -> Vec<AnnoKey> {
    vec![
        AnnoKey {
            ns: ANNIS_NS.into(),
            name: "tok".into(),
        },
        AnnoKey {
            name: "pos".into(),
            ns: DEFAULT_NS.into(),
        },
        AnnoKey {
            name: "lemma".into(),
            ns: DEFAULT_NS.into(),
        },
    ]
}

impl Default for ImportTreeTagger {
    fn default() -> Self {
        Self {
            column_names: default_column_names(),
            file_encoding: Default::default(),
            attribute_decoding: default_attribute_decoding(),
        }
    }
}

#[derive(Facet, Default, Deserialize, Debug, Clone, Copy, Serialize, PartialEq)]
#[serde(rename_all = "lowercase")]
#[repr(u8)]
pub enum AttributeDecoding {
    #[default]
    Entities,
    None,
}

impl Importer for ImportTreeTagger {
    fn import_corpus(
        &self,
        input_path: &Path,
        step_id: StepID,
        config: GenericImportConfiguration,
        tx: Option<crate::workflow::StatusSender>,
    ) -> std::result::Result<GraphUpdate, Box<dyn std::error::Error>> {
        let mut u = GraphUpdate::default();

        let documents = import_corpus_graph_from_files(&mut u, input_path, &config)?;

        let reporter = ProgressReporter::new(tx, step_id, documents.len())?;

        let params = MapperParams {
            column_names: self.column_names.iter().cloned().collect_vec(),
            attribute_decoding: self.attribute_decoding,
        };

        let decoder_builder = if let Some(encoding) = &self.file_encoding {
            DecodeReaderBytesBuilder::new()
                .encoding(Encoding::for_label(encoding.as_bytes()))
                .clone()
        } else {
            DecodeReaderBytesBuilder::new()
        };

        for (file_path, doc_path) in documents {
            reporter.info(format!("Processing {}", &file_path.to_string_lossy()))?;

            let f = std::fs::File::open(&file_path)?;
            let mut file_content = String::new();

            decoder_builder
                .build(&f)
                .read_to_string(&mut file_content)?;

            let tt: Pairs<Rule> = TreeTaggerParser::parse(Rule::treetagger, &file_content)?;

            let text_node_name = format!("{}#text", &doc_path);

            let mut doc_mapper = DocumentMapper {
                doc_path,
                text_node_name,
                params: &params,
                last_token_id: None,
                number_of_token: 0,
                number_of_spans: 0,
                tag_stack: Vec::new(),
            };

            doc_mapper.map(&mut u, tt)?;
            reporter.worked(1)?;
        }
        Ok(u)
    }

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

#[cfg(test)]
mod tests;