use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "api", derive(utoipa::ToSchema))]
#[serde(rename_all = "lowercase")]
pub enum KeywordAlgorithm {
#[cfg(feature = "keywords-yake")]
Yake,
#[cfg(feature = "keywords-rake")]
Rake,
}
impl Default for KeywordAlgorithm {
fn default() -> Self {
#[cfg(feature = "keywords-yake")]
return Self::Yake;
#[cfg(all(feature = "keywords-rake", not(feature = "keywords-yake")))]
return Self::Rake;
#[cfg(not(any(feature = "keywords-yake", feature = "keywords-rake")))]
compile_error!("At least one keyword extraction feature must be enabled");
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "api", derive(utoipa::ToSchema))]
pub struct Keyword {
pub text: String,
pub score: f32,
pub algorithm: KeywordAlgorithm,
#[serde(skip_serializing_if = "Option::is_none")]
pub positions: Option<Vec<usize>>,
}
impl Keyword {
pub fn new(text: String, score: f32, algorithm: KeywordAlgorithm) -> Self {
Self {
text,
score,
algorithm,
positions: None,
}
}
pub fn with_positions(text: String, score: f32, algorithm: KeywordAlgorithm, positions: Vec<usize>) -> Self {
Self {
text,
score,
algorithm,
positions: Some(positions),
}
}
}