annatto 0.52.0

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
use regex::{Regex, RegexBuilder};
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::io::{BufRead, BufReader, Read};
use std::sync::{LazyLock, Mutex};

pub(super) enum Language {
    Unknown,
    English,
    Romanian,
    Italian,
    French,
    Portuguese,
    Galician,
    Catalan,
}

impl From<Language> for LanguageConfig {
    fn from(value: Language) -> Self {
        // Start with the defaults
        let mut p_char = r#"\[¿¡{'`"‚„†‡‹‘’“”•–—›»«"#;
        let mut f_char = r#"\]}'`",;:!?؟%‚„…†‡‰‹‘’“”•–—›»«"#;
        let mut p_clitic = "";
        let mut f_clitic = "";

        match value {
            Language::Unknown => { /* use the default values */ }
            Language::English => {
                f_clitic = "['’´](s|re|ve|d|m|em|ll)|n['’´]t";
            }
            Language::Romanian => {
                p_char = r#"\[¿¡{`"‚„†‡‹‘’“”•–—›»«"#;
                f_char = r#"\]}`",;:!?\%‚„…†‡‰‹‘’“”•–—›»«"#
            }
            Language::Italian => {
                p_clitic = "(?:d[ae]ll|nell|all|[ld]|sull|quest|un|senz|tutt|c|s)['´’]";
            }
            Language::French => {
                p_clitic = "(?:[dcjlmnst]|qu|jusqu|lorsqu|quoiqu|puisqu)['’´]";
                f_clitic = "-t-elles?|-t-ils?|-t-on|-ce|-elles?|-ils?|-je|-la|-les?|-leur|-lui|-mêmes?|-m['’´]|-moi|-nous|-on|-toi|-tu|-t['’´]|-vous|-en|-y|-ci|-là";
            }
            Language::Portuguese => {
                f_clitic = "-a|-as|-la|-las|-lha|-lhas|-lhe|-lhes|-lho|-lhos|-lo|-los|-ma|-mas|-me|-mo|-mos|-na|-nas|-no|-no-la|-no-las|-no-lo|-no-los|-nos|-o|-os|-s|-se|-se-á|-se-ão|-se-é|-se-ia|-se-lha|-se-lhas|-se-lhe|-se-lhes|-se-lho|-se-lhos|-se-nos|-se-vos|-ta|-tas|-te|-to|-tos|-vo-la|-vo-las|-vo-lo|-vo-los|-vos";
            }
            Language::Galician => {
                f_clitic = "-la|-las|-lo|-los|-nos";
            }
            Language::Catalan => {
                p_clitic = "[dlmnst]['’´]";
                f_clitic =
                    "['’´](n|s|ls|l|hi|ns|t|m|ho)|-(se|lo|la|li|los|les|hi|ho|ne|nos|me|s|te|m)";
            }
        }

        // TODO: make this configurable
        let abbreviations: HashSet<String> = HashSet::new();

        LanguageConfig {
            p_char: p_char.to_string(),
            f_char: f_char.to_string(),
            p_clitic: p_clitic.to_string(),
            f_clitic: f_clitic.to_string(),
            abbreviations,
        }
    }
}

impl<S> From<S> for Language
where
    S: AsRef<str>,
{
    fn from(value: S) -> Self {
        if value.as_ref().len() >= 2 {
            // Match the first two letters
            match &value.as_ref()[0..2] {
                "ca" => Language::Catalan,
                "en" => Language::English,
                "fr" => Language::French,
                "gl" => Language::Galician,
                "it" => Language::Italian,
                "pt" => Language::Portuguese,
                "ro" => Language::Romanian,
                _ => Language::Unknown,
            }
        } else {
            Language::Unknown
        }
    }
}

#[derive(Clone)]
struct LanguageConfig {
    /// Punctuation characters to cut of at a beginning of a word. Must be in a
    /// form that can be inserted into a Regex character class `[p_char]`.
    p_char: String,
    /// Punctuation characters to cut of at the ending of a word. Must be in a
    /// form that can be inserted into a Regex character class `[f_char]`.
    f_char: String,
    p_clitic: String,
    f_clitic: String,
    abbreviations: HashSet<String>,
}

