lindera-analysis 5.0.0

Text analysis chain (character filters, token filters, tokenizer) for Lindera.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use std::collections::HashMap;

use daachorse::DoubleArrayAhoCorasick;
use daachorse::DoubleArrayAhoCorasickBuilder;
use daachorse::MatchKind;
use serde_json::Value;

use crate::character_filter::{CharacterFilter, OffsetMapping, Transformation};
use lindera::LinderaResult;
use lindera::error::LinderaErrorKind;

pub const MAPPING_CHARACTER_FILTER_NAME: &str = "mapping";

pub type MappingCharacterFilterConfig = Value;

#[derive(Clone)]
pub struct MappingCharacterFilter {
    mapping: HashMap<String, String>,
    trie: DoubleArrayAhoCorasick<u32>,
}

impl MappingCharacterFilter {
    /// Create a new `MappingCharacterFilter` from a surface-to-replacement mapping.
    ///
    /// # Arguments
    ///
    /// * `mapping` - A map from surface text to its replacement text. Keys must be
    ///   non-empty; an empty key would match at every byte position under the
    ///   leftmost-longest search strategy used by `apply`, which is never a
    ///   meaningful mapping and is therefore rejected.
    ///
    /// # Returns
    ///
    /// A `MappingCharacterFilter`, or an error if `mapping` contains an empty key or
    /// the underlying Aho-Corasick automaton fails to build.
    pub fn new(mapping: HashMap<String, String>) -> LinderaResult<Self> {
        if mapping.keys().any(|key| key.is_empty()) {
            return Err(LinderaErrorKind::Args
                .with_error(anyhow::anyhow!("mapping key must not be empty.")));
        }

        let mut keyset: Vec<(&[u8], u32)> = Vec::new();
        let mut keys = mapping.keys().collect::<Vec<_>>();
        keys.sort();
        for (value, key) in keys.into_iter().enumerate() {
            keyset.push((key.as_bytes(), value as u32));
        }

        let trie = DoubleArrayAhoCorasickBuilder::new()
            .match_kind(MatchKind::LeftmostLongest)
            .build_with_values(keyset)
            .map_err(|err| LinderaErrorKind::Build.with_error(anyhow::anyhow!(err)))?;

        Ok(Self { mapping, trie })
    }

    pub fn from_config(config: &MappingCharacterFilterConfig) -> LinderaResult<Self> {
        let mapping = config
            .get("mapping")
            .and_then(Value::as_object)
            .ok_or_else(|| {
                LinderaErrorKind::Parse.with_error(anyhow::anyhow!("mapping must be an object."))
            })?
            .iter()
            .filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string())))
            .collect::<HashMap<String, String>>();

        Self::new(mapping)
    }
}

