tipping-rs 0.2.1

A Rust implementation of Token Interdependency Parsing (Tipping) algorithm
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
use std::{collections::BTreeSet, marker::PhantomData};

use hashbrown::{HashMap, HashSet};
use rayon::prelude::*;

use fancy_regex::Regex;

use crate::{
    graph::{anchor_nodes, build_graph},
    template::{parameter_masks, shared_slices, templates},
    token_filter::StaticFilter,
    token_record::TokenRecord,
    tokenizer::{Token, Tokenizer},
    traits::Tokenize,
};

type Clusters = Vec<Option<usize>>;
type Templates = Vec<std::collections::HashSet<String>>;
type Masks = std::collections::HashMap<String, String>;

pub struct NoCompute;
pub struct Compute;

/// Tipping (Token Interdependency Parsing) log parser
/// ```
/// use fancy_regex::Regex;
///
///let msgs = vec![
///     "User 'admin' logged in from IP address 192.168.1.10",
///     "Attempt to access unauthorized resource by user 'guest'",
///     "Database connection failed due to timeout",
///     "Processing request for data retrieval with queryId: 34521",
/// ];
///
///let special_whites = vec![];
///let special_blacks = vec![Regex::new(r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}").unwrap()];
///let symbols = "'{}|,".chars().collect();
///let (event_ids, masks, templates) = tipping_rs::Parser::default()
///    .with_threshold(0.5)
///    .with_special_whites(special_whites)
///    .with_special_blacks(special_blacks)
///    .with_symbols(symbols)
///    .with_filter_alphabetic(true)
///    .with_filter_numeric(false)
///    .with_filter_impure(false)
///    .compute_templates()
///    .compute_masks()
///    .parse(&msgs);
/// ```
#[derive(Debug, Clone)]
pub struct Parser<Templates = NoCompute, Masks = NoCompute> {
    threshold: f32,
    special_whites: Vec<Regex>,
    special_blacks: Vec<Regex>,
    symbols: HashSet<char>,
    filter_alphabetic: bool,
    filter_numeric: bool,
    filter_impure: bool,
    compute_templates: PhantomData<Templates>,
    compute_mask: PhantomData<Masks>,
}

impl Default for Parser {
    fn default() -> Self {
        Self::new()
    }
}
impl Parser {
    /// Initiates a new Parser with default parameters
    pub fn new() -> Self {
        Parser {
            threshold: 0.5,
            special_whites: Default::default(),
            special_blacks: Default::default(),
            symbols: Default::default(),
            filter_alphabetic: true,
            filter_numeric: false,
            filter_impure: false,
            compute_templates: Default::default(),
            compute_mask: Default::default(),
        }
    }

    /// Sets `value` as threshold. The threshold determines if an interdependency
    /// link should be considered during key node computation or not. The
    /// threshod should be `0 <= threshold <= 1`.
    #[must_use]
    pub fn with_threshold(mut self, value: f32) -> Self {
        assert!(0.0 <= value);
        assert!(value <= 1.0);
        self.threshold = value;
        self
    }

    /// Sets `value` as spcial white regexes. White regexes are never parameterized.
    #[must_use]
    pub fn with_special_whites(mut self, value: Vec<Regex>) -> Self {
        self.special_whites = value;
        self
    }

    /// Sets `value` as special black regexes. Black regexes are always parameterized.
    #[must_use]
    pub fn with_special_blacks(mut self, value: Vec<Regex>) -> Self {
        self.special_blacks = value;
        self
    }

    /// Sets `value` as symbols. Symbolds are characters that are used alongside the
    /// white spaces to split string during tokenization.
    #[must_use]
    pub fn with_symbols(mut self, value: HashSet<char>) -> Self {
        self.symbols = value;
        self
    }

    /// The `value` determines if alphabetic tokens should be used during key node computation.
    #[must_use]
    pub fn with_filter_alphabetic(mut self, value: bool) -> Self {
        self.filter_alphabetic = value;
        self
    }

    /// The `value` determines if numeric tokens should be used during key node computation.
    #[must_use]
    pub fn with_filter_numeric(mut self, value: bool) -> Self {
        self.filter_numeric = value;
        self
    }

    /// The `value` determines if impure tokens should be used during key node computation.
    #[must_use]
    pub fn with_filter_impure(mut self, value: bool) -> Self {
        self.filter_impure = value;
        self
    }
}

impl<T> Parser<NoCompute, T> {
    // Add templates computation to the output
    #[must_use]
    pub fn compute_templates(self) -> Parser<Compute, T> {
        Parser::<Compute, T> {
            threshold: self.threshold,
            special_whites: self.special_whites,
            special_blacks: self.special_blacks,
            symbols: self.symbols,
            filter_alphabetic: self.filter_alphabetic,
            filter_numeric: self.filter_numeric,
            filter_impure: self.filter_impure,
            compute_templates: Default::default(),
            compute_mask: Default::default(),
        }
    }
}