/// A character to pre-mark locations to split. The algorithm will determine
/// places like spaces where to insert splits. But we can't use the space
/// character for this, because inside SGML tags there should actually not be a
/// split when there is a space character (blank).
/// To workaround this problem, a special character is defined instead of a blank that takes the role of marking where to split.
const SPLIT_MARKER: char = '\u{0179}';
const SPLIT_MARKER_STR: &str = "\u{0179}";

static COMPILED_REGEX_CACHE: LazyLock<Mutex<HashMap<String, Regex>>> =
    LazyLock::new(Mutex::default);

fn cached_regex(p: &str) -> crate::error::Result<Regex> {
    let mut cache = COMPILED_REGEX_CACHE.lock()?;
    if let Some(existing) = cache.get(p) {
        Ok(existing.clone())
    } else {
        let compiled = Regex::new(p)?;
        cache.insert(p.to_string(), compiled.clone());
        Ok(compiled)
    }
}

fn cached_regex_case_insensitive(p: &str) -> crate::error::Result<Regex> {
    let mut cache = COMPILED_REGEX_CACHE.lock()?;
    if let Some(existing) = cache.get(p) {
        Ok(existing.clone())
    } else {
        let compiled = RegexBuilder::new(p).case_insensitive(true).build()?;
        cache.insert(p.to_string(), compiled.clone());
        Ok(compiled)
    }
}

#[derive(Clone)]
pub(super) struct TreeTaggerTokenizer {
    config: LanguageConfig,
}

#[derive(Clone)]
pub(super) struct Token {
    pub value: String,
    pub whitespace_after: Option<String>,
}

impl Token {
    fn new_val<S: ToString>(value: S) -> Self {
        Self {
            value: value.to_string(),
            whitespace_after: None,
        }
    }
}

impl TreeTaggerTokenizer {
    pub(super) fn new(language: Language) -> anyhow::Result<Self> {
        let config: LanguageConfig = language.into();
        Ok(Self { config })
    }

