scirs2-text 0.4.3

Text processing module for SciRS2 (scirs2-text)
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
//! Pattern-based information extraction using token sequences.
//!
//! Provides a flexible rule engine operating on tokenized input.  Each pattern
//! is a sequence of [`PatternElement`]s that are matched left-to-right against
//! a slice of [`Token`]s.  Gap elements allow for skip-regions of bounded
//! length.

use regex::Regex;

use crate::error::{Result, TextError};

// ---------------------------------------------------------------------------
// Token type
// ---------------------------------------------------------------------------

/// A single token with optional part-of-speech and lemma annotations.
#[derive(Debug, Clone)]
pub struct Token {
    /// Surface form of the token.
    pub text: String,
    /// Optional part-of-speech tag (e.g. "NN", "VBZ").
    pub pos: Option<String>,
    /// Optional base/lemma form.
    pub lemma: Option<String>,
}

impl Token {
    /// Convenience constructor.
    pub fn new(text: impl Into<String>) -> Token {
        Token {
            text: text.into(),
            pos: None,
            lemma: None,
        }
    }

    /// Builder: set POS tag.
    pub fn with_pos(mut self, pos: impl Into<String>) -> Token {
        self.pos = Some(pos.into());
        self
    }

    /// Builder: set lemma.
    pub fn with_lemma(mut self, lemma: impl Into<String>) -> Token {
        self.lemma = Some(lemma.into());
        self
    }
}

// ---------------------------------------------------------------------------
// Pattern element
// ---------------------------------------------------------------------------

/// A single element in a named extraction pattern.
#[derive(Debug, Clone)]
pub enum PatternElement {
    /// Match a fixed string (case-insensitive).
    Literal(String),
    /// Match a token whose POS tag equals the given string.
    PoS(String),
    /// Match a token whose text matches the given regex string.
    Regex(String),
    /// Match any single token.
    Any,
    /// Match between `min` and `max` tokens (inclusive), skipping them.
    Gap {
        /// Minimum number of tokens to skip.
        min: usize,
        /// Maximum number of tokens to skip.
        max: usize,
    },
}

// ---------------------------------------------------------------------------
// Pattern and result types
// ---------------------------------------------------------------------------

/// A sequence of [`PatternElement`]s representing one extraction rule.
#[derive(Debug, Clone)]
pub struct Pattern {
    /// Ordered list of elements that must match in sequence.
    pub template: Vec<PatternElement>,
}

impl Pattern {
    /// Construct a pattern from a template.
    pub fn new(template: Vec<PatternElement>) -> Pattern {
        Pattern { template }
    }
}

/// A single extraction match.
#[derive(Debug, Clone)]
pub struct Match {
    /// Name of the pattern that produced this match.
    pub pattern_name: String,
    /// Index of the first token in the match.
    pub start: usize,
    /// Index one past the last token in the match.
    pub end: usize,
    /// Captured text fragments (one per non-Gap, non-Any element).
    pub groups: Vec<String>,
}

// ---------------------------------------------------------------------------
// PatternMatcher
// ---------------------------------------------------------------------------

/// A collection of named extraction patterns applied to token sequences.
#[derive(Default)]
pub struct PatternMatcher {
    patterns: Vec<(String, Pattern)>,
    /// Compiled regexes cached by pattern string.
    regex_cache: std::collections::HashMap<String, Regex>,
}

impl PatternMatcher {
    /// Create an empty matcher.
    pub fn new() -> PatternMatcher {
        PatternMatcher::default()
    }

    /// Add a named pattern.
    pub fn add_pattern(&mut self, name: impl Into<String>, pattern: Pattern) -> Result<()> {
        // Pre-compile any Regex elements
        for elem in &pattern.template {
            if let PatternElement::Regex(re_str) = elem {
                if !self.regex_cache.contains_key(re_str) {
                    let compiled = Regex::new(re_str).map_err(|e| {
                        TextError::InvalidInput(format!("Bad regex '{}': {}", re_str, e))
                    })?;
                    self.regex_cache.insert(re_str.clone(), compiled);
                }
            }
        }
        self.patterns.push((name.into(), pattern));
        Ok(())
    }