impl<T> Parser<T, NoCompute> {
    // Add parameter mask computation to the output
    #[must_use]
    pub fn compute_masks(self) -> Parser<T, Compute> {
        Parser::<T, Compute> {
            threshold: self.threshold,
            special_whites: self.special_whites,
            special_blacks: self.special_blacks,
            symbols: self.symbols,
            filter_alphabetic: self.filter_alphabetic,
            filter_numeric: self.filter_numeric,
            filter_impure: self.filter_impure,
            compute_templates: Default::default(),
            compute_mask: Default::default(),
        }
    }
}

impl Parser<NoCompute, NoCompute> {
    /// Parses the input `messages` and returns `Clusters`.
    ///
    /// - `Clusters`: A `Vec<Option<usize>>` representing potential cluster IDs. Each `Option<usize>`
    ///   corresponds to the cluster ID of the message at the same index, or `None` if the message
    ///   couldn't be parsed.
    ///
    pub fn parse<Message: AsRef<str> + Sync>(self, messages: &[Message]) -> Clusters {
        let tokenizer = Tokenizer::new(self.special_whites, self.special_blacks, self.symbols);
        let filter = StaticFilter::with(
            self.filter_alphabetic,
            self.filter_numeric,
            self.filter_impure,
        );
        let idep = TokenRecord::new(messages, &tokenizer, &filter);
        let cmap = group_by_anchor_tokens(messages, &tokenizer, &idep, self.threshold);
        let mut clus = vec![None; messages.len()];
        cmap.into_iter()
            .filter(|(anchor_toks, _)| !anchor_toks.is_empty())
            .enumerate()
            .for_each(|(cid, (_, indices))| {
                for idx in indices {
                    clus[idx] = Some(cid);
                }
            });
        clus
    }
}

impl Parser<Compute, NoCompute> {
    /// Parses the input `messages` and returns `Clusters`, and `Templates`.
    ///
    /// - `Clusters`: A `Vec<Option<usize>>` representing potential cluster IDs. Each `Option<usize>`
    ///   corresponds to the cluster ID of the message at the same index, or `None` if the message
    ///   couldn't be parsed.
    ///
    /// - `Templates`: A `Vec<HashSet<String>>` where each set of templates is aligned with the
    ///   corresponding cluster ID in the `Clusters` vector.
    ///
    pub fn parse<Message: AsRef<str> + Sync>(self, messages: &[Message]) -> (Clusters, Templates) {
        let tokenizer = Tokenizer::new(self.special_whites, self.special_blacks, self.symbols);
        let filter = StaticFilter::with(
            self.filter_alphabetic,
            self.filter_numeric,
            self.filter_impure,
        );
        let idep = TokenRecord::new(messages, &tokenizer, &filter);
        let cmap = group_by_anchor_tokens(messages, &tokenizer, &idep, self.threshold);
        let mut clus = vec![None; messages.len()];
        let mut temps = vec![HashSet::default(); cmap.len()];
        let tokenizer =
            tokenizer.new_with_symbols("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".chars().collect());
        cmap.into_iter()
            .filter(|(anchor_toks, _)| !anchor_toks.is_empty())
            .enumerate()
            .for_each(|(cid, (_, indices))| {
                let stok = shared_slices(
                    indices.iter().cloned().map(|idx| messages[idx].as_ref()),
                    &tokenizer,
                    self.filter_alphabetic,
                    self.filter_numeric,
                    self.filter_impure,
                );
                temps[cid] = templates(
                    indices.iter().cloned().map(|idx| messages[idx].as_ref()),
                    &tokenizer,
                    &stok,
                );
                for idx in indices {
                    clus[idx] = Some(cid);
                }
            });

        (
            clus,
            temps
                .into_iter()
                .map(|map| map.into_iter().collect())
                .collect(),
        )
    }
}

impl Parser<NoCompute, Compute> {
    /// Parses the input `messages` and returns `Clusters`,  `Masks`.
    ///
    /// - `Clusters`: A `Vec<Option<usize>>` representing potential cluster IDs. Each `Option<usize>`
    ///   corresponds to the cluster ID of the message at the same index, or `None` if the message
    ///   couldn't be parsed.
    ///
    /// - `Masks`: A table mapping each message to its parameter masks.
    ///
    pub fn parse<Message: AsRef<str> + Sync>(self, messages: &[Message]) -> (Clusters, Masks) {
        let tokenizer = Tokenizer::new(self.special_whites, self.special_blacks, self.symbols);
        let filter = StaticFilter::with(
            self.filter_alphabetic,
            self.filter_numeric,
            self.filter_impure,
        );
        let idep = TokenRecord::new(messages, &tokenizer, &filter);
        let cmap = group_by_anchor_tokens(messages, &tokenizer, &idep, self.threshold);
        let mut clus = vec![None; messages.len()];
        let mut masks = HashMap::new();
        let tokenizer =
            tokenizer.new_with_symbols("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".chars().collect());
        cmap.into_iter()
            .filter(|(anchor_toks, _)| !anchor_toks.is_empty())
            .enumerate()
            .for_each(|(cid, (_, indices))| {
                let stok = shared_slices(
                    indices.iter().cloned().map(|idx| messages[idx].as_ref()),
                    &tokenizer,
                    self.filter_alphabetic,
                    self.filter_numeric,
                    self.filter_impure,
                );
                masks.extend(parameter_masks(
                    indices.iter().cloned().map(|idx| messages[idx].as_ref()),
                    &tokenizer,
                    &stok,
                ));
                for idx in indices {
                    clus[idx] = Some(cid);
                }
            });

        (clus, masks.into_iter().collect())
    }
}

