sudachi/plugin/oov/simple_oov/
mod.rsuse crate::analysis::created::CreatedWords;
use serde::Deserialize;
use serde_json::Value;
use crate::analysis::Node;
use crate::config::Config;
use crate::dic::grammar::Grammar;
use crate::dic::word_id::WordId;
use crate::input_text::InputBuffer;
use crate::plugin::oov::OovProviderPlugin;
use crate::prelude::*;
use crate::util::check_params::CheckParams;
use crate::util::user_pos::{UserPosMode, UserPosSupport};
#[derive(Default)]
pub struct SimpleOovPlugin {
left_id: u16,
right_id: u16,
cost: i16,
oov_pos_id: u16,
}
#[allow(non_snake_case)]
#[derive(Deserialize)]
struct PluginSettings {
oovPOS: Vec<String>,
leftId: i64,
rightId: i64,
cost: i64,
#[serde(default)]
userPOS: UserPosMode,
}
impl OovProviderPlugin for SimpleOovPlugin {
fn set_up(
&mut self,
settings: &Value,
_config: &Config,
mut grammar: &mut Grammar,
) -> SudachiResult<()> {
let settings: PluginSettings = serde_json::from_value(settings.clone())?;
self.oov_pos_id = grammar.handle_user_pos(&settings.oovPOS, settings.userPOS)?;
self.left_id = grammar.check_left_id(settings.leftId)?;
self.right_id = grammar.check_right_id(settings.rightId)?;
self.cost = grammar.check_cost(settings.cost)?;
Ok(())
}
fn provide_oov(
&self,
input_text: &InputBuffer,
offset: usize,
other_words: CreatedWords,
result: &mut Vec<Node>,
) -> SudachiResult<usize> {
if other_words.not_empty() {
return Ok(0);
}
let length = input_text.get_word_candidate_length(offset);
result.push(Node::new(
offset as u16,
(offset + length) as u16,
self.left_id,
self.right_id,
self.cost,
WordId::oov(self.oov_pos_id as u32),
));
Ok(1)
}
}