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
use std::collections::BTreeSet;
use anyhow::{Ok, anyhow};
use graphannis::{
model::AnnotationComponentType,
update::{GraphUpdate, UpdateEvent},
};
use graphannis_core::graph::ANNIS_NS;
use itertools::Itertools;
use super::{
SaltObject, SaltType, get_element_id, get_features, get_meta_annotations, resolve_element,
};
pub(super) struct SaltCorpusStructureMapper {}
impl SaltCorpusStructureMapper {
pub(super) fn new() -> SaltCorpusStructureMapper {
SaltCorpusStructureMapper {}
}
pub(super) fn map_corpus_structure(
&self,
input: &str,
updates: &mut GraphUpdate,
) -> anyhow::Result<BTreeSet<String>> {
let doc = roxmltree::Document::parse(input)?;
let root = doc.root_element();
if root.tag_name().name() != "SaltProject" {
return Err(anyhow!(
"SaltXML project file must start with <SaltProject> tag"
));
}
let mut documents = BTreeSet::new();
// Iterate over all corpus graphs
for cg in root
.children()
.filter(|t| t.tag_name().name() == "sCorpusGraphs")
{
// Get all nodes
let nodes = cg
.children()
.filter(|t| t.tag_name().name() == "nodes")
.collect_vec();
for node in nodes.iter() {
let salt_type = SaltType::from_node(node);
match salt_type {
SaltType::Corpus | SaltType::Document => {
// Get the element ID from the label
let node_name = get_element_id(node)
.ok_or_else(|| anyhow!("Missing element ID for corpus graph node"))?;
// Create the element with the collected properties
updates.add_event(UpdateEvent::AddNode {
node_name: node_name.to_string(),
node_type: "corpus".into(),
})?;
// Add the document ID to the result
if SaltType::Document == salt_type {
documents.insert(node_name.to_string());
}
// Add features as annotations
for feature_node in get_features(node) {
let annos_ns = feature_node.attribute("namespace");
let anno_name = feature_node.attribute("name").ok_or_else(|| {
anyhow!("Missing \"name\" attribute for node \"{node_name}\"")
})?;
let anno_value = SaltObject::from(
feature_node.attribute("value").unwrap_or_default(),
);
if annos_ns == Some("salt") {
if anno_name == "SNAME" {
// Only map this specific feature as document name
if salt_type == SaltType::Document {
updates.add_event(UpdateEvent::AddNodeLabel {
node_name: node_name.to_string(),
anno_ns: ANNIS_NS.to_string(),
anno_name: "doc".to_string(),
anno_value: anno_value.to_string(),
})?;
}
}
} else {
updates.add_event(UpdateEvent::AddNodeLabel {
node_name: node_name.to_string(),
anno_ns: annos_ns.unwrap_or_default().to_string(),
anno_name: anno_name.to_string(),
anno_value: anno_value.to_string(),
})?;
}
}
// Add meta annotations
for anno_node in get_meta_annotations(node) {
let annos_ns = anno_node.attribute("namespace");
if annos_ns != Some("salt") {
let anno_name = anno_node.attribute("name").ok_or_else(|| {
anyhow!("Missing \"name\" attribute for node \"{node_name}\"")
})?;
let anno_value = SaltObject::from(
anno_node.attribute("value").unwrap_or_default(),
);
updates.add_event(UpdateEvent::AddNodeLabel {
node_name: node_name.to_string(),
anno_ns: annos_ns.unwrap_or_default().to_string(),
anno_name: anno_name.to_string(),
anno_value: anno_value.to_string(),
})?;
}
}
}
_ => {}
}
}
// Add a PartOf Edge between parent corpora and the sub-corpora/documents
for e in cg.children().filter(|n| n.tag_name().name() == "edges") {
match SaltType::from_node(&e) {
SaltType::CorpusRelation | SaltType::DocumentRelation => {
let source_ref = e.attribute("source").unwrap_or_default();
let target_ref = e.attribute("target").unwrap_or_default();
let source_node = resolve_element(source_ref, "nodes", &nodes)
.and_then(|n| get_element_id(&n));
let target_node = resolve_element(target_ref, "nodes", &nodes)
.and_then(|n| get_element_id(&n));
if let (Some(source_node), Some(target_node)) = (source_node, target_node) {
// PartOf has the inverse meaning of the corpus and documentation relation in Salt
updates.add_event(UpdateEvent::AddEdge {
source_node: target_node,
target_node: source_node,
layer: ANNIS_NS.to_string(),
component_type: AnnotationComponentType::PartOf.to_string(),
component_name: "".into(),
})?;
}
}
_ => {}
}
}
}
Ok(documents)
}
}