    /// Returns a list of token and the possible whitespace that comes after each token
    pub(super) fn tokenize<R: Read>(&self, reader: R) -> anyhow::Result<Vec<Token>> {
        let mut result = Vec::new();

        let mut buffered_reader = BufReader::new(reader);

        // Tokenize line by line
        let mut line = String::new();
        let mut is_first_line = true;
        while buffered_reader.read_line(&mut line)? > 0 {
            if is_first_line {
                // The first line might contain a byte order marker (BOM)
                line = cached_regex("^\u{FEFF}")?.replace(&line, "").to_string();
                is_first_line = false;
            }
        }

        // Replace newline and tab charachters with spaces, so we don't have to distinguish them later
        line = cached_regex("[\n\t]")?.replace_all(&line, " ").to_string();

        // Spaces *inside* SGML tags (e.g. `<mytag a=" " b = "">` should be
        // protected and not create new separate token. Replace all spaces within a
        // special character, then replace all other spaces with another character
        // and restore the original spaces inside the SGML tags.
        while let Cow::Owned(new_line) =
            cached_regex("(<[^<> ]*) ([^<>]*>)")?.replace_all(&line, "${1}\u{0179}${2}")
        {
            line = new_line;
        }
        line = line.replace(' ', "\u{178}");
        line = line
            .replace('\u{0179}', " ")
            .replace('\u{178}', SPLIT_MARKER_STR);

        // Mark SGML tags as split points for the tokenization
        line = cached_regex("(<[^<>]*>)")?
            .replace_all(&line, &format!("{SPLIT_MARKER}$1{SPLIT_MARKER}"))
            .to_string();

        // Remove split marks at beginning and end of the line, and also repeating ones
        line = line.trim_matches(SPLIT_MARKER).to_string();
        line = cached_regex(&format!("{SPLIT_MARKER}{SPLIT_MARKER}{SPLIT_MARKER}*"))?
            .replace_all(&line, SPLIT_MARKER_STR)
            .to_string();

        // Split by the prepared split marker
        for segment in line.split(SPLIT_MARKER) {
            let mut segment = segment.to_string();
            if cached_regex("^<.*>$")?.is_match(&segment) {
                // The complete segment (not line) is an SGML tag and can be added as one token
                result.push(Token::new_val(segment));
            } else {
                // The pre-splitted segment can contain more than one token, e.g.
                // because of punctuation.

                // Special handling for "..."
                segment = cached_regex("(\\.\\.\\.)")?
                    .replace_all(&segment, " ... ")
                    .to_string();
                // Add missing blanks between certain punctuation characters
                segment = cached_regex("([;!?])([^ ])")?
                    .replace_all(&segment, "$1 $2")
                    .to_string();

                // Split the remaining string at blanks and use this as initial
                // token list. Since we already know this is not an SGML-Tag, no
                // special whitespace handling is necessary.
                for mut current_token in segment.split(' ').map(str::to_string) {
                    let mut suffix = Vec::new();
                    // Separate punctuation and parentheses from words
                    let mut finished = false;
                    while !finished {
                        if let Some(m) =
                            substitute("^(\\()([^\\)]*)(.)$", "$2$3", &mut current_token)?
                        {
                            // Separate preceding parentheses
                            result.push(Token::new_val(m.get(1).map_or("", |m| m.as_str())));
                        } else if let Some(m) =
                            substitute("^([^(]+)(\\))$", "$1", &mut current_token)?
                        {
                            // Separate following preceding parentheses
                            suffix.insert(0, Token::new_val(m.get(2).map_or("", |m| m.as_str())));
                        } else if let Some(m) = substitute(
                            &format!("^([{}])(.)", &self.config.p_char),
                            "$2",
                            &mut current_token,
                        )? {
                            // Separate preceding punctuation
                            result.push(Token::new_val(m.get(1).map_or("", |m| m.as_str())));
                        } else if let Some(m) = substitute(
                            &format!("(.)([{}])$", &self.config.f_char),
                            "$1",
                            &mut current_token,
                        )? {
                            // Separate trailing punctuation
                            suffix.insert(0, Token::new_val(m.get(2).map_or("", |m| m.as_str())));
                        } else if let Some(m) = substitute(
                            &format!("([{}]|\\))\\.$", &self.config.f_char),
                            "",
                            &mut current_token,
                        )? {
                            // Separate trailing periods if punctuation precedes
                            suffix.insert(0, Token::new_val("."));
                            let punction_before_period =
                                m.get(1).map_or("", |m| m.as_str()).to_string();
                            if current_token.is_empty() {
                                current_token = punction_before_period;
                            } else {
                                suffix.insert(0, Token::new_val(punction_before_period));
                            }
                        } else {
                            finished = true;
                        }
                    }

                    // handle explicitly listed tokens
                    if self.config.abbreviations.contains(&current_token) {
                        result.push(Token::new_val(&current_token));
                        result.extend(suffix.iter().cloned());
                        continue;
                    }
                    // abbreviations of the form A. or U.S.A.
                    if cached_regex("^([A-Za-z-]\\.)+$")?.is_match(&current_token) {
                        result.push(Token::new_val(&current_token));
                        result.extend(suffix.iter().cloned());
                        continue;
                    }
                    // disambiguate periods
                    if let Some(m) = cached_regex("^(..*)\\.$")?.captures(&current_token.clone())
                        && current_token != "..."
                    {
                        current_token = m.get(1).map_or("", |m| m.as_str()).to_string();
                        suffix.insert(0, Token::new_val("."));
                        if self.config.abbreviations.contains(&current_token) {
                            result.push(Token::new_val(&current_token));
                            result.extend(suffix.iter().cloned());
                            continue;
                        }
                    }
                    // cut off clitics
                    while let Some(m) = substitute("^(--)(.)", "$2", &mut current_token)? {
                        result.push(Token::new_val(m.get(1).map_or("", |m| m.as_str())));
                    }
                    if !self.config.p_clitic.is_empty() {
                        while let Some(m) = substitute_i(
                            &format!("^({})(.)", self.config.p_clitic),
                            "$2",
                            &mut current_token,
                        )? {
                            result.push(Token::new_val(m.get(1).map_or("", |m| m.as_str())));
                        }
                    }
                    while let Some(m) = substitute("(.)(--)$", "$1", &mut current_token)? {
                        suffix.insert(0, Token::new_val(m.get(2).map_or("", |m| m.as_str())));
                    }
                    if !self.config.f_clitic.is_empty() {
                        while let Some(m) = substitute_i(
                            &format!("(.)({})$", self.config.f_clitic),
                            "$1",
                            &mut current_token,
                        )? {
                            suffix.insert(0, Token::new_val(m.get(2).map_or("", |m| m.as_str())));
                        }
                    }
                    result.push(Token::new_val(current_token));
                    result.extend(suffix);
                }
            }
        }

        let result = result
            .into_iter()
            .filter(|t| !t.value.is_empty() || t.whitespace_after.is_some())
            .collect();

        Ok(result)
    }
}

