Struct vaporetto::Predictor

source ·
pub struct Predictor(_);
Expand description

Predictor created from the model.

Example 1: without tag prediction

use std::fs::File;

use vaporetto::{Model, Predictor, Sentence};

let f = File::open("../resources/model.bin").unwrap();
let model = Model::read(f).unwrap();
let predictor = Predictor::new(model, false).unwrap();

let mut s = Sentence::from_raw("まぁ社長は火星猫だ").unwrap();
predictor.predict(&mut s);
// s.fill_tags(); will panic!

let mut buf = String::new();
s.write_tokenized_text(&mut buf);
assert_eq!(
    "まぁ 社長 は 火星 猫 だ",
    buf,
);

Example 2: with tag prediction

Tag prediction requires crate feature tag-prediction.

use std::fs::File;

use vaporetto::{Model, Predictor, Sentence};

let mut f = File::open("../resources/model.bin").unwrap();
let model = Model::read(f).unwrap();
let predictor = Predictor::new(model, true).unwrap();

let mut s = Sentence::from_raw("まぁ社長は火星猫だ").unwrap();
predictor.predict(&mut s);
s.fill_tags();

let mut buf = String::new();
s.write_tokenized_text(&mut buf);
assert_eq!(
    "まぁ/名詞/マー 社長/名詞/シャチョー は/助詞/ワ 火星/名詞/カセー 猫/名詞/ネコ だ/助動詞/ダ",
    buf,
);

Implementations§

source§

impl Predictor

source

pub fn new(model: Model, predict_tags: bool) -> Result<Self>

Creates a new predictor from the model.

Arguments
  • model - A model data.
  • predict_tags - If you want to predict tags, set to true.
Errors

Returns an error variant when the model is invalid.

source

pub fn predict<'a>(&'a self, sentence: &mut Sentence<'_, 'a>)

Predicts word boundaries of the given sentence. If necessary, this function also prepares for predicting tags.

source

pub fn serialize_to_vec(&self) -> Result<Vec<u8>>

Serializes the predictor into a Vec.

source

pub unsafe fn deserialize_from_slice_unchecked( data: &[u8] ) -> Result<(Self, &[u8])>

Deserializes a predictor from a given slice and returns a tuple of the predictor and the remaining slice.

Safety

The given data must be a correct predictor exported by Predictor::serialize_to_vec() function.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.