impl CharacterFilter for MappingCharacterFilter {
    fn name(&self) -> &'static str {
        MAPPING_CHARACTER_FILTER_NAME
    }

    /// Apply the filter using the `OffsetMapping` API.
    ///
    /// Performs a single leftmost-longest pass over `text` with the underlying
    /// Aho-Corasick automaton: at each position the longest configured key wins,
    /// matches never overlap, and scanning resumes immediately after each match
    /// (mirroring `docs/src/concepts/filters.md`'s documented "longest-match
    /// search" semantics for this filter).
    ///
    /// # Arguments
    ///
    /// * `text` - The text to filter, replaced in place with the mapped text.
    ///
    /// # Returns
    ///
    /// An `OffsetMapping` recording one `Transformation` per replacement whose
    /// byte length differs from the original (length-preserving replacements are
    /// not recorded), in ascending filtered-offset order.
    fn apply(&self, text: &mut String) -> LinderaResult<OffsetMapping> {
        let mut filtered_text = String::with_capacity(text.len());
        let mut mapping = OffsetMapping::new();

        {
            let source = text.as_str();
            let mut cursor = 0_usize;

            for m in self.trie.leftmost_find_iter(source) {
                // Keys are validated non-empty in `new`, and all keys are valid
                // UTF-8, so matches are non-empty, non-overlapping, strictly
                // ascending, and always land on char boundaries.
                debug_assert!(m.start() >= cursor && m.end() > m.start());

                // Copy the unmatched gap before this match verbatim.
                filtered_text.push_str(&source[cursor..m.start()]);

                let input_start = m.start();
                let input_len = m.end() - m.start();
                let replacement_text = &self.mapping[&source[m.start()..m.end()]];
                let replacement_len = replacement_text.len();

                // Record transformation if text changed
                if input_len != replacement_len {
                    let transformation = Transformation::new(
                        input_start,
                        input_start + input_len,
                        filtered_text.len(),
                        filtered_text.len() + replacement_len,
                    );
                    mapping.add_transformation(transformation);
                }

                filtered_text.push_str(replacement_text);
                cursor = m.end();
            }

            // Copy the trailing unmatched tail.
            filtered_text.push_str(&source[cursor..]);
        }

        *text = filtered_text;
        Ok(mapping)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use crate::character_filter::mapping::{MappingCharacterFilter, MappingCharacterFilterConfig};
    use crate::character_filter::{CharacterFilter, Transformation};

    #[test]
    fn test_mapping_character_filter_config() {
        let config_str = r#"
        {
            "mapping": {
                "ア": "ア",
                "イ": "イ",
                "ウ": "ウ",
                "エ": "エ",
                "オ": "オ"
            }
        }
        "#;
        let result: Result<MappingCharacterFilterConfig, _> = serde_json::from_str(config_str);
        assert!(result.is_ok());
    }

    #[test]
    fn test_mapping_character_filter_from_config() {
        let config_str = r#"
        {
            "mapping": {
                "ア": "ア",
                "イ": "イ",
                "ウ": "ウ",
                "エ": "エ",
                "オ": "オ"
            }
        }
        "#;
        let config = serde_json::from_str::<MappingCharacterFilterConfig>(config_str).unwrap();

        let result = MappingCharacterFilter::from_config(&config);
        assert!(result.is_ok());
    }

    #[test]
    fn test_mapping_character_filter_apply() {
        {
            let config_str = r#"
            {
                "mapping": {
                    "ア": "ア",
                    "イ": "イ",
                    "ウ": "ウ",
                    "エ": "エ",
                    "オ": "オ"
                }
            }
            "#;
            let config = serde_json::from_str::<MappingCharacterFilterConfig>(config_str).unwrap();

            let filter = MappingCharacterFilter::from_config(&config).unwrap();

            let original_text = "アイウエオ";
            let mut text = original_text.to_string();
            let mapping = filter.apply(&mut text).unwrap();
            assert_eq!("アイウエオ", text.as_str());
            assert!(mapping.is_empty());

            // Test text fragments
            let start = 3;
            let end = 6;
            assert_eq!("", &text[start..end]);
            let correct_start = mapping.correct_offset(start, text.len());
            let correct_end = mapping.correct_offset(end, text.len());
            assert_eq!(3, correct_start);
            assert_eq!(6, correct_end);
            assert_eq!("", &original_text[correct_start..correct_end]);
        }

        {
            let config_str = r#"
            {
                "mapping": {
                    "リ": "リ",
                    "ン": "ン",
                    "デ": "デ",
                    "ラ": "ラ"
                }
            }
            "#;
            let config = serde_json::from_str::<MappingCharacterFilterConfig>(config_str).unwrap();

            let filter = MappingCharacterFilter::from_config(&config).unwrap();
            let original_text = "リンデラ";
            let mut text = original_text.to_string();
            let mapping = filter.apply(&mut text).unwrap();
            assert_eq!("リンデラ", text.as_str());

            // Verify transformation: "デ"(6-12) → "デ"(6-9)
            assert_eq!(1, mapping.transformations.len());
            let transform = &mapping.transformations[0];
            assert_eq!(6, transform.original_start);
            assert_eq!(12, transform.original_end);
            assert_eq!(6, transform.filtered_start);
            assert_eq!(9, transform.filtered_end);

            // Test text fragments
            let start = 6;
            let end = 9;
            assert_eq!("", &text[start..end]);
            let correct_start = mapping.correct_offset(start, text.len());
            let correct_end = mapping.correct_offset(end, text.len());
            assert_eq!(6, correct_start);
            assert_eq!(12, correct_end);
            assert_eq!("デ", &original_text[correct_start..correct_end]);
        }

        {
            let config_str = r#"
            {
                "mapping": {
                    "リンデラ": "リンデラ"
                }
            }
            "#;
            let config = serde_json::from_str::<MappingCharacterFilterConfig>(config_str).unwrap();

            let filter = MappingCharacterFilter::from_config(&config).unwrap();
            let original_text = "リンデラ";
            let mut text = original_text.to_string();
            let mapping = filter.apply(&mut text).unwrap();
            assert_eq!("リンデラ", text.as_str());

            // Verify transformation: "リンデラ"(0-15) → "リンデラ"(0-12)
            assert_eq!(1, mapping.transformations.len());
            let transform = &mapping.transformations[0];
            assert_eq!(0, transform.original_start);
            assert_eq!(15, transform.original_end);
            assert_eq!(0, transform.filtered_start);
            assert_eq!(12, transform.filtered_end);

            // Test text fragments
            let start = 0;
            let end = 12;
            assert_eq!("リンデラ", &text[start..end]);
            let correct_start = mapping.correct_offset(start, text.len());
            let correct_end = mapping.correct_offset(end, text.len());
            assert_eq!(0, correct_start);
            assert_eq!(15, correct_end);
            assert_eq!("リンデラ", &original_text[correct_start..correct_end]);
        }

        {
            let config_str = r#"
            {
                "mapping": {
                    "リンデラ": "Lindera"
                }
            }
            "#;
            let config = serde_json::from_str::<MappingCharacterFilterConfig>(config_str).unwrap();

            let filter = MappingCharacterFilter::from_config(&config).unwrap();
            let original_text = "Rust製形態素解析器リンデラで日本語を形態素解析する。";
            let mut text = original_text.to_string();
            let mapping = filter.apply(&mut text).unwrap();
            assert_eq!(
                "Rust製形態素解析器Linderaで日本語を形態素解析する。",
                text.as_str()
            );

            // Verify transformation: "リンデラ"(25-37) → "Lindera"(25-32)
            assert_eq!(1, mapping.transformations.len());
            let transform = &mapping.transformations[0];
            assert_eq!(25, transform.original_start);
            assert_eq!(37, transform.original_end);
            assert_eq!(25, transform.filtered_start);
            assert_eq!(32, transform.filtered_end);

            // Test text fragments
            let start = 25;
            let end = 32;
            assert_eq!("Lindera", &text[start..end]);
            let correct_start = mapping.correct_offset(start, text.len());
            let correct_end = mapping.correct_offset(end, text.len());
            assert_eq!(25, correct_start);
            assert_eq!(37, correct_end);
            assert_eq!("リンデラ", &original_text[correct_start..correct_end]);

            let start = 35;
            let end = 44;
            assert_eq!("日本語", &text[start..end]);
            let correct_start = mapping.correct_offset(start, text.len());
            let correct_end = mapping.correct_offset(end, text.len());
            assert_eq!(40, correct_start);
            assert_eq!(49, correct_end);
            assert_eq!("日本語", &original_text[correct_start..correct_end]);
        }

        {
            let config_str = r#"
            {
                "mapping": {
                    "1": "1",
                    "0": "0",
                    "㍑": "リットル"
                }
            }
            "#;
            let config = serde_json::from_str(config_str).unwrap();

            let filter = MappingCharacterFilter::from_config(&config).unwrap();
            let original_text = "10㍑";
            let mut text = original_text.to_string();
            let mapping = filter.apply(&mut text).unwrap();
            assert_eq!("10リットル", text.as_str());

            // All three replacements are recorded because of byte length differences
            assert_eq!(3, mapping.transformations.len());

            // Verify the last transformation: "㍑"(6-9) → "リットル"(2-14)
            let transform = &mapping.transformations[2];
            assert_eq!(6, transform.original_start);
            assert_eq!(9, transform.original_end);
            assert_eq!(2, transform.filtered_start);
            assert_eq!(14, transform.filtered_end);

            // Test text fragments
            let start = 2;
            let end = 14;
            assert_eq!("リットル", &text[start..end]);
            let correct_start = mapping.correct_offset(start, text.len());
            let correct_end = mapping.correct_offset(end, text.len());
            assert_eq!(6, correct_start);
            assert_eq!(9, correct_end);
            assert_eq!("", &original_text[correct_start..correct_end]);
        }
    }

    #[test]
    fn test_mapping_character_filter_apply_longest_match() {
        let mut mapping = HashMap::new();
        mapping.insert("ab".to_string(), "X".to_string());
        mapping.insert("abc".to_string(), "YY".to_string());
        mapping.insert("b".to_string(), "Z".to_string());
        let filter = MappingCharacterFilter::new(mapping).unwrap();

        let mut text = "abcabx".to_string();
        let mapping = filter.apply(&mut text).unwrap();
        assert_eq!("YYXx", text.as_str());

        // "abc" (longest match at 0) wins over "ab"/"b"; "ab" (longest at 3) wins over "b".
        assert_eq!(2, mapping.transformations.len());
        assert_eq!(Transformation::new(0, 3, 0, 2), mapping.transformations[0]);
        assert_eq!(Transformation::new(3, 5, 2, 3), mapping.transformations[1]);
    }

    #[test]
    fn test_mapping_character_filter_apply_backtrack() {
        // "abcd" fails to match "abx", but the shorter key "b" hiding inside the
        // failed prefix must still be found via the automaton's fail links.
        let mut mapping = HashMap::new();
        mapping.insert("abcd".to_string(), "1".to_string());
        mapping.insert("b".to_string(), "22".to_string());
        let filter = MappingCharacterFilter::new(mapping).unwrap();

        let mut text = "abx".to_string();
        let mapping = filter.apply(&mut text).unwrap();
        assert_eq!("a22x", text.as_str());
        assert_eq!(1, mapping.transformations.len());
        assert_eq!(Transformation::new(1, 2, 1, 3), mapping.transformations[0]);
    }

    #[test]
    fn test_mapping_character_filter_apply_leftmost_wins() {
        // The long leftmost match consumes "h", so the overlapping key "hz"
        // starting inside it must not fire.
        let mut mapping = HashMap::new();
        mapping.insert("abcdefgh".to_string(), "1".to_string());
        mapping.insert("hz".to_string(), "2".to_string());
        let filter = MappingCharacterFilter::new(mapping).unwrap();

        let mut text = "abcdefghz".to_string();
        let mapping = filter.apply(&mut text).unwrap();
        assert_eq!("1z", text.as_str());
        assert_eq!(1, mapping.transformations.len());
    }

    #[test]
    fn test_mapping_character_filter_apply_shared_prefix() {
        // "デ" and "ラ" share the "EF BE" lead byte pair; this exercises fail-link
        // recovery mid-multibyte-character and proves the gap copy never slices
        // across a char boundary.
        let mut mapping = HashMap::new();
        mapping.insert("デ".to_string(), "".to_string());
        mapping.insert("".to_string(), "".to_string());
        let filter = MappingCharacterFilter::new(mapping).unwrap();

        let mut text = "テラ".to_string();
        let mapping = filter.apply(&mut text).unwrap();
        assert_eq!("テラ", text.as_str());
        // "ラ" -> "ラ" is a same-byte-length substitution (3 -> 3 bytes).
        assert!(mapping.is_empty());
    }

    #[test]
    fn test_mapping_character_filter_empty_key_rejected() {
        let mut mapping = HashMap::new();
        mapping.insert(String::new(), "x".to_string());
        assert!(MappingCharacterFilter::new(mapping).is_err());

        let mut mapping = HashMap::new();
        mapping.insert("a".to_string(), "b".to_string());
        assert!(MappingCharacterFilter::new(mapping).is_ok());
    }

    #[test]
    fn test_mapping_character_filter_apply_large_input() {
        let mut mapping = HashMap::new();
        mapping.insert("リンデラ".to_string(), "Lindera".to_string());
        let filter = MappingCharacterFilter::new(mapping).unwrap();

        // Large, entirely non-matching input: the worst case for the previous
        // O(n^2) implementation. A generous absolute wall-clock ceiling is used
        // instead of a two-point scaling ratio, since a linear implementation
        // finishes in low single-digit milliseconds here (leaving a huge margin)
        // while the previous quadratic implementation would take several seconds
        // at this size, making the ceiling a reliable regression guard without
        // being sensitive to CI timing noise.
        let mut text = "".repeat(100_000);
        let original_len = text.len();

        let start = std::time::Instant::now();
        let mapping = filter.apply(&mut text).unwrap();
        let elapsed = start.elapsed();

        assert_eq!(original_len, text.len());
        assert!(mapping.is_empty());
        assert!(
            elapsed.as_secs() < 3,
            "apply() took too long ({elapsed:?}); the quadratic-scan regression may have returned"
        );
    }
}