    /// Find all pattern matches in the token sequence.
    ///
    /// Tries every starting position for every pattern; returns all matches
    /// (possibly overlapping).
    pub fn match_all(&self, tokens: &[Token]) -> Vec<Match> {
        let mut results = Vec::new();
        for (name, pattern) in &self.patterns {
            for start in 0..tokens.len() {
                if let Some((end, groups)) = self.try_match(pattern, tokens, start) {
                    results.push(Match {
                        pattern_name: name.clone(),
                        start,
                        end,
                        groups,
                    });
                }
            }
        }
        results
    }

    /// Attempt to match `pattern` starting at `start` in `tokens`.
    ///
    /// Returns `Some((end_exclusive, captured_groups))` on success.
    fn try_match(
        &self,
        pattern: &Pattern,
        tokens: &[Token],
        start: usize,
    ) -> Option<(usize, Vec<String>)> {
        self.try_match_from(pattern, tokens, start, 0, Vec::new())
    }

    /// Recursive helper that matches pattern elements from `elem_idx` onwards,
    /// starting at token position `pos`, with previously captured `groups`.
    fn try_match_from(
        &self,
        pattern: &Pattern,
        tokens: &[Token],
        pos: usize,
        elem_idx: usize,
        groups: Vec<String>,
    ) -> Option<(usize, Vec<String>)> {
        // All elements matched
        if elem_idx >= pattern.template.len() {
            return Some((pos, groups));
        }

        let elem = &pattern.template[elem_idx];

        match elem {
            PatternElement::Literal(s) => {
                if pos >= tokens.len() {
                    return None;
                }
                if tokens[pos].text.to_lowercase() != s.to_lowercase() {
                    return None;
                }
                let mut new_groups = groups;
                new_groups.push(tokens[pos].text.clone());
                self.try_match_from(pattern, tokens, pos + 1, elem_idx + 1, new_groups)
            }
            PatternElement::PoS(tag) => {
                if pos >= tokens.len() {
                    return None;
                }
                let tok_pos = tokens[pos].pos.as_deref().unwrap_or("");
                if tok_pos != tag.as_str() {
                    return None;
                }
                let mut new_groups = groups;
                new_groups.push(tokens[pos].text.clone());
                self.try_match_from(pattern, tokens, pos + 1, elem_idx + 1, new_groups)
            }
            PatternElement::Regex(re_str) => {
                if pos >= tokens.len() {
                    return None;
                }
                let re = self.regex_cache.get(re_str)?;
                if !re.is_match(&tokens[pos].text) {
                    return None;
                }
                let mut new_groups = groups;
                new_groups.push(tokens[pos].text.clone());
                self.try_match_from(pattern, tokens, pos + 1, elem_idx + 1, new_groups)
            }
            PatternElement::Any => {
                if pos >= tokens.len() {
                    return None;
                }
                let mut new_groups = groups;
                new_groups.push(tokens[pos].text.clone());
                self.try_match_from(pattern, tokens, pos + 1, elem_idx + 1, new_groups)
            }
            PatternElement::Gap { min, max } => {
                // Try each skip length from min to max; return the first
                // that allows the rest of the pattern to match.
                for skip in *min..=*max {
                    let new_pos = pos + skip;
                    if new_pos > tokens.len() {
                        break;
                    }
                    if let Some(result) =
                        self.try_match_from(pattern, tokens, new_pos, elem_idx + 1, groups.clone())
                    {
                        return Some(result);
                    }
                }
                None
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Built-in rule-based NER patterns
// ---------------------------------------------------------------------------

/// Build a `PatternMatcher` pre-loaded with common NER patterns.
///
/// Includes patterns for dates, money, email, URL, and phone numbers.
pub fn build_ner_pattern_matcher() -> Result<PatternMatcher> {
    let mut matcher = PatternMatcher::new();

    // Date: MM/DD/YYYY or YYYY-MM-DD
    matcher.add_pattern(
        "DATE",
        Pattern::new(vec![PatternElement::Regex(
            r"(?:(?:0?[1-9]|1[0-2])[\/\-](?:0?[1-9]|[12][0-9]|3[01])[\/\-](?:19|20)?\d{2}|(?:19|20)\d{2}[\/\-](?:0?[1-9]|1[0-2])[\/\-](?:0?[1-9]|[12][0-9]|3[01]))".to_string(),
        )]),
    )?;

    // Money: $1234.56 or similar
    matcher.add_pattern(
        "MONEY",
        Pattern::new(vec![PatternElement::Regex(
            r"\$[0-9]+(?:\.[0-9]+)?".to_string(),
        )]),
    )?;

    // Email
    matcher.add_pattern(
        "EMAIL",
        Pattern::new(vec![PatternElement::Regex(
            r"[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}".to_string(),
        )]),
    )?;

    // URL
    matcher.add_pattern(
        "URL",
        Pattern::new(vec![PatternElement::Regex(r"https?://[^\s]+".to_string())]),
    )?;

    // Phone: (NNN) NNN-NNNN or NNN-NNN-NNNN
    matcher.add_pattern(
        "PHONE",
        Pattern::new(vec![PatternElement::Regex(
            r"(?:\+?1[\-.\s]?)?\(?\d{3}\)?[\-.\s]\d{3}[\-.\s]\d{4}".to_string(),
        )]),
    )?;

    Ok(matcher)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn tokenize_simple(text: &str) -> Vec<Token> {
        text.split_whitespace().map(Token::new).collect()
    }

    #[test]
    fn test_literal_match() {
        let mut matcher = PatternMatcher::new();
        matcher
            .add_pattern(
                "greeting",
                Pattern::new(vec![
                    PatternElement::Literal("hello".to_string()),
                    PatternElement::Literal("world".to_string()),
                ]),
            )
            .expect("add_pattern failed");

        let tokens = tokenize_simple("hello world foo bar");
        let matches = matcher.match_all(&tokens);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].start, 0);
        assert_eq!(matches[0].end, 2);
    }

    #[test]
    fn test_pos_match() {
        let mut matcher = PatternMatcher::new();
        matcher
            .add_pattern(
                "dt_nn",
                Pattern::new(vec![
                    PatternElement::PoS("DT".to_string()),
                    PatternElement::PoS("NN".to_string()),
                ]),
            )
            .expect("add_pattern failed");

        let tokens = vec![
            Token::new("the").with_pos("DT"),
            Token::new("dog").with_pos("NN"),
            Token::new("runs").with_pos("VBZ"),
        ];
        let matches = matcher.match_all(&tokens);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].groups, vec!["the", "dog"]);
    }

