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
use crate::{config::Format, Document};
use serde::{Deserialize, Serialize};
use std::io::Write;
#[derive(Eq, PartialEq, Serialize, Deserialize, Clone, Hash, Debug)]
pub struct Text {
pub text: String,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SpacyEntity {
pub entity: Vec<(usize, usize, String)>,
}
impl Format {
pub fn save(&self, annotations: Vec<Document>, path: &str) -> Result<String, std::io::Error> {
match self {
Format::Spacy => Format::spacy(annotations, path),
Format::Jsonl => Format::jsonl(annotations, path),
Format::Csv => Format::csv(annotations, path),
Format::Brat => Format::brat(annotations, path),
Format::Conll => Format::conll(annotations, path),
}
}
fn remove_extension_from_path(path: &str) -> String {
let mut path = path.to_string();
if path.contains('.') {
path.truncate(path.rfind('.').unwrap());
}
path
}
fn spacy(documents: Vec<Document>, path: &str) -> Result<String, std::io::Error> {
let path = Format::remove_extension_from_path(path);
let mut file = std::fs::File::create(format!("{path}.json"))?;
let annotations_tranformed: Vec<(String, SpacyEntity)> = documents
.into_iter()
.map(|annotation| {
(
annotation.text,
SpacyEntity {
entity: annotation.label,
},
)
})
.collect();
let json = serde_json::to_string(&annotations_tranformed).unwrap();
file.write_all(json.as_bytes())?;
Ok(path)
}
fn jsonl(documents: Vec<Document>, path: &str) -> Result<String, std::io::Error> {
let path = Format::remove_extension_from_path(path);
let mut file = std::fs::File::create(format!("{path}.jsonl"))?;
for document in documents {
let json = serde_json::to_string(&document).unwrap();
file.write_all(json.as_bytes())?;
file.write_all(b"\n")?;
}
Ok(path)
}
fn csv(documents: Vec<Document>, path: &str) -> Result<String, std::io::Error> {
let path = Format::remove_extension_from_path(path);
let mut file = std::fs::File::create(format!("{path}.csv"))?;
for document in documents {
let json = serde_json::to_string(&document).unwrap();
file.write_all(json.as_bytes())?;
file.write_all(b"\n")?;
}
Ok(path)
}
fn brat(documents: Vec<Document>, path: &str) -> Result<String, std::io::Error> {
let path = Format::remove_extension_from_path(path);
let mut file_ann = std::fs::File::create(format!("{path}.ann"))?;
let mut file_txt = std::fs::File::create(format!("{path}.txt"))?;
for document in documents {
let text = document.text;
file_txt.write_all(text.as_bytes())?;
file_txt.write_all(b"\n")?;
for (id, (start, end, label)) in document.label.into_iter().enumerate() {
let entity = text[start..end].to_string();
let line = format!("T{id}\t{label}\t{start}\t{end}\t{entity}");
file_ann.write_all(line.as_bytes())?;
file_ann.write_all(b"\n")?;
}
}
Ok(path)
}
fn conll(documents: Vec<Document>, path: &str) -> Result<String, std::io::Error> {
let path = Format::remove_extension_from_path(path);
let mut file = std::fs::File::create(format!("{path}.txt"))?;
let annotations_tranformed: Vec<Vec<(String, String)>> = documents
.into_iter()
.map(|annotation| {
let text = annotation.text;
let words: Vec<&str> = text.split_whitespace().collect();
let mut labels: Vec<String> = vec!["O".to_string(); words.len()];
for (start, end, label) in annotation.label {
let entity = text[start..end].to_string();
let index = words.iter().position(|&word| word.contains(&entity));
if index.is_none() {
continue;
}
let index = index.unwrap();
labels[index] = label;
}
words
.iter()
.zip(labels.iter())
.map(|(word, label)| (word.to_string(), label.to_string()))
.collect()
})
.collect();
for annotation in annotations_tranformed {
for (word, label) in annotation {
let line = format!("{word}\t{label}");
file.write_all(line.as_bytes())?;
file.write_all(b"\n")?;
}
file.write_all(b"\n")?;
}
Ok(path)
}
}