impl Parser<Compute, Compute> {
    /// Parses the input `messages` and returns `Clusters`, `Templates`, and `Masks`.
    ///
    /// - `Clusters`: A `Vec<Option<usize>>` representing potential cluster IDs. Each `Option<usize>`
    ///   corresponds to the cluster ID of the message at the same index, or `None` if the message
    ///   couldn't be parsed.
    ///
    /// - `Templates`: A `Vec<HashSet<String>>` where each set of templates is aligned with the
    ///   corresponding cluster ID in the `Clusters` vector.
    ///
    /// - `Masks`: A table mapping each message to its parameter masks.
    ///
    pub fn parse<Message: AsRef<str> + Sync>(
        self,
        messages: &[Message],
    ) -> (Clusters, Templates, Masks) {
        let tokenizer = Tokenizer::new(self.special_whites, self.special_blacks, self.symbols);
        let filter = StaticFilter::with(
            self.filter_alphabetic,
            self.filter_numeric,
            self.filter_impure,
        );
        let idep = TokenRecord::new(messages, &tokenizer, &filter);
        let groups = group_by_anchor_tokens(messages, &tokenizer, &idep, self.threshold);
        let mut clus = vec![None; messages.len()];
        let mut temps = vec![HashSet::default(); groups.len()];
        let mut masks = HashMap::new();
        let tokenizer =
            tokenizer.new_with_symbols("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~".chars().collect());
        groups
            .into_iter()
            .filter(|(anchor_toks, _)| !anchor_toks.is_empty())
            .enumerate()
            .for_each(|(cid, (_, indices))| {
                let stok = shared_slices(
                    indices.iter().cloned().map(|idx| messages[idx].as_ref()),
                    &tokenizer,
                    self.filter_alphabetic,
                    self.filter_numeric,
                    self.filter_impure,
                );
                temps[cid] = templates(
                    indices.iter().cloned().map(|idx| messages[idx].as_ref()),
                    &tokenizer,
                    &stok,
                );
                masks.extend(parameter_masks(
                    indices.iter().cloned().map(|idx| messages[idx].as_ref()),
                    &tokenizer,
                    &stok,
                ));
                for idx in indices {
                    clus[idx] = Some(cid);
                }
            });

        (
            clus,
            temps
                .into_iter()
                .map(|map| map.into_iter().collect())
                .collect(),
            masks.into_iter().collect(),
        )
    }
}

fn group_by_anchor_tokens<'a, T: AsRef<str> + Sync>(
    messages: &'a [T],
    tokenizer: &Tokenizer,
    idep: &'a TokenRecord<'a>,
    threshold: f32,
) -> HashMap<BTreeSet<Token<'a>>, BTreeSet<usize>> {
    messages
        .iter()
        .enumerate()
        .par_bridge()
        .map(|(idx, msg)| {
            (idx, {
                let tokens = tokenizer.tokenize(msg.as_ref());
                let graph = build_graph(
                    tokens
                        .iter()
                        .copied()
                        .filter(|tok| idep.occurence(tok.as_str()).is_some()),
                    |tok1, tok2| {
                        idep.dependency(tok1.as_str(), tok2.as_str()).unwrap_or(0.0) > threshold
                    },
                );
                let mut anchor_toks = anchor_nodes(graph);
                for tok in tokens {
                    match tok {
                        Token::SpecialWhite(_) => {
                            anchor_toks.insert(tok);
                        }
                        Token::SpecialBlack(_) => {
                            anchor_toks.remove(&tok);
                        }
                        _ => (),
                    }
                }
                anchor_toks
            })
        })
        .fold_with(
            HashMap::<BTreeSet<Token<'a>>, BTreeSet<usize>>::new(),
            |mut map, (idx, anchor_tokens)| {
                map.entry(anchor_tokens)
                    .and_modify(|indices| {
                        indices.insert(idx);
                    })
                    .or_insert([idx].into());
                map
            },
        )
        .reduce(Default::default, |mut m1, mut m2| {
            if m1.len() > m2.len() {
                m1.reserve(m2.len());
                for (k, v) in m2 {
                    if let Some(set) = m1.get_mut(&k) {
                        set.extend(v);
                    } else {
                        m1.insert(k, v);
                    }
                }
                m1
            } else {
                m2.reserve(m1.len());
                for (k, v) in m1 {
                    if let Some(set) = m2.get_mut(&k) {
                        set.extend(v);
                    } else {
                        m2.insert(k, v);
                    }
                }
                m2
            }
        })
}