use lindera::token::Token;
#[napi(js_name = "Token")]
#[derive(Clone)]
pub struct JsToken {
surface: String,
byte_start: u32,
byte_end: u32,
position: u32,
word_id: u32,
is_unknown: bool,
details: Option<Vec<String>>,
}
#[napi]
impl JsToken {
#[napi(getter)]
pub fn surface(&self) -> String {
self.surface.clone()
}
#[napi(getter)]
pub fn byte_start(&self) -> u32 {
self.byte_start
}
#[napi(getter)]
pub fn byte_end(&self) -> u32 {
self.byte_end
}
#[napi(getter)]
pub fn position(&self) -> u32 {
self.position
}
#[napi(getter)]
pub fn word_id(&self) -> u32 {
self.word_id
}
#[napi(getter)]
pub fn is_unknown(&self) -> bool {
self.is_unknown
}
#[napi(getter)]
pub fn details(&self) -> Option<Vec<String>> {
self.details.clone()
}
#[napi]
pub fn get_detail(&self, index: u32) -> Option<String> {
self.details
.as_ref()
.and_then(|d| d.get(index as usize).cloned())
}
}
impl JsToken {
pub fn from_token(mut token: Token) -> Self {
let details = token.details().iter().map(|s| s.to_string()).collect();
Self {
surface: token.surface.to_string(),
byte_start: token.byte_start as u32,
byte_end: token.byte_end as u32,
position: token.position as u32,
word_id: token.word_id.id,
is_unknown: token.word_id.is_unknown(),
details: Some(details),
}
}
}
#[napi(js_name = "NbestResult")]
pub struct JsNbestResult {
tokens: Vec<JsToken>,
cost: i64,
}
#[napi]
impl JsNbestResult {
#[napi(getter)]
pub fn tokens(&self) -> Vec<JsToken> {
self.tokens.clone()
}
#[napi(getter)]
pub fn cost(&self) -> i64 {
self.cost
}
}
impl JsNbestResult {
pub fn new(tokens: Vec<JsToken>, cost: i64) -> Self {
Self { tokens, cost }
}
}