/// Substitute the first match for the `pattern` in the `buffer` with the `replacement`.
/// The `replacement` can contain back-references to the pattern.
/// Returns `Ok(Some(Vec<String>))` with the captured values as string vector if there was a match.
fn substitute(
    pattern: &str,
    replacement: &str,
    buffer: &mut String,
) -> anyhow::Result<Option<Vec<String>>> {
    let pattern = cached_regex(pattern)?;
    // The first capture group is always the whole match
    if let Some(caps) = pattern.captures(buffer)
        && let Some(whole_match) = caps.get(0)
    {
        // Collect the captured values before we replace the original buffer
        let captured_values = caps
            .iter()
            .map(|c| {
                if let Some(m) = c {
                    m.as_str().to_string()
                } else {
                    String::new()
                }
            })
            .collect();

        // Get the expanded captures as string
        let mut expanded_replacment = String::new();
        caps.expand(replacement, &mut expanded_replacment);
        // Add the left and right context from the original buffer to the replacement
        expanded_replacment.insert_str(0, &buffer[0..whole_match.start()]);
        expanded_replacment.push_str(&buffer[whole_match.end()..]);
        // Replace the whole match with the expanded string in the original buffer
        *buffer = expanded_replacment;

        return Ok(Some(captured_values));
    }
    Ok(None)
}

/// Substitute the first match for the `pattern` in the `buffer` with the `replacement`.
/// The search is **case-insensitive**.
/// The `replacement` can contain back-references to the pattern.
/// Returns `Ok(Some(Vec<String>))` with the captured values as string vector if there was a match.
fn substitute_i(
    pattern: &str,
    replacement: &str,
    buffer: &mut String,
) -> anyhow::Result<Option<Vec<String>>> {
    let pattern = cached_regex_case_insensitive(pattern)?;
    // The first capture group is always the whole match
    if let Some(caps) = pattern.captures(buffer)
        && let Some(whole_match) = caps.get(0)
    {
        // Collect the captured values before we replace the original buffer
        let captured_values = caps
            .iter()
            .map(|c| {
                if let Some(m) = c {
                    m.as_str().to_string()
                } else {
                    String::new()
                }
            })
            .collect();

        // Get the expanded captures as string
        let mut expanded_replacment = String::new();
        caps.expand(replacement, &mut expanded_replacment);
        // Add the left and right context from the original buffer to the replacement
        expanded_replacment.insert_str(0, &buffer[0..whole_match.start()]);
        expanded_replacment.push_str(&buffer[whole_match.end()..]);
        // Replace the whole match with the expanded string in the original buffer
        *buffer = expanded_replacment;

        return Ok(Some(captured_values));
    }
    Ok(None)
}

#[cfg(test)]
mod tests;