use super::ffi;
use super::{Error, Lattice, Tagger};
use super::{LcAttr, RcAttr};
use std::ffi::CStr;
use std::ptr::NonNull;
pub struct Model {
pub(crate) inner: NonNull<ffi::mecab_model_t>,
}
impl Model {
pub fn as_ptr(&self) -> *mut ffi::mecab_model_t {
self.inner.as_ptr()
}
pub fn from_cli_arg(arg: &CStr) -> Result<Self, Error> {
unsafe {
let inner = ffi::mecab_model_new2(arg.as_ptr());
NonNull::new(inner)
.map(|inner| Self { inner })
.ok_or_else(Error::global)
}
}
pub fn new_tagger(&self) -> Result<Tagger<'_>, Error> {
Tagger::new(self)
}
pub fn new_lattice(&self) -> Result<Lattice<'_>, Error> {
Lattice::new(self)
}
pub fn transition_cost(&self, rc_attr: RcAttr, lc_attr: LcAttr) -> i32 {
unsafe {
ffi::mecab_model_transition_cost(self.as_ptr(), rc_attr.to_raw(), lc_attr.to_raw())
}
}
}
impl Drop for Model {
fn drop(&mut self) {
unsafe {
ffi::mecab_model_destroy(self.as_ptr());
}
}
}
unsafe impl Sync for Model {}
unsafe impl Send for Model {}