Skip to main content

sudachi/plugin/
path_rewrite.rs

1/*
2 * Copyright (c) 2021-2026 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::dic::word_info::WordInfoResolver;
27use crate::input_text::InputBuffer;
28use crate::plugin::path_rewrite::join_katakana_oov::JoinKatakanaOovPlugin;
29use crate::plugin::path_rewrite::join_numeric::JoinNumericPlugin;
30use crate::plugin::PluginCategory;
31use crate::prelude::*;
32
33/// Trait of plugin to rewrite the path from lattice
34pub trait PathRewritePlugin: Sync + Send {
35    /// Loads necessary information for the plugin
36    fn set_up(&mut self, settings: &Value, config: &Config, grammar: &Grammar)
37        -> SudachiResult<()>;
38
39    /// Returns a rewritten path
40    fn rewrite(
41        &self,
42        text: &InputBuffer,
43        path: Vec<ResultNode>,
44        lattice: &Lattice,
45        resolver: &dyn WordInfoResolver,
46    ) -> SudachiResult<Vec<ResultNode>>;
47}
48
49impl PluginCategory for dyn PathRewritePlugin {
50    type BoxType = Box<dyn PathRewritePlugin + Sync + Send>;
51    type InitFnType = unsafe fn() -> SudachiResult<Self::BoxType>;
52    fn configurations(cfg: &Config) -> &[Value] {
53        &cfg.path_rewrite_plugins
54    }
55
56    fn bundled_impl(name: &str) -> Option<Self::BoxType> {
57        match name {
58            "JoinNumericPlugin" => Some(Box::<JoinNumericPlugin>::default()),
59            "JoinKatakanaOovPlugin" => Some(Box::<JoinKatakanaOovPlugin>::default()),
60            _ => None,
61        }
62    }
63
64    fn do_setup(
65        ptr: &mut Self::BoxType,
66        settings: &Value,
67        config: &Config,
68        grammar: &mut Grammar,
69    ) -> SudachiResult<()> {
70        ptr.set_up(settings, config, grammar)
71    }
72}