sudachi/plugin/input_text/default_input_text/
mod.rs

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
/*
 * Copyright (c) 2021-2024 Works Applications Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

use std::collections::{HashMap, HashSet};
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;

use aho_corasick::{
    AhoCorasick, AhoCorasickBuilder, AhoCorasickKind, Anchored, MatchKind, StartKind,
};
use serde::Deserialize;
use serde_json::Value;
use unicode_normalization::{is_nfkc_quick, IsNormalized, UnicodeNormalization};

use crate::config::{Config, ConfigError};
use crate::dic::grammar::Grammar;
use crate::hash::RoMu;
use crate::input_text::{InputBuffer, InputEditor};
use crate::plugin::input_text::InputTextPlugin;
use crate::prelude::*;

#[cfg(test)]
mod tests;

const DEFAULT_REWRITE_DEF_FILE: &str = "rewrite.def";
const DEFAULT_REWRITE_DEF_BYTES: &[u8] = include_bytes!("../../../../../resources/rewrite.def");

/// Provides basic normalization of the input text
#[derive(Default)]
pub struct DefaultInputTextPlugin {
    /// Set of characters to skip normalization
    ignore_normalize_set: HashSet<char, RoMu>,
    /// Mapping from a character to the maximum char_length of possible replacement
    key_lengths: HashMap<char, usize>,
    /// Replacement mapping
    replace_char_map: HashMap<String, String>,
    /// Checks whether the string contains symbols to normalize
    checker: Option<AhoCorasick>,
    replacements: Vec<String>,
}

/// Struct corresponds with raw config json file.
#[allow(non_snake_case)]
#[derive(Deserialize)]
struct PluginSettings {
    rewriteDef: Option<PathBuf>,
}

impl DefaultInputTextPlugin {
    /// Loads rewrite definition
    ///
    /// Definition syntax:
    ///     Ignored normalize:
    ///         Each line contains a character
    ///     Replace char list:
    ///         Each line contains two strings separated by white spaces
    ///         Plugin replaces the first by the second
    ///         Same target string cannot be defined multiple times
    ///     Empty or line starts with "#" will be ignored
    fn read_rewrite_lists<T: BufRead>(&mut self, reader: T) -> SudachiResult<()> {
        let mut ignore_normalize_set = HashSet::with_hasher(RoMu::new());
        let mut key_lengths = HashMap::new();
        let mut replace_char_map = HashMap::new();
        for (i, line) in reader.lines().enumerate() {
            let line = line?;
            let line = line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            let cols: Vec<_> = line.split_whitespace().collect();

            // ignored normalize list
            if cols.len() == 1 {
                if cols[0].chars().count() != 1 {
                    return Err(SudachiError::InvalidDataFormat(
                        i,
                        format!("{} is not character", cols[0]),
                    ));
                }
                ignore_normalize_set.insert(cols[0].chars().next().unwrap());
                continue;
            }
            // replace char list
            if cols.len() == 2 {
                if replace_char_map.contains_key(cols[0]) {
                    return Err(SudachiError::InvalidDataFormat(
                        i,
                        format!("{} is already defined", cols[0]),
                    ));
                }
                let first_char = cols[0].chars().next().unwrap();
                let n_char = cols[0].chars().count();
                if key_lengths.get(&first_char).copied().unwrap_or(0) < n_char {
                    key_lengths.insert(first_char, n_char);
                }
                replace_char_map.insert(cols[0].to_string(), cols[1].to_string());
                continue;
            }
            return Err(SudachiError::InvalidDataFormat(i, "".to_string()));
        }

        self.ignore_normalize_set = ignore_normalize_set;
        self.key_lengths = key_lengths;
        self.replace_char_map = replace_char_map;

        let mut values: Vec<String> = Vec::new();
        let mut keys: Vec<String> = Vec::new();

        for (k, v) in self.replace_char_map.iter() {
            keys.push(k.clone());
            values.push(v.clone());
        }

        self.checker = Some(
            AhoCorasickBuilder::new()
                .kind(Some(AhoCorasickKind::DFA))
                .match_kind(MatchKind::LeftmostLongest)
                .start_kind(StartKind::Both)
                .build(keys.clone())
                .map_err(|e| {
                    ConfigError::InvalidFormat(format!("failed to parse rewrite.def: {e:?}"))
                })?,
        );

        self.replacements = values;

        Ok(())
    }

    #[inline]
    fn should_ignore(&self, ch: char) -> bool {
        self.ignore_normalize_set.contains(&ch)
    }

    /// Fast case: lowercasing is not needed and the string is already in NFKC
    /// Use AhoCorasick automaton to find all replacements and replace them
    ///
    /// Ignores are not used here, forced replacements have higher priority
    /// Fast version does not need to walk every character!
    fn replace_fast<'a>(
        &'a self,
        buffer: &InputBuffer,
        mut replacer: InputEditor<'a>,
    ) -> SudachiResult<InputEditor<'a>> {
        let cur = buffer.current();
        let checker = self.checker.as_ref().unwrap();

        let ac_input = aho_corasick::Input::new(cur).anchored(Anchored::No);

        for m in checker.find_iter(ac_input) {
            let replacement = self.replacements[m.pattern()].as_str();
            replacer.replace_ref(m.start()..m.end(), replacement);
        }

        Ok(replacer)
    }

    /// Slow case: need to handle lowercasing or NFKC normalization
    /// Slow version needs to walk every character
    fn replace_slow<'a>(
        &'a self,
        buffer: &InputBuffer,
        mut replacer: InputEditor<'a>,
    ) -> SudachiResult<InputEditor<'a>> {
        let cur = buffer.current();
        let checker = self.checker.as_ref().unwrap();
        let mut min_offset = 0;

        let mut ac_input = aho_corasick::Input::new(cur)
            .anchored(Anchored::Yes)
            .earliest(true);

        for (offset, ch) in cur.char_indices() {
            if offset < min_offset {
                continue;
            }
            ac_input.set_start(offset);
            // 1. replacement as defined by char.def
            if let Some(m) = checker.find(ac_input.clone()) {
                let range = m.range();
                let replacement = self.replacements[m.pattern()].as_str();
                min_offset = range.end;
                replacer.replace_ref(range, replacement);
                continue;
            }

            // 2. handle normalization
            let need_lowercase = ch.is_uppercase();
            let need_nkfc =
                !self.should_ignore(ch) && is_nfkc_quick(std::iter::once(ch)) != IsNormalized::Yes;

            // iterator types are incompatible, so calls can't be moved outside branches
            match (need_lowercase, need_nkfc) {
                //no need to do anything
                (false, false) => continue,
                // only lowercasing
                (true, false) => {
                    let chars = ch.to_lowercase();
                    self.handle_normalization_slow(chars, &mut replacer, offset, ch.len_utf8(), ch)
                }
                // only normalization
                (false, true) => {
                    let chars = std::iter::once(ch).nfkc();
                    self.handle_normalization_slow(chars, &mut replacer, offset, ch.len_utf8(), ch)
                }
                // both
                (true, true) => {
                    let chars = ch.to_lowercase().nfkc();
                    self.handle_normalization_slow(chars, &mut replacer, offset, ch.len_utf8(), ch)
                }
            }
        }
        Ok(replacer)
    }

    fn handle_normalization_slow<'a, I: Iterator<Item = char>>(
        &'a self,
        mut data: I,
        replacer: &mut InputEditor<'a>,
        start: usize,
        len: usize,
        ch: char,
    ) {
        if let Some(ch2) = data.next() {
            if ch2 != ch {
                replacer.replace_char_iter(start..start + len, ch2, data)
            }
        }
    }
}

impl InputTextPlugin for DefaultInputTextPlugin {
    fn set_up(
        &mut self,
        settings: &Value,
        config: &Config,
        _grammar: &Grammar,
    ) -> SudachiResult<()> {
        let settings: PluginSettings = serde_json::from_value(settings.clone())?;

        let rewrite_file_path = config.complete_path(
            settings
                .rewriteDef
                .unwrap_or_else(|| DEFAULT_REWRITE_DEF_FILE.into()),
        );

        if rewrite_file_path.is_ok() {
            let reader = BufReader::new(fs::File::open(rewrite_file_path?)?);
            self.read_rewrite_lists(reader)?;
        } else {
            let reader = BufReader::new(DEFAULT_REWRITE_DEF_BYTES);
            self.read_rewrite_lists(reader)?;
        }

        Ok(())
    }

    fn uses_chars(&self) -> bool {
        true
    }

    fn rewrite_impl<'a>(
        &'a self,
        buffer: &InputBuffer,
        edit: InputEditor<'a>,
    ) -> SudachiResult<InputEditor<'a>> {
        let chars = buffer.current_chars();
        let need_nkfc = is_nfkc_quick(chars.iter().cloned()) != IsNormalized::Yes;

        let need_lowercase = chars.iter().any(|c| c.is_uppercase());

        if need_nkfc || need_lowercase {
            self.replace_slow(buffer, edit)
        } else {
            self.replace_fast(buffer, edit)
        }
    }
}