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
use log::{debug, error};
use serde::Deserialize;
use std::{collections::HashSet, fs};
use std::{fmt::Display, fmt::Formatter, iter::FromIterator};
use crate::utils::{
contains_numbers, contains_punctuation, contains_special_characters, is_alphanumeric,
};
#[derive(Deserialize, Clone)]
pub struct Config {
pub texts: Texts,
pub annotations: Annotations,
pub entities: Entities,
pub logging: Option<Logging>,
}
impl Default for Config {
fn default() -> Self {
Config {
texts: Texts::default(),
annotations: Annotations::default(),
entities: Entities::default(),
logging: Some(Logging::default()),
}
}
}
#[derive(Deserialize, Clone)]
#[serde(default)]
pub struct Logging {
pub level: String,
}
impl Default for Logging {
fn default() -> Self {
Logging {
level: "info".to_string(),
}
}
}
#[derive(Deserialize, Clone, Default)]
pub struct Texts {
pub input: Input,
pub filters: Filters,
}
#[derive(Deserialize, Clone)]
pub struct Input {
pub path: String,
pub filter: Option<bool>,
}
impl Default for Input {
fn default() -> Self {
Input {
path: "".to_string(),
filter: Some(true),
}
}
}
#[derive(Deserialize, Clone)]
pub struct Filters {
pub alphanumeric: bool,
pub case_sensitive: bool,
pub min_length: i32,
pub max_length: i32,
pub punctuation: bool,
pub numbers: bool,
pub special_characters: bool,
pub accept_special_characters: Option<String>,
pub list_of_special_characters: Option<HashSet<char>>,
}
impl Default for Filters {
fn default() -> Self {
Filters {
alphanumeric: false,
case_sensitive: false,
min_length: 0,
max_length: 1024,
punctuation: false,
numbers: false,
special_characters: false,
accept_special_characters: None,
list_of_special_characters: Some(HashSet::new()),
}
}
}
impl Display for Filters {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"alphanumeric: {}, case_sensitive: {}, min_length: {}, max_length: {}, punctuation: {}, numbers: {}, special_characters: {}, accept_special_characters: {:?}",
self.alphanumeric, self.case_sensitive, self.min_length, self.max_length, self.punctuation, self.numbers, self.special_characters, self.accept_special_characters
)
}
}
impl Filters {
pub fn set_special_characters(&mut self) {
let special_characters: HashSet<char> = HashSet::from_iter(vec![
'@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}',
';', ':', '"', '\'', '<', '>', ',', '.', '?', '/', '\\', '|', '~', '`',
]);
let accept_special_characters: HashSet<char> = self
.accept_special_characters
.as_ref()
.unwrap_or(&"".to_string())
.chars()
.collect();
self.list_of_special_characters = Some(
special_characters
.difference(&accept_special_characters)
.cloned()
.collect(),
);
}
pub fn get_special_characters(&self) -> HashSet<char> {
self.list_of_special_characters.as_ref().unwrap().clone()
}
pub fn is_valid(&self, text: &str) -> bool {
if text.is_empty() {
return false;
}
if self.alphanumeric && is_alphanumeric(text) {
debug!("{} is not alphanumeric", text);
return false;
}
if self.punctuation && contains_punctuation(text) {
debug!("'{}' contains punctuation", text);
return false;
}
if self.numbers && contains_numbers(text) {
debug!("{} does not contain numbers", text);
return false;
}
if self.special_characters
&& contains_special_characters(text, self.get_special_characters())
{
debug!("{} contains special characters", text);
return false;
}
if self.min_length >= 0 && text.len() < self.min_length as usize {
debug!("{} is too short", text);
return false;
}
if self.max_length >= 0 && text.len() > self.max_length as usize {
return false;
}
true
}
}
#[derive(Debug, Deserialize, Clone, Default)]
pub struct Annotations {
pub output: Output,
pub format: Format,
}
#[derive(Debug, Deserialize, Clone, Default)]
pub enum Format {
#[serde(rename = "csv")]
Csv,
#[serde(rename = "jsonl")]
#[default]
Jsonl,
#[serde(rename = "spacy")]
Spacy,
#[serde(rename = "brat")]
Brat,
#[serde(rename = "conll")]
Conll,
}
#[derive(Debug, Deserialize, Clone, Default)]
pub struct Output {
pub path: String,
}
#[derive(Deserialize, Clone, Default)]
pub struct Entities {
pub input: Input,
pub filters: Filters,
pub excludes: Excludes,
}
#[derive(Debug, Deserialize, Clone, Default)]
pub struct Excludes {
pub path: Option<String>,
}
impl Config {
pub fn from_file(path: &str) -> Self {
let config = fs::read_to_string(path).expect("Unable to read the configuration file");
let config = toml::from_str(&config);
match config {
Ok(config) => config,
Err(e) => {
error!("Unable to parse the configuration file: {}", e);
std::process::exit(1);
}
}
}
pub fn summary(&self) {
debug!("------------------------------");
debug!("Configuration file summary |");
debug!("------------------------------");
debug!("Texts input path: {}", self.texts.input.path);
debug!("Texts filters: {}", self.texts.filters);
debug!("Annotations output path: {}", self.annotations.output.path);
debug!("Entities input path: {}", self.entities.input.path);
debug!("Entities filters: {}", self.entities.filters);
debug!(
"Entities excludes path: {}",
self.entities
.excludes
.path
.as_ref()
.unwrap_or(&"None".to_string())
);
}
}