sudachi/plugin/path_rewrite/
mod.rs

1/*
2 * Copyright (c) 2021-2024 Works Applications Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17pub mod join_katakana_oov;
18pub mod join_numeric;
19
20use serde_json::Value;
21
22use crate::analysis::lattice::Lattice;
23use crate::analysis::node::ResultNode;
24use crate::config::Config;
25use crate::dic::grammar::Grammar;
26use crate::input_text::InputBuffer;
27use crate::plugin::path_rewrite::join_katakana_oov::JoinKatakanaOovPlugin;
28use crate::plugin::path_rewrite::join_numeric::JoinNumericPlugin;
29use crate::plugin::PluginCategory;
30use crate::prelude::*;
31
32/// Trait of plugin to rewrite the path from lattice
33pub trait PathRewritePlugin: Sync + Send {
34    /// Loads necessary information for the plugin
35    fn set_up(&mut self, settings: &Value, config: &Config, grammar: &Grammar)
36        -> SudachiResult<()>;
37
38    /// Returns a rewritten path
39    fn rewrite(
40        &self,
41        text: &InputBuffer,
42        path: Vec<ResultNode>,
43        lattice: &Lattice,
44    ) -> SudachiResult<Vec<ResultNode>>;
45}
46
47impl PluginCategory for dyn PathRewritePlugin {
48    type BoxType = Box<dyn PathRewritePlugin + Sync + Send>;
49    type InitFnType = unsafe fn() -> SudachiResult<Self::BoxType>;
50    fn configurations(cfg: &Config) -> &[Value] {
51        &cfg.path_rewrite_plugins
52    }
53
54    fn bundled_impl(name: &str) -> Option<Self::BoxType> {
55        match name {
56            "JoinNumericPlugin" => Some(Box::<JoinNumericPlugin>::default()),
57            "JoinKatakanaOovPlugin" => Some(Box::<JoinKatakanaOovPlugin>::default()),
58            _ => None,
59        }
60    }
61
62    fn do_setup(
63        ptr: &mut Self::BoxType,
64        settings: &Value,
65        config: &Config,
66        grammar: &mut Grammar,
67    ) -> SudachiResult<()> {
68        ptr.set_up(settings, config, grammar)
69    }
70}