use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "api", derive(utoipa::ToSchema))]
pub struct Entity {
pub category: EntityCategory,
pub text: String,
pub start: u32,
pub end: u32,
#[serde(skip_serializing_if = "Option::is_none")]
pub confidence: Option<f32>,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "api", derive(utoipa::ToSchema))]
#[serde(rename_all = "snake_case")]
pub enum EntityCategory {
Person,
Organization,
Location,
Date,
Time,
Money,
Percent,
Email,
Phone,
Url,
Custom(String),
}
impl Default for EntityCategory {
fn default() -> Self {
Self::Custom(String::new())
}
}
impl From<String> for EntityCategory {
fn from(s: String) -> Self {
match s.as_str() {
"person" => Self::Person,
"organization" => Self::Organization,
"location" => Self::Location,
"date" => Self::Date,
"time" => Self::Time,
"money" => Self::Money,
"percent" => Self::Percent,
"email" => Self::Email,
"phone" => Self::Phone,
"url" => Self::Url,
other => Self::Custom(other.to_string()),
}
}
}
impl std::str::FromStr for EntityCategory {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(s.to_string()))
}
}