    #[test]
    fn test_regex_match() {
        let mut matcher = PatternMatcher::new();
        matcher
            .add_pattern(
                "money",
                Pattern::new(vec![PatternElement::Regex(
                    r"\$[0-9]+(?:\.[0-9]+)?".to_string(),
                )]),
            )
            .expect("add_pattern failed");

        let tokens = tokenize_simple("costs $29.99 shipping $5");
        let matches = matcher.match_all(&tokens);
        assert_eq!(matches.len(), 2);
    }

    #[test]
    fn test_any_match() {
        let mut matcher = PatternMatcher::new();
        matcher
            .add_pattern(
                "any_word",
                Pattern::new(vec![
                    PatternElement::Literal("the".to_string()),
                    PatternElement::Any,
                ]),
            )
            .expect("add_pattern failed");

        let tokens = tokenize_simple("the cat sat on the mat");
        let matches = matcher.match_all(&tokens);
        // "the cat" and "the mat"
        assert!(matches.len() >= 2);
    }

    #[test]
    fn test_gap_match() {
        let mut matcher = PatternMatcher::new();
        matcher
            .add_pattern(
                "verb_phrase",
                Pattern::new(vec![
                    PatternElement::Literal("john".to_string()),
                    PatternElement::Gap { min: 0, max: 2 },
                    PatternElement::Literal("mary".to_string()),
                ]),
            )
            .expect("add_pattern failed");

        let tokens = tokenize_simple("john loves mary");
        let matches = matcher.match_all(&tokens);
        assert!(!matches.is_empty());
    }

    #[test]
    fn test_ner_patterns_email() {
        let matcher = build_ner_pattern_matcher().expect("build failed");
        let tokens = tokenize_simple("contact user@example.com for info");
        let matches = matcher.match_all(&tokens);
        assert!(matches.iter().any(|m| m.pattern_name == "EMAIL"));
    }

    #[test]
    fn test_ner_patterns_money() {
        let matcher = build_ner_pattern_matcher().expect("build failed");
        let tokens = tokenize_simple("costs $100 today");
        let matches = matcher.match_all(&tokens);
        assert!(matches.iter().any(|m| m.pattern_name == "MONEY"));
    }

    #[test]
    fn test_bad_regex_error() {
        let mut matcher = PatternMatcher::new();
        let result = matcher.add_pattern(
            "bad",
            Pattern::new(vec![PatternElement::Regex("[invalid".to_string())]),
        );
        assert!(result.is_err